Ansible 2.3 feature support for dellos9 and dellos10 (#22856)

* Ansible 2.3 feature support for dellos9 and dellos10
* Use Persistent Connection Manager

* Fix CI issue, revert the doc and metadata changes

* Reverted the meta_info (supported_by) to community from core

* Fixed the CI issues, use module_utisl.six and updated legacy-files
This commit is contained in:
Senthil Kumar Ganesan 2017-03-27 11:32:57 -07:00 committed by John R Barker
parent f9b75d44e8
commit dd63dfcf1e
17 changed files with 1083 additions and 416 deletions

View file

@ -330,9 +330,8 @@ DEFAULT_STRATEGY_PLUGIN_PATH = get_config(p, DEFAULTS, 'strategy_plugins', 'AN
'~/.ansible/plugins/strategy:/usr/share/ansible/plugins/strategy', value_type='pathlist') '~/.ansible/plugins/strategy:/usr/share/ansible/plugins/strategy', value_type='pathlist')
NETWORK_GROUP_MODULES = get_config(p, DEFAULTS, 'network_group_modules','NETWORK_GROUP_MODULES', ['eos', 'nxos', 'ios', 'iosxr', 'junos', NETWORK_GROUP_MODULES = get_config(p, DEFAULTS, 'network_group_modules','NETWORK_GROUP_MODULES', ['eos', 'nxos', 'ios', 'iosxr', 'junos',
'vyos', 'sros'], 'vyos', 'sros', 'dellos9', 'dellos10'],
value_type='list') value_type='list')
DEFAULT_STRATEGY = get_config(p, DEFAULTS, 'strategy', 'ANSIBLE_STRATEGY', 'linear') DEFAULT_STRATEGY = get_config(p, DEFAULTS, 'strategy', 'ANSIBLE_STRATEGY', 'linear')
DEFAULT_STDOUT_CALLBACK = get_config(p, DEFAULTS, 'stdout_callback', 'ANSIBLE_STDOUT_CALLBACK', 'default') DEFAULT_STDOUT_CALLBACK = get_config(p, DEFAULTS, 'stdout_callback', 'ANSIBLE_STDOUT_CALLBACK', 'default')
# cache # cache

View file

@ -1,5 +1,6 @@
# #
# (c) 2015 Peter Sprygada, <psprygada@ansible.com> # (c) 2015 Peter Sprygada, <psprygada@ansible.com>
# (c) 2017 Red Hat, Inc
# #
# Copyright (c) 2016 Dell Inc. # Copyright (c) 2016 Dell Inc.
# #
@ -28,28 +29,98 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
import re import re
from ansible.module_utils.network import register_transport, to_list from ansible.module_utils.basic import env_fallback
from ansible.module_utils.shell import CliBase from ansible.module_utils.network_common import to_list, ComplexList
from ansible.module_utils.netcfg import NetworkConfig, ConfigLine from ansible.module_utils.connection import exec_command
from ansible.module_utils.netcfg import NetworkConfig,ConfigLine
_DEVICE_CONFIGS = {}
WARNING_PROMPTS_RE = [
r"[\r\n]?\[confirm yes/no\]:\s?$",
r"[\r\n]?\[y/n\]:\s?$",
r"[\r\n]?\[yes/no\]:\s?$"
]
dellos10_argument_spec = {
'host': dict(),
'port': dict(type='int'),
'username': dict(fallback=(env_fallback, ['ANSIBLE_NET_USERNAME'])),
'password': dict(fallback=(env_fallback, ['ANSIBLE_NET_PASSWORD']), no_log=True),
'ssh_keyfile': dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'),
'authorize': dict(fallback=(env_fallback, ['ANSIBLE_NET_AUTHORIZE']), type='bool'),
'auth_pass': dict(fallback=(env_fallback, ['ANSIBLE_NET_AUTH_PASS']), no_log=True),
'timeout': dict(type='int'),
'provider': dict(type='dict'),
}
def check_args(module, warnings):
provider = module.params['provider'] or {}
for key in dellos10_argument_spec:
if key != 'provider' and module.params[key]:
warnings.append('argument %s has been deprecated and will be '
'removed in a future version' % key)
def get_config(module): def get_config(module, flags=[]):
contents = module.params['config'] cmd = 'show running-config '
cmd += ' '.join(flags)
cmd = cmd.strip()
if not contents: try:
contents = module.config.get_config() return _DEVICE_CONFIGS[cmd]
module.params['config'] = contents except KeyError:
return NetworkConfig(indent=1, contents=contents[0]) rc, out, err = exec_command(module, cmd)
else: if rc != 0:
return NetworkConfig(indent=1, contents=contents) module.fail_json(msg='unable to retrieve current config', stderr=err)
cfg = str(out).strip()
_DEVICE_CONFIGS[cmd] = cfg
return cfg
def to_commands(module, commands):
spec = {
'command': dict(key=True),
'prompt': dict(),
'answer': dict()
}
transform = ComplexList(spec, module)
return transform(commands)
def run_commands(module, commands, check_rc=True):
responses = list()
commands = to_commands(module, to_list(commands))
for cmd in commands:
cmd = module.jsonify(cmd)
rc, out, err = exec_command(module, cmd)
if check_rc and rc != 0:
module.fail_json(msg=err, rc=rc)
responses.append(out)
return responses
def load_config(module, commands):
rc, out, err = exec_command(module, 'configure terminal')
if rc != 0:
module.fail_json(msg='unable to enter configuration mode', err=err)
commands.append('commit')
for command in to_list(commands):
if command == 'end':
continue
cmd = {'command': command, 'prompt': WARNING_PROMPTS_RE, 'answer': 'yes'}
rc, out, err = exec_command(module, module.jsonify(cmd))
if rc != 0:
module.fail_json(msg=err, command=command, rc=rc)
exec_command(module, 'end')
def get_sublevel_config(running_config, module): def get_sublevel_config(running_config, module):
contents = list() contents = list()
current_config_contents = list() current_config_contents = list()
running_config = NetworkConfig(contents=running_config, indent=1)
obj = running_config.get_object(module.params['parents']) obj = running_config.get_object(module.params['parents'])
if obj: if obj:
contents = obj.children contents = obj.children
@ -61,67 +132,8 @@ def get_sublevel_config(running_config, module):
current_config_contents.append(c.rjust(len(c) + indent, ' ')) current_config_contents.append(c.rjust(len(c) + indent, ' '))
if isinstance(c, ConfigLine): if isinstance(c, ConfigLine):
current_config_contents.append(c.raw) current_config_contents.append(c.raw)
indent = indent + 1 indent = 1
sublevel_config = '\n'.join(current_config_contents) sublevel_config = '\n'.join(current_config_contents)
return sublevel_config return sublevel_config
class Cli(CliBase):
NET_PASSWD_RE = re.compile(r"[\r\n]?password:\s?$", re.I)
CLI_PROMPTS_RE = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
]
CLI_ERRORS_RE = [
re.compile(r"% ?Error"),
re.compile(r"% ?Bad secret"),
re.compile(r"Syntax error:"),
re.compile(r"invalid input", re.I),
re.compile(r"(?:incomplete|ambiguous) command", re.I),
re.compile(r"connection timed out", re.I),
re.compile(r"[^\r\n]+ not found", re.I),
re.compile(r"'[^']' +returned error code: ?\d+"),
]
def connect(self, params, **kwargs):
super(Cli, self).connect(params, kickstart=False, **kwargs)
self.shell.send('terminal length 0')
def configure(self, commands, **kwargs):
cmds = ['configure terminal']
cmds.extend(to_list(commands))
cmds.append('end')
cmds.append('commit')
responses = self.execute(cmds)
responses.pop(0)
return responses
def get_config(self, **kwargs):
return self.execute(['show running-configuration'])
def load_config(self, commands, **kwargs):
return self.configure(commands)
def commit_config(self, **kwargs):
self.execute(['commit'])
def abort_config(self, **kwargs):
self.execute(['discard'])
def save_config(self):
self.execute(['copy running-config startup-config'])
Cli = register_transport('cli', default=True)(Cli)

View file

@ -1,5 +1,6 @@
# #
# (c) 2015 Peter Sprygada, <psprygada@ansible.com> # (c) 2015 Peter Sprygada, <psprygada@ansible.com>
# (c) 2017 Red Hat, Inc
# #
# Copyright (c) 2016 Dell Inc. # Copyright (c) 2016 Dell Inc.
# #
@ -28,28 +29,97 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
import re import re
from ansible.module_utils.shell import CliBase from ansible.module_utils.basic import env_fallback
from ansible.module_utils.network import register_transport, to_list, Command from ansible.module_utils.network_common import to_list, ComplexList
from ansible.module_utils.netcfg import NetworkConfig, ConfigLine from ansible.module_utils.connection import exec_command
from ansible.module_utils.netcfg import NetworkConfig,ConfigLine
_DEVICE_CONFIGS = {}
WARNING_PROMPTS_RE = [
r"[\r\n]?\[confirm yes/no\]:\s?$",
r"[\r\n]?\[y/n\]:\s?$",
r"[\r\n]?\[yes/no\]:\s?$"
]
dellos9_argument_spec = {
'host': dict(),
'port': dict(type='int'),
'username': dict(fallback=(env_fallback, ['ANSIBLE_NET_USERNAME'])),
'password': dict(fallback=(env_fallback, ['ANSIBLE_NET_PASSWORD']), no_log=True),
'ssh_keyfile': dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'),
'authorize': dict(fallback=(env_fallback, ['ANSIBLE_NET_AUTHORIZE']), type='bool'),
'auth_pass': dict(fallback=(env_fallback, ['ANSIBLE_NET_AUTH_PASS']), no_log=True),
'timeout': dict(type='int'),
'provider': dict(type='dict'),
}
def check_args(module, warnings):
provider = module.params['provider'] or {}
for key in dellos9_argument_spec:
if key != 'provider' and module.params[key]:
warnings.append('argument %s has been deprecated and will be '
'removed in a future version' % key)
def get_config(module): def get_config(module, flags=[]):
contents = module.params['config'] cmd = 'show running-config '
cmd += ' '.join(flags)
cmd = cmd.strip()
if not contents: try:
contents = module.config.get_config() return _DEVICE_CONFIGS[cmd]
module.params['config'] = contents except KeyError:
return NetworkConfig(indent=1, contents=contents[0]) rc, out, err = exec_command(module, cmd)
else: if rc != 0:
return NetworkConfig(indent=1, contents=contents) module.fail_json(msg='unable to retrieve current config', stderr=err)
cfg = str(out).strip()
_DEVICE_CONFIGS[cmd] = cfg
return cfg
def to_commands(module, commands):
spec = {
'command': dict(key=True),
'prompt': dict(),
'answer': dict()
}
transform = ComplexList(spec, module)
return transform(commands)
def run_commands(module, commands, check_rc=True):
responses = list()
commands = to_commands(module, to_list(commands))
for cmd in commands:
cmd = module.jsonify(cmd)
rc, out, err = exec_command(module, cmd)
if check_rc and rc != 0:
module.fail_json(msg=err, rc=rc)
responses.append(out)
return responses
def load_config(module, commands):
rc, out, err = exec_command(module, 'configure terminal')
if rc != 0:
module.fail_json(msg='unable to enter configuration mode', err=err)
for command in to_list(commands):
if command == 'end':
continue
cmd = {'command': command, 'prompt': WARNING_PROMPTS_RE, 'answer': 'yes'}
rc, out, err = exec_command(module, module.jsonify(cmd))
if rc != 0:
module.fail_json(msg=err, command=command, rc=rc)
exec_command(module, 'end')
def get_sublevel_config(running_config, module): def get_sublevel_config(running_config, module):
contents = list() contents = list()
current_config_contents = list() current_config_contents = list()
running_config = NetworkConfig(contents=running_config, indent=1)
obj = running_config.get_object(module.params['parents']) obj = running_config.get_object(module.params['parents'])
if obj: if obj:
contents = obj.children contents = obj.children
@ -61,75 +131,8 @@ def get_sublevel_config(running_config, module):
current_config_contents.append(c.rjust(len(c) + indent, ' ')) current_config_contents.append(c.rjust(len(c) + indent, ' '))
if isinstance(c, ConfigLine): if isinstance(c, ConfigLine):
current_config_contents.append(c.raw) current_config_contents.append(c.raw)
indent = indent + 1 indent = 1
sublevel_config = '\n'.join(current_config_contents) sublevel_config = '\n'.join(current_config_contents)
return sublevel_config return sublevel_config
class Cli(CliBase):
NET_PASSWD_RE = re.compile(r"[\r\n]?password:\s?$", re.I)
WARNING_PROMPTS_RE = [
re.compile(r"[\r\n]?\[confirm yes/no\]:\s?$"),
re.compile(r"[\r\n]?\[y/n\]:\s?$"),
re.compile(r"[\r\n]?\[yes/no\]:\s?$")
]
CLI_PROMPTS_RE = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
]
CLI_ERRORS_RE = [
re.compile(r"% ?Error: (?:(?!\bdoes not exist\b)(?!\balready exists\b)(?!\bHost not found\b)(?!\bnot active\b).)*$"),
re.compile(r"% ?Bad secret"),
re.compile(r"invalid input", re.I),
re.compile(r"(?:incomplete|ambiguous) command", re.I),
re.compile(r"connection timed out", re.I),
re.compile(r"'[^']' +returned error code: ?\d+"),
]
def connect(self, params, **kwargs):
super(Cli, self).connect(params, kickstart=False, **kwargs)
self.shell.send('terminal length 0')
def authorize(self, params, **kwargs):
passwd = params['auth_pass']
self.run_commands(
Command('enable', prompt=self.NET_PASSWD_RE, response=passwd)
)
def configure(self, commands, **kwargs):
cmds = ['configure terminal']
cmdlist = list()
for c in to_list(commands):
cmd = Command(c, prompt=self.WARNING_PROMPTS_RE, response='yes')
cmdlist.append(cmd)
cmds.extend(cmdlist)
cmds.append('end')
responses = self.execute(cmds)
responses.pop(0)
return responses
def get_config(self, **kwargs):
return self.execute(['show running-config'])
def load_config(self, commands, **kwargs):
return self.configure(commands)
def save_config(self):
cmdlist = list()
cmd = 'copy running-config startup-config'
cmdlist.append(Command(cmd, prompt=self.WARNING_PROMPTS_RE, response='yes'))
self.execute(cmdlist)
Cli = register_transport('cli', default=True)(Cli)

View file

@ -2,7 +2,7 @@
# #
# (c) 2015 Peter Sprygada, <psprygada@ansible.com> # (c) 2015 Peter Sprygada, <psprygada@ansible.com>
# #
# Copyright (c) 2016 Dell Inc. # Copyright (c) 2017 Dell Inc.
# #
# This file is part of Ansible # This file is part of Ansible
# #
@ -140,14 +140,14 @@ warnings:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import time
from ansible.module_utils.basic import get_exception from ansible.module_utils.dellos10 import run_commands
from ansible.module_utils.netcli import CommandRunner, FailedConditionsError from ansible.module_utils.dellos10 import dellos10_argument_spec, check_args
from ansible.module_utils.network import NetworkModule, NetworkError from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import string_types from ansible.module_utils.network_common import ComplexList
import ansible.module_utils.dellos10 from ansible.module_utils.netcli import Conditional
VALID_KEYS = ['command', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
@ -155,75 +155,87 @@ def to_lines(stdout):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module):
for cmd in module.params['commands']: def parse_commands(module, warnings):
if isinstance(cmd, string_types): command = ComplexList(dict(
cmd = dict(command=cmd, output=None) command=dict(key=True),
elif 'command' not in cmd: prompt=dict(),
module.fail_json(msg='command keyword argument is required') answer=dict()
elif not set(cmd.keys()).issubset(VALID_KEYS): ), module)
module.fail_json(msg='unknown keyword specified') commands = command(module.params['commands'])
yield cmd for index, item in enumerate(commands):
if module.check_mode and not item['command'].startswith('show'):
warnings.append(
'only show commands are supported when using check mode, not '
'executing `%s`' % item['command']
)
elif item['command'].startswith('conf'):
module.fail_json(
msg='dellos10_command does not support running config mode '
'commands. Please use dellos10_config instead'
)
return commands
def main(): def main():
spec = dict( """main entry point for module execution
"""
argument_spec = dict(
# { command: <str>, prompt: <str>, response: <str> } # { command: <str>, prompt: <str>, response: <str> }
commands=dict(type='list', required=True), commands=dict(type='list', required=True),
wait_for=dict(type='list'),
wait_for=dict(type='list', aliases=['waitfor']),
match=dict(default='all', choices=['all', 'any']),
retries=dict(default=10, type='int'), retries=dict(default=10, type='int'),
interval=dict(default=1, type='int') interval=dict(default=1, type='int')
) )
module = NetworkModule(argument_spec=spec, argument_spec.update(dellos10_argument_spec)
connect_on_load=False,
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True)
commands = list(parse_commands(module)) result = {'changed': False}
conditionals = module.params['wait_for'] or list()
warnings = list() warnings = list()
check_args(module, warnings)
runner = CommandRunner(module) commands = parse_commands(module, warnings)
for cmd in commands:
if module.check_mode and not cmd['command'].startswith('show'):
warnings.append('only show commands are supported when using '
'check mode, not executing `%s`' % cmd)
else:
if cmd['command'].startswith('conf'):
module.fail_json(msg='dellos10_command does not support running '
'config mode commands. Please use '
'dellos10_config instead')
runner.add_command(**cmd)
for item in conditionals:
runner.add_conditional(item)
runner.retries = module.params['retries']
runner.interval = module.params['interval']
try:
runner.run()
except FailedConditionsError:
exc = get_exception()
module.fail_json(msg=str(exc), failed_conditions=exc.failed_conditions)
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc))
result = dict(changed=False)
result['stdout'] = list()
for cmd in commands:
try:
output = runner.get_command(cmd['command'])
except ValueError:
output = 'command not executed due to check_mode, see warnings'
result['stdout'].append(output)
result['warnings'] = warnings result['warnings'] = warnings
result['stdout_lines'] = list(to_lines(result['stdout']))
wait_for = module.params['wait_for'] or list()
conditionals = [Conditional(c) for c in wait_for]
retries = module.params['retries']
interval = module.params['interval']
match = module.params['match']
while retries > 0:
responses = run_commands(module, commands)
for item in list(conditionals):
if item(responses):
if match == 'any':
conditionals = list()
break
conditionals.remove(item)
if not conditionals:
break
time.sleep(interval)
retries -= 1
if conditionals:
failed_conditions = [item.raw for item in conditionals]
msg = 'One or more conditional statements have not be satisfied'
module.fail_json(msg=msg, failed_conditions=failed_conditions)
result = {
'changed': False,
'stdout': responses,
'stdout_lines': list(to_lines(responses))
}
module.exit_json(**result) module.exit_json(**result)

View file

@ -2,7 +2,7 @@
# #
# (c) 2015 Peter Sprygada, <psprygada@ansible.com> # (c) 2015 Peter Sprygada, <psprygada@ansible.com>
# #
# Copyright (c) 2016 Dell Inc. # Copyright (c) 2017 Dell Inc.
# #
# This file is part of Ansible # This file is part of Ansible
# #
@ -195,9 +195,13 @@ saved:
sample: True sample: True
""" """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import NetworkConfig, dumps from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.network import NetworkModule
from ansible.module_utils.dellos10 import get_config, get_sublevel_config from ansible.module_utils.dellos10 import get_config, get_sublevel_config
from ansible.module_utils.dellos10 import dellos10_argument_spec, check_args
from ansible.module_utils.dellos10 import load_config, run_commands
from ansible.module_utils.dellos10 import WARNING_PROMPTS_RE
def get_candidate(module): def get_candidate(module):
candidate = NetworkConfig(indent=1) candidate = NetworkConfig(indent=1)
@ -223,16 +227,18 @@ def main():
match=dict(default='line', match=dict(default='line',
choices=['line', 'strict', 'exact', 'none']), choices=['line', 'strict', 'exact', 'none']),
replace=dict(default='line', choices=['line', 'block']), replace=dict(default='line', choices=['line', 'block']),
update=dict(choices=['merge', 'check'], default='merge'), update=dict(choices=['merge', 'check'], default='merge'),
save=dict(type='bool', default=False), save=dict(type='bool', default=False),
config=dict(), config=dict(),
backup=dict(type='bool', default=False) backup=dict(type='bool', default=False)
) )
argument_spec.update(dellos10_argument_spec)
mutually_exclusive = [('lines', 'src')] mutually_exclusive = [('lines', 'src')]
module = NetworkModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
connect_on_load=False,
mutually_exclusive=mutually_exclusive, mutually_exclusive=mutually_exclusive,
supports_check_mode=True) supports_check_mode=True)
@ -240,24 +246,30 @@ def main():
match = module.params['match'] match = module.params['match']
replace = module.params['replace'] replace = module.params['replace']
result = dict(changed=False, saved=False)
warnings = list()
check_args(module, warnings)
result = dict(changed=False, saved=False, warnings=warnings)
candidate = get_candidate(module) candidate = get_candidate(module)
if match != 'none': if match != 'none':
config = get_config(module) config = get_config(module)
if parents: if parents:
contents = get_sublevel_config(config, module) contents = get_sublevel_config(config, module)
config = NetworkConfig(contents=contents, indent=1) config = NetworkConfig(contents=contents, indent=1)
else:
config = NetworkConfig(contents=config, indent=1)
configobjs = candidate.difference(config, match=match, replace=replace) configobjs = candidate.difference(config, match=match, replace=replace)
else: else:
configobjs = candidate.items configobjs = candidate.items
if module.params['backup']: if module.params['backup']:
result['__backup__'] = module.cli('show running-config')[0] result['__backup__'] = get_config(module)
commands = list() commands = list()
if configobjs: if configobjs:
commands = dumps(configobjs, 'commands') commands = dumps(configobjs, 'commands')
commands = commands.split('\n') commands = commands.split('\n')
@ -269,11 +281,11 @@ def main():
commands.extend(module.params['after']) commands.extend(module.params['after'])
if not module.check_mode and module.params['update'] == 'merge': if not module.check_mode and module.params['update'] == 'merge':
response = module.config.load_config(commands) load_config(module, commands)
result['responses'] = response
if module.params['save']: if module.params['save']:
module.config.save_config() cmd = {'command': 'copy runing-config startup-config', 'prompt': WARNING_PROMPTS_RE, 'answer': 'yes'}
run_commands(module, [cmd])
result['saved'] = True result['saved'] = True
result['changed'] = True result['changed'] = True

View file

@ -2,7 +2,7 @@
# #
# (c) 2015 Peter Sprygada, <psprygada@ansible.com> # (c) 2015 Peter Sprygada, <psprygada@ansible.com>
# #
# Copyright (c) 2016 Dell Inc. # Copyright (c) 2017 Dell Inc.
# #
# This file is part of Ansible # This file is part of Ansible
# #
@ -134,47 +134,57 @@ ansible_net_neighbors:
import re import re
from ansible.module_utils.basic import get_exception from ansible.module_utils.dellos10 import run_commands
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.dellos10 import dellos10_argument_spec, check_args
from ansible.module_utils.network import NetworkModule from ansible.module_utils.basic import AnsibleModule
import ansible.module_utils.dellos10 from ansible.module_utils.six import iteritems
from ansible.module_utils.six.moves import zip
try: try:
from lxml import etree as ET from lxml import etree as ET
except ImportError: except ImportError:
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
class FactsBase(object): class FactsBase(object):
def __init__(self, runner): COMMANDS = list()
self.runner = runner
self.facts = dict()
self.commands() def __init__(self, module):
self.module = module
self.facts = dict()
self.responses = None
def populate(self):
self.responses = run_commands(self.module, self.COMMANDS, check_rc=False)
def run(self, cmd):
return run_commands(self.module, cmd, check_rc=False)
class Default(FactsBase): class Default(FactsBase):
def commands(self): COMMANDS = [
self.runner.add_command('show version | display-xml') 'show version | display-xml',
self.runner.add_command('show system | display-xml') 'show system | display-xml',
self.runner.add_command('show running-configuration | grep hostname') 'show running-configuration | grep hostname'
]
def populate(self): def populate(self):
super(Default, self).populate()
data = self.runner.get_command('show version | display-xml') data = self.responses[0]
xml_data = ET.fromstring(data) xml_data = ET.fromstring(data)
self.facts['name'] = self.parse_name(xml_data) self.facts['name'] = self.parse_name(xml_data)
self.facts['version'] = self.parse_version(xml_data) self.facts['version'] = self.parse_version(xml_data)
data = self.runner.get_command('show system | display-xml') data = self.responses[1]
xml_data = ET.fromstring(data) xml_data = ET.fromstring(data)
self.facts['servicetag'] = self.parse_serialnum(xml_data) self.facts['servicetag'] = self.parse_serialnum(xml_data)
self.facts['model'] = self.parse_model(xml_data) self.facts['model'] = self.parse_model(xml_data)
data = self.runner.get_command('show running-configuration | grep hostname') data = self.responses[2]
self.facts['hostname'] = self.parse_hostname(data) self.facts['hostname'] = self.parse_hostname(data)
def parse_name(self, data): def parse_name(self, data):
@ -213,18 +223,21 @@ class Default(FactsBase):
class Hardware(FactsBase): class Hardware(FactsBase):
def commands(self): COMMANDS = [
self.runner.add_command('show processes memory | grep Total') 'show version | display-xml',
'show processes memory | grep Total'
]
def populate(self): def populate(self):
data = self.runner.get_command('show version | display-xml') super(Hardware, self).populate()
data = self.responses[0]
xml_data = ET.fromstring(data) xml_data = ET.fromstring(data)
self.facts['cpu_arch'] = self.parse_cpu_arch(xml_data) self.facts['cpu_arch'] = self.parse_cpu_arch(xml_data)
data = self.runner.get_command('show processes memory | grep Total') data = self.responses[1]
match = self.parse_memory(data) match = self.parse_memory(data)
if match: if match:
self.facts['memtotal_mb'] = int(match[0]) / 1024 self.facts['memtotal_mb'] = int(match[0]) / 1024
@ -243,24 +256,25 @@ class Hardware(FactsBase):
class Config(FactsBase): class Config(FactsBase):
def commands(self): COMMANDS = ['show running-config']
self.runner.add_command('show running-config')
def populate(self): def populate(self):
config = self.runner.get_command('show running-config') super(Config, self).populate()
self.facts['config'] = config self.facts['config'] = self.responses[0]
class Interfaces(FactsBase): class Interfaces(FactsBase):
def commands(self): COMMANDS = [
self.runner.add_command('show interface | display-xml') 'show interface | display-xml',
]
def populate(self): def populate(self):
super(Interfaces, self).populate()
self.facts['all_ipv4_addresses'] = list() self.facts['all_ipv4_addresses'] = list()
self.facts['all_ipv6_addresses'] = list() self.facts['all_ipv6_addresses'] = list()
data = self.runner.get_command('show interface | display-xml') data = self.responses[0]
xml_data = ET.fromstring(data) xml_data = ET.fromstring(data)
@ -294,21 +308,23 @@ class Interfaces(FactsBase):
for interface in interfaces.findall('./data/ports/ports-state/port'): for interface in interfaces.findall('./data/ports/ports-state/port'):
name = self.parse_item(interface, 'name') name = self.parse_item(interface, 'name')
fanout = self.parse_item(interface, 'fanout-state') # media-type name interface name format phy-eth 1/1/1
mediatype = self.parse_item(interface, 'media-type') mediatype = self.parse_item(interface, 'media-type')
typ, sname = name.split('-eth') typ, sname = name.split('-eth')
name = "ethernet" + sname
if fanout == "BREAKOUT_1x1": try:
name = "ethernet" + sname
intf = int_facts[name] intf = int_facts[name]
intf['mediatype'] = mediatype intf['mediatype'] = mediatype
else: except:
# TODO: Loop for the exact subport # fanout
for subport in xrange(1, 5): for subport in xrange(1, 5):
name = "ethernet" + sname + ":" + str(subport) name = "ethernet" + sname + ":" + str(subport)
intf = int_facts[name] try:
intf['mediatype'] = mediatype intf = int_facts[name]
intf['mediatype'] = mediatype
except:
pass
return int_facts return int_facts
@ -329,7 +345,7 @@ class Interfaces(FactsBase):
ipv4 = interface.find('ipv4') ipv4 = interface.find('ipv4')
ip_address = "" ip_address = ""
if ipv4 is not None: if ipv4 is not None:
prim_ipaddr = ipv4.find('./address/primary-addr') prim_ipaddr = ipv4.find('./address/primary-addr')
if prim_ipaddr is not None: if prim_ipaddr is not None:
ip_address = prim_ipaddr.text ip_address = prim_ipaddr.text
self.add_ip_address(ip_address, 'ipv4') self.add_ip_address(ip_address, 'ipv4')
@ -340,7 +356,7 @@ class Interfaces(FactsBase):
ipv4 = interface.find('ipv4') ipv4 = interface.find('ipv4')
ip_address = "" ip_address = ""
if ipv4 is not None: if ipv4 is not None:
sec_ipaddr = ipv4.find('./address/secondary-addr') sec_ipaddr = ipv4.find('./address/secondary-addr')
if sec_ipaddr is not None: if sec_ipaddr is not None:
ip_address = sec_ipaddr.text ip_address = sec_ipaddr.text
self.add_ip_address(ip_address, 'ipv4') self.add_ip_address(ip_address, 'ipv4')
@ -351,7 +367,7 @@ class Interfaces(FactsBase):
ipv6 = interface.find('ipv6') ipv6 = interface.find('ipv6')
ip_address = "" ip_address = ""
if ipv6 is not None: if ipv6 is not None:
ipv6_addr = ipv6.find('./address/ipv6-address') ipv6_addr = ipv6.find('./address/ipv6-address')
if ipv6_addr is not None: if ipv6_addr is not None:
ip_address = ipv6_addr.text ip_address = ipv6_addr.text
self.add_ip_address(ip_address, 'ipv6') self.add_ip_address(ip_address, 'ipv6')
@ -384,11 +400,16 @@ VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
def main(): def main():
spec = dict( """main entry point for module execution
"""
argument_spec = dict(
gather_subset=dict(default=['!config'], type='list') gather_subset=dict(default=['!config'], type='list')
) )
module = NetworkModule(argument_spec=spec, supports_check_mode=True) argument_spec.update(dellos10_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
gather_subset = module.params['gather_subset'] gather_subset = module.params['gather_subset']
@ -426,28 +447,23 @@ def main():
facts = dict() facts = dict()
facts['gather_subset'] = list(runable_subsets) facts['gather_subset'] = list(runable_subsets)
runner = CommandRunner(module)
instances = list() instances = list()
for key in runable_subsets: for key in runable_subsets:
runs = FACT_SUBSETS[key](runner) instances.append(FACT_SUBSETS[key](module))
instances.append(runs)
runner.run() for inst in instances:
inst.populate()
try: facts.update(inst.facts)
for inst in instances:
inst.populate()
facts.update(inst.facts)
except Exception:
module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict() ansible_facts = dict()
for key, value in facts.items(): for key, value in iteritems(facts):
key = 'ansible_net_%s' % key key = 'ansible_net_%s' % key
ansible_facts[key] = value ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts) warnings = list()
check_args(module, warnings)
module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -20,10 +20,9 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
ANSIBLE_METADATA = {'metadata_version': '1.0', ANSIBLE_METADATA = {'status': ['preview'],
'status': ['preview'], 'supported_by': 'community',
'supported_by': 'community'} 'metadata_version': '1.0'}
DOCUMENTATION = """ DOCUMENTATION = """
--- ---
@ -149,14 +148,14 @@ warnings:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import time
from ansible.module_utils.basic import get_exception from ansible.module_utils.dellos9 import run_commands
from ansible.module_utils.netcli import CommandRunner, FailedConditionsError from ansible.module_utils.dellos9 import dellos9_argument_spec, check_args
from ansible.module_utils.network import NetworkModule, NetworkError from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import string_types from ansible.module_utils.network_common import ComplexList
import ansible.module_utils.dellos9 from ansible.module_utils.netcli import Conditional
VALID_KEYS = ['command', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
@ -164,74 +163,87 @@ def to_lines(stdout):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module):
for cmd in module.params['commands']: def parse_commands(module, warnings):
if isinstance(cmd, string_types): command = ComplexList(dict(
cmd = dict(command=cmd, output=None) command=dict(key=True),
elif 'command' not in cmd: prompt=dict(),
module.fail_json(msg='command keyword argument is required') answer=dict()
elif not set(cmd.keys()).issubset(VALID_KEYS): ), module)
module.fail_json(msg='unknown keyword specified') commands = command(module.params['commands'])
yield cmd for index, item in enumerate(commands):
if module.check_mode and not item['command'].startswith('show'):
warnings.append(
'only show commands are supported when using check mode, not '
'executing `%s`' % item['command']
)
elif item['command'].startswith('conf'):
module.fail_json(
msg='dellos9_command does not support running config mode '
'commands. Please use dellos9_config instead'
)
return commands
def main(): def main():
spec = dict( """main entry point for module execution
"""
argument_spec = dict(
# { command: <str>, prompt: <str>, response: <str> } # { command: <str>, prompt: <str>, response: <str> }
commands=dict(type='list', required=True), commands=dict(type='list', required=True),
wait_for=dict(type='list'),
wait_for=dict(type='list', aliases=['waitfor']),
match=dict(default='all', choices=['all', 'any']),
retries=dict(default=10, type='int'), retries=dict(default=10, type='int'),
interval=dict(default=1, type='int') interval=dict(default=1, type='int')
) )
module = NetworkModule(argument_spec=spec, argument_spec.update(dellos9_argument_spec)
connect_on_load=False,
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True)
commands = list(parse_commands(module)) result = {'changed': False}
conditionals = module.params['wait_for'] or list()
warnings = list() warnings = list()
check_args(module, warnings)
runner = CommandRunner(module) commands = parse_commands(module, warnings)
for cmd in commands:
if module.check_mode and not cmd['command'].startswith('show'):
warnings.append('only show commands are supported when using '
'check mode, not executing `%s`' % cmd)
else:
if cmd['command'].startswith('conf'):
module.fail_json(msg='dellos9_command does not support running '
'config mode commands. Please use '
'dellos9_config instead')
runner.add_command(**cmd)
for item in conditionals:
runner.add_conditional(item)
runner.retries = module.params['retries']
runner.interval = module.params['interval']
try:
runner.run()
except FailedConditionsError:
exc = get_exception()
module.fail_json(msg=str(exc), failed_conditions=exc.failed_conditions)
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc))
result = dict(changed=False)
result['stdout'] = list()
for cmd in commands:
try:
output = runner.get_command(cmd['command'])
except ValueError:
output = 'command not executed due to check_mode, see warnings'
result['stdout'].append(output)
result['warnings'] = warnings result['warnings'] = warnings
result['stdout_lines'] = list(to_lines(result['stdout']))
wait_for = module.params['wait_for'] or list()
conditionals = [Conditional(c) for c in wait_for]
retries = module.params['retries']
interval = module.params['interval']
match = module.params['match']
while retries > 0:
responses = run_commands(module, commands)
for item in list(conditionals):
if item(responses):
if match == 'any':
conditionals = list()
break
conditionals.remove(item)
if not conditionals:
break
time.sleep(interval)
retries -= 1
if conditionals:
failed_conditions = [item.raw for item in conditionals]
msg = 'One or more conditional statements have not be satisfied'
module.fail_json(msg=msg, failed_conditions=failed_conditions)
result = {
'changed': False,
'stdout': responses,
'stdout_lines': list(to_lines(responses))
}
module.exit_json(**result) module.exit_json(**result)

View file

@ -201,9 +201,12 @@ saved:
sample: True sample: True
""" """
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import NetworkConfig, dumps from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.network import NetworkModule
from ansible.module_utils.dellos9 import get_config, get_sublevel_config from ansible.module_utils.dellos9 import get_config, get_sublevel_config
from ansible.module_utils.dellos9 import dellos9_argument_spec, check_args
from ansible.module_utils.dellos9 import load_config, run_commands
from ansible.module_utils.dellos9 import WARNING_PROMPTS_RE
def get_candidate(module): def get_candidate(module):
@ -237,10 +240,11 @@ def main():
backup=dict(type='bool', default=False) backup=dict(type='bool', default=False)
) )
argument_spec.update(dellos9_argument_spec)
mutually_exclusive = [('lines', 'src')] mutually_exclusive = [('lines', 'src')]
module = NetworkModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
connect_on_load=False,
mutually_exclusive=mutually_exclusive, mutually_exclusive=mutually_exclusive,
supports_check_mode=True) supports_check_mode=True)
@ -248,7 +252,11 @@ def main():
match = module.params['match'] match = module.params['match']
replace = module.params['replace'] replace = module.params['replace']
result = dict(changed=False, saved=False)
warnings = list()
check_args(module, warnings)
result = dict(changed=False, saved=False, warnings=warnings)
candidate = get_candidate(module) candidate = get_candidate(module)
@ -257,15 +265,18 @@ def main():
if parents: if parents:
contents = get_sublevel_config(config, module) contents = get_sublevel_config(config, module)
config = NetworkConfig(contents=contents, indent=1) config = NetworkConfig(contents=contents, indent=1)
else:
config = NetworkConfig(contents=config, indent=1)
configobjs = candidate.difference(config, match=match, replace=replace) configobjs = candidate.difference(config, match=match, replace=replace)
else: else:
configobjs = candidate.items configobjs = candidate.items
if module.params['backup']: if module.params['backup']:
result['__backup__'] = module.cli('show running-config')[0] result['__backup__'] = get_config(module)
commands = list() commands = list()
if configobjs: if configobjs:
commands = dumps(configobjs, 'commands') commands = dumps(configobjs, 'commands')
commands = commands.split('\n') commands = commands.split('\n')
@ -277,11 +288,11 @@ def main():
commands.extend(module.params['after']) commands.extend(module.params['after'])
if not module.check_mode and module.params['update'] == 'merge': if not module.check_mode and module.params['update'] == 'merge':
response = module.config.load_config(commands) load_config(module, commands)
result['responses'] = response
if module.params['save']: if module.params['save']:
module.config.save_config() cmd = {'command': 'copy runing-config startup-config', 'prompt': WARNING_PROMPTS_RE, 'answer': 'yes'}
run_commands(module, [cmd])
result['saved'] = True result['saved'] = True
result['changed'] = True result['changed'] = True

View file

@ -142,37 +142,48 @@ ansible_net_neighbors:
import re import re
import itertools import itertools
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.dellos9 import run_commands
from ansible.module_utils.network import NetworkModule from ansible.module_utils.dellos9 import dellos9_argument_spec, check_args
import ansible.module_utils.dellos9 from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import iteritems
from ansible.module_utils.six.moves import zip
class FactsBase(object): class FactsBase(object):
def __init__(self, runner): COMMANDS = list()
self.runner = runner
self.facts = dict()
self.commands() def __init__(self, module):
self.module = module
self.facts = dict()
self.responses = None
def populate(self):
self.responses = run_commands(self.module, self.COMMANDS, check_rc=False)
def run(self, cmd):
return run_commands(self.module, cmd, check_rc=False)
class Default(FactsBase): class Default(FactsBase):
def commands(self): COMMANDS = [
self.runner.add_command('show version') 'show version',
self.runner.add_command('show inventory') 'show inventory',
self.runner.add_command('show running-config | grep hostname') 'show running-config | grep hostname'
]
def populate(self): def populate(self):
data = self.runner.get_command('show version') super(Default, self).populate()
data = self.responses[0]
self.facts['version'] = self.parse_version(data) self.facts['version'] = self.parse_version(data)
self.facts['model'] = self.parse_model(data) self.facts['model'] = self.parse_model(data)
self.facts['image'] = self.parse_image(data) self.facts['image'] = self.parse_image(data)
data = self.runner.get_command('show inventory') data = self.responses[1]
self.facts['serialnum'] = self.parse_serialnum(data) self.facts['serialnum'] = self.parse_serialnum(data)
data = self.runner.get_command('show running-config | grep hostname') data = self.responses[2]
self.facts['hostname'] = self.parse_hostname(data) self.facts['hostname'] = self.parse_hostname(data)
def parse_version(self, data): def parse_version(self, data):
@ -206,15 +217,17 @@ class Default(FactsBase):
class Hardware(FactsBase): class Hardware(FactsBase):
def commands(self): COMMANDS = [
self.runner.add_command('show file-systems') 'show file-systems',
self.runner.add_command('show memory | except Processor') 'show memory | except Processor'
]
def populate(self): def populate(self):
data = self.runner.get_command('show file-systems') super(Hardware, self).populate()
data = self.responses[0]
self.facts['filesystems'] = self.parse_filesystems(data) self.facts['filesystems'] = self.parse_filesystems(data)
data = self.runner.get_command('show memory | except Processor') data = self.responses[1]
match = re.findall('\s(\d+)\s', data) match = re.findall('\s(\d+)\s', data)
if match: if match:
self.facts['memtotal_mb'] = int(match[0]) / 1024 self.facts['memtotal_mb'] = int(match[0]) / 1024
@ -226,25 +239,28 @@ class Hardware(FactsBase):
class Config(FactsBase): class Config(FactsBase):
def commands(self): COMMANDS = ['show running-config']
self.runner.add_command('show running-config')
def populate(self): def populate(self):
self.facts['config'] = self.runner.get_command('show running-config') super(Config, self).populate()
self.facts['config'] = self.responses[0]
class Interfaces(FactsBase): class Interfaces(FactsBase):
def commands(self): COMMANDS = [
self.runner.add_command('show interfaces') 'show interfaces',
self.runner.add_command('show ipv6 interface') 'show ipv6 interface',
self.runner.add_command('show lldp neighbors detail') 'show lldp neighbors detail',
'show inventory'
]
def populate(self): def populate(self):
super(Interfaces, self).populate()
self.facts['all_ipv4_addresses'] = list() self.facts['all_ipv4_addresses'] = list()
self.facts['all_ipv6_addresses'] = list() self.facts['all_ipv6_addresses'] = list()
data = self.runner.get_command('show interfaces') data = self.responses[0]
interfaces = self.parse_interfaces(data) interfaces = self.parse_interfaces(data)
for key in interfaces.keys(): for key in interfaces.keys():
@ -261,14 +277,14 @@ class Interfaces(FactsBase):
self.facts['interfaces'] = self.populate_interfaces(interfaces) self.facts['interfaces'] = self.populate_interfaces(interfaces)
data = self.runner.get_command('show ipv6 interface') data = self.responses[1]
if len(data) > 0: if len(data) > 0:
data = self.parse_ipv6_interfaces(data) data = self.parse_ipv6_interfaces(data)
self.populate_ipv6_interfaces(data) self.populate_ipv6_interfaces(data)
data = self.runner.get_command('show inventory') data = self.responses[3]
if 'LLDP' in self.get_protocol_list(data): if 'LLDP' in self.get_protocol_list(data):
neighbors = self.runner.get_command('show lldp neighbors detail') neighbors = self.responses[2]
self.facts['neighbors'] = self.parse_neighbors(neighbors) self.facts['neighbors'] = self.parse_neighbors(neighbors)
def get_protocol_list(self, data): def get_protocol_list(self, data):
@ -499,11 +515,16 @@ VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
def main(): def main():
spec = dict( """main entry point for module execution
"""
argument_spec = dict(
gather_subset=dict(default=['!config'], type='list') gather_subset=dict(default=['!config'], type='list')
) )
module = NetworkModule(argument_spec=spec, supports_check_mode=True) argument_spec.update(dellos9_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
gather_subset = module.params['gather_subset'] gather_subset = module.params['gather_subset']
@ -541,28 +562,23 @@ def main():
facts = dict() facts = dict()
facts['gather_subset'] = list(runable_subsets) facts['gather_subset'] = list(runable_subsets)
runner = CommandRunner(module)
instances = list() instances = list()
for key in runable_subsets: for key in runable_subsets:
runs = FACT_SUBSETS[key](runner) instances.append(FACT_SUBSETS[key](module))
instances.append(runs)
runner.run() for inst in instances:
inst.populate()
try: facts.update(inst.facts)
for inst in instances:
inst.populate()
facts.update(inst.facts)
except Exception:
module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict() ansible_facts = dict()
for key, value in facts.items(): for key, value in iteritems(facts):
key = 'ansible_net_%s' % key key = 'ansible_net_%s' % key
ansible_facts[key] = value ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts) warnings = list()
check_args(module, warnings)
module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -0,0 +1,125 @@
#
# (c) 2016 Red Hat Inc.
#
# Copyright (c) 2017 Dell Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import sys
import copy
from ansible.plugins.action.normal import ActionModule as _ActionModule
from ansible.utils.path import unfrackpath
from ansible.plugins import connection_loader
from ansible.module_utils.six import iteritems
from ansible.module_utils.dellos10 import dellos10_argument_spec
from ansible.module_utils.basic import AnsibleFallbackNotFound
from ansible.module_utils._text import to_bytes
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class ActionModule(_ActionModule):
def run(self, tmp=None, task_vars=None):
if self._play_context.connection != 'local':
return dict(
failed=True,
msg='invalid connection specified, expected connection=local, '
'got %s' % self._play_context.connection
)
provider = self.load_provider()
pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
pc.network_os = 'dellos10'
pc.port = provider['port'] or self._play_context.port or 22
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password
pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
pc.timeout = provider['timeout'] or self._play_context.timeout
pc.become = provider['authorize'] or False
pc.become_pass = provider['auth_pass']
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
socket_path = self._get_socket_path(pc)
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
if not os.path.exists(socket_path):
# start the connection if it isn't started
rc, out, err = connection.exec_command('open_shell()')
if not rc == 0:
return {'failed': True, 'msg': 'unable to open shell', 'rc': rc}
else:
# make sure we are in the right cli context which should be
# enable mode and not config module
rc, out, err = connection.exec_command('prompt()')
while str(out).strip().endswith(')#'):
display.debug('wrong context, sending exit to device')
connection.exec_command('exit')
rc, out, err = connection.exec_command('prompt()')
task_vars['ansible_socket'] = socket_path
if self._play_context.become_method == 'enable':
self._play_context.become = False
self._play_context.become_method = None
return super(ActionModule, self).run(tmp, task_vars)
def _get_socket_path(self, play_context):
ssh = connection_loader.get('ssh', class_only=True)
cp = ssh._create_control_path(play_context.remote_addr, play_context.port, play_context.remote_user)
path = unfrackpath("$HOME/.ansible/pc")
return cp % dict(directory=path)
def load_provider(self):
provider = self._task.args.get('provider', {})
for key, value in iteritems(dellos10_argument_spec):
if key != 'provider' and key not in provider:
if key in self._task.args:
provider[key] = self._task.args[key]
elif 'fallback' in value:
provider[key] = self._fallback(value['fallback'])
elif key not in provider:
provider[key] = None
return provider
def _fallback(self, fallback):
strategy = fallback[0]
args = []
kwargs = {}
for item in fallback[1:]:
if isinstance(item, dict):
kwargs = item
else:
args = item
try:
return strategy(*args, **kwargs)
except AnsibleFallbackNotFound:
pass

View file

@ -1,6 +1,8 @@
# #
# Copyright 2015 Peter Sprygada <psprygada@ansible.com> # Copyright 2015 Peter Sprygada <psprygada@ansible.com>
# #
# Copyright (c) 2017 Dell Inc.
#
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
@ -19,10 +21,94 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from ansible.plugins.action import ActionBase import os
from ansible.plugins.action.net_config import ActionModule as NetActionModule import re
import time
import glob
class ActionModule(NetActionModule, ActionBase): from ansible.plugins.action.dellos10 import ActionModule as _ActionModule
pass from ansible.module_utils._text import to_text
from ansible.module_utils.six.moves.urllib.parse import urlsplit
from ansible.utils.vars import merge_hash
PRIVATE_KEYS_RE = re.compile('__.+__')
class ActionModule(_ActionModule):
def run(self, tmp=None, task_vars=None):
if self._task.args.get('src'):
try:
self._handle_template()
except ValueError as exc:
return dict(failed=True, msg=exc.message)
result = super(ActionModule, self).run(tmp, task_vars)
if self._task.args.get('backup') and result.get('__backup__'):
# User requested backup and no error occurred in module.
# NOTE: If there is a parameter error, _backup key may not be in results.
filepath = self._write_backup(task_vars['inventory_hostname'],
result['__backup__'])
result['backup_path'] = filepath
# strip out any keys that have two leading and two trailing
# underscore characters
for key in result.keys():
if PRIVATE_KEYS_RE.match(key):
del result[key]
return result
def _get_working_path(self):
cwd = self._loader.get_basedir()
if self._task._role is not None:
cwd = self._task._role._role_path
return cwd
def _write_backup(self, host, contents):
backup_path = self._get_working_path() + '/backup'
if not os.path.exists(backup_path):
os.mkdir(backup_path)
for fn in glob.glob('%s/%s*' % (backup_path, host)):
os.remove(fn)
tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time()))
filename = '%s/%s_config.%s' % (backup_path, host, tstamp)
open(filename, 'w').write(contents)
return filename
def _handle_template(self):
src = self._task.args.get('src')
working_path = self._get_working_path()
if os.path.isabs(src) or urlsplit('src').scheme:
source = src
else:
source = self._loader.path_dwim_relative(working_path, 'templates', src)
if not source:
source = self._loader.path_dwim_relative(working_path, src)
if not os.path.exists(source):
raise ValueError('path specified in src not found')
try:
with open(source, 'r') as f:
template_data = to_text(f.read())
except IOError:
return dict(failed=True, msg='unable to load src file')
# Create a template search path in the following order:
# [working_path, self_role_path, dependent_role_paths, dirname(source)]
searchpath = [working_path]
if self._task._role is not None:
searchpath.append(self._task._role._role_path)
if hasattr(self._task, "_block:"):
dep_chain = self._task._block.get_dep_chain()
if dep_chain is not None:
for role in dep_chain:
searchpath.append(role._role_path)
searchpath.append(os.path.dirname(source))
self._templar.environment.loader.searchpath = searchpath
self._task.args['src'] = self._templar.template(template_data)

View file

@ -1,6 +1,8 @@
# #
# Copyright 2015 Peter Sprygada <psprygada@ansible.com> # Copyright 2015 Peter Sprygada <psprygada@ansible.com>
# #
# Copyright (c) 2017 Dell Inc.
#
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify

View file

@ -0,0 +1,125 @@
#
# (c) 2016 Red Hat Inc.
#
# Copyright (c) 2017 Dell Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import sys
import copy
from ansible.plugins.action.normal import ActionModule as _ActionModule
from ansible.utils.path import unfrackpath
from ansible.plugins import connection_loader
from ansible.module_utils.six import iteritems
from ansible.module_utils.dellos9 import dellos9_argument_spec
from ansible.module_utils.basic import AnsibleFallbackNotFound
from ansible.module_utils._text import to_bytes
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class ActionModule(_ActionModule):
def run(self, tmp=None, task_vars=None):
if self._play_context.connection != 'local':
return dict(
failed=True,
msg='invalid connection specified, expected connection=local, '
'got %s' % self._play_context.connection
)
provider = self.load_provider()
pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
pc.network_os = 'dellos9'
pc.port = provider['port'] or self._play_context.port or 22
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password
pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
pc.timeout = provider['timeout'] or self._play_context.timeout
pc.become = provider['authorize'] or False
pc.become_pass = provider['auth_pass']
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
socket_path = self._get_socket_path(pc)
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
if not os.path.exists(socket_path):
# start the connection if it isn't started
rc, out, err = connection.exec_command('open_shell()')
if not rc == 0:
return {'failed': True, 'msg': 'unable to open shell', 'rc': rc}
else:
# make sure we are in the right cli context which should be
# enable mode and not config module
rc, out, err = connection.exec_command('prompt()')
while str(out).strip().endswith(')#'):
display.debug('wrong context, sending exit to device')
connection.exec_command('exit')
rc, out, err = connection.exec_command('prompt()')
task_vars['ansible_socket'] = socket_path
if self._play_context.become_method == 'enable':
self._play_context.become = False
self._play_context.become_method = None
return super(ActionModule, self).run(tmp, task_vars)
def _get_socket_path(self, play_context):
ssh = connection_loader.get('ssh', class_only=True)
cp = ssh._create_control_path(play_context.remote_addr, play_context.port, play_context.remote_user)
path = unfrackpath("$HOME/.ansible/pc")
return cp % dict(directory=path)
def load_provider(self):
provider = self._task.args.get('provider', {})
for key, value in iteritems(dellos9_argument_spec):
if key != 'provider' and key not in provider:
if key in self._task.args:
provider[key] = self._task.args[key]
elif 'fallback' in value:
provider[key] = self._fallback(value['fallback'])
elif key not in provider:
provider[key] = None
return provider
def _fallback(self, fallback):
strategy = fallback[0]
args = []
kwargs = {}
for item in fallback[1:]:
if isinstance(item, dict):
kwargs = item
else:
args = item
try:
return strategy(*args, **kwargs)
except AnsibleFallbackNotFound:
pass

View file

@ -1,6 +1,8 @@
# #
# Copyright 2015 Peter Sprygada <psprygada@ansible.com> # Copyright 2015 Peter Sprygada <psprygada@ansible.com>
# #
# Copyright (c) 2017 Dell Inc.
#
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
@ -19,10 +21,94 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from ansible.plugins.action import ActionBase import os
from ansible.plugins.action.net_config import ActionModule as NetActionModule import re
import time
import glob
class ActionModule(NetActionModule, ActionBase): from ansible.plugins.action.dellos9 import ActionModule as _ActionModule
pass from ansible.module_utils._text import to_text
from ansible.module_utils.six.moves.urllib.parse import urlsplit
from ansible.utils.vars import merge_hash
PRIVATE_KEYS_RE = re.compile('__.+__')
class ActionModule(_ActionModule):
def run(self, tmp=None, task_vars=None):
if self._task.args.get('src'):
try:
self._handle_template()
except ValueError as exc:
return dict(failed=True, msg=exc.message)
result = super(ActionModule, self).run(tmp, task_vars)
if self._task.args.get('backup') and result.get('__backup__'):
# User requested backup and no error occurred in module.
# NOTE: If there is a parameter error, _backup key may not be in results.
filepath = self._write_backup(task_vars['inventory_hostname'],
result['__backup__'])
result['backup_path'] = filepath
# strip out any keys that have two leading and two trailing
# underscore characters
for key in result.keys():
if PRIVATE_KEYS_RE.match(key):
del result[key]
return result
def _get_working_path(self):
cwd = self._loader.get_basedir()
if self._task._role is not None:
cwd = self._task._role._role_path
return cwd
def _write_backup(self, host, contents):
backup_path = self._get_working_path() + '/backup'
if not os.path.exists(backup_path):
os.mkdir(backup_path)
for fn in glob.glob('%s/%s*' % (backup_path, host)):
os.remove(fn)
tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time()))
filename = '%s/%s_config.%s' % (backup_path, host, tstamp)
open(filename, 'w').write(contents)
return filename
def _handle_template(self):
src = self._task.args.get('src')
working_path = self._get_working_path()
if os.path.isabs(src) or urlsplit('src').scheme:
source = src
else:
source = self._loader.path_dwim_relative(working_path, 'templates', src)
if not source:
source = self._loader.path_dwim_relative(working_path, src)
if not os.path.exists(source):
raise ValueError('path specified in src not found')
try:
with open(source, 'r') as f:
template_data = to_text(f.read())
except IOError:
return dict(failed=True, msg='unable to load src file')
# Create a template search path in the following order:
# [working_path, self_role_path, dependent_role_paths, dirname(source)]
searchpath = [working_path]
if self._task._role is not None:
searchpath.append(self._task._role._role_path)
if hasattr(self._task, "_block:"):
dep_chain = self._task._block.get_dep_chain()
if dep_chain is not None:
for role in dep_chain:
searchpath.append(role._role_path)
searchpath.append(os.path.dirname(source))
self._templar.environment.loader.searchpath = searchpath
self._task.args['src'] = self._templar.template(template_data)

View file

@ -0,0 +1,78 @@
#
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Copyright (c) 2017 Dell Inc.
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import re
import json
from ansible.plugins.terminal import TerminalBase
from ansible.errors import AnsibleConnectionFailure
class TerminalModule(TerminalBase):
terminal_stdout_re = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
]
terminal_stderr_re = [
re.compile(r"% ?Error: (?:(?!\bdoes not exist\b)(?!\balready exists\b)(?!\bHost not found\b)(?!\bnot active\b).)*$"),
re.compile(r"% ?Bad secret"),
re.compile(r"invalid input", re.I),
re.compile(r"(?:incomplete|ambiguous) command", re.I),
re.compile(r"connection timed out", re.I),
re.compile(r"'[^']' +returned error code: ?\d+"),
]
def on_open_shell(self):
try:
self._exec_cli_command('terminal length 0')
except AnsibleConnectionFailure:
raise AnsibleConnectionFailure('unable to set terminal parameters')
def on_authorize(self, passwd=None):
if self._get_prompt().endswith('#'):
return
cmd = {'command': 'enable'}
if passwd:
cmd['prompt'] = r"[\r\n]?password: $"
cmd['answer'] = passwd
try:
self._exec_cli_command(json.dumps(cmd))
except AnsibleConnectionFailure:
raise AnsibleConnectionFailure('unable to elevate privilege to enable mode')
def on_deauthorize(self):
prompt = self._get_prompt()
if prompt is None:
# if prompt is None most likely the terminal is hung up at a prompt
return
if prompt.strip().endswith(')#'):
self._exec_cli_command('end')
self._exec_cli_command('disable')
elif prompt.endswith('#'):
self._exec_cli_command('disable')

View file

@ -0,0 +1,78 @@
#
# (c) 2016 Red Hat Inc.
#
# Copyright (c) 2017 Dell Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import re
import json
from ansible.plugins.terminal import TerminalBase
from ansible.errors import AnsibleConnectionFailure
class TerminalModule(TerminalBase):
terminal_stdout_re = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
]
terminal_stderr_re = [
re.compile(r"% ?Error: (?:(?!\bdoes not exist\b)(?!\balready exists\b)(?!\bHost not found\b)(?!\bnot active\b).)*$"),
re.compile(r"% ?Bad secret"),
re.compile(r"invalid input", re.I),
re.compile(r"(?:incomplete|ambiguous) command", re.I),
re.compile(r"connection timed out", re.I),
re.compile(r"'[^']' +returned error code: ?\d+"),
]
def on_open_shell(self):
try:
self._exec_cli_command('terminal length 0')
except AnsibleConnectionFailure:
raise AnsibleConnectionFailure('unable to set terminal parameters')
def on_authorize(self, passwd=None):
if self._get_prompt().endswith('#'):
return
cmd = {'command': 'enable'}
if passwd:
cmd['prompt'] = r"[\r\n]?password: $"
cmd['answer'] = passwd
try:
self._exec_cli_command(json.dumps(cmd))
except AnsibleConnectionFailure:
raise AnsibleConnectionFailure('unable to elevate privilege to enable mode')
def on_deauthorize(self):
prompt = self._get_prompt()
if prompt is None:
# if prompt is None most likely the terminal is hung up at a prompt
return
if prompt.strip().endswith(')#'):
self._exec_cli_command('end')
self._exec_cli_command('disable')
elif prompt.endswith('#'):
self._exec_cli_command('disable')

View file

@ -512,12 +512,8 @@ lib/ansible/modules/network/cumulus/_cl_img_install.py
lib/ansible/modules/network/cumulus/_cl_license.py lib/ansible/modules/network/cumulus/_cl_license.py
lib/ansible/modules/network/cumulus/_cl_ports.py lib/ansible/modules/network/cumulus/_cl_ports.py
lib/ansible/modules/network/cumulus/nclu.py lib/ansible/modules/network/cumulus/nclu.py
lib/ansible/modules/network/dellos10/dellos10_command.py
lib/ansible/modules/network/dellos10/dellos10_config.py
lib/ansible/modules/network/dellos10/dellos10_facts.py
lib/ansible/modules/network/dellos6/dellos6_command.py lib/ansible/modules/network/dellos6/dellos6_command.py
lib/ansible/modules/network/dellos6/dellos6_facts.py lib/ansible/modules/network/dellos6/dellos6_facts.py
lib/ansible/modules/network/dellos9/dellos9_command.py
lib/ansible/modules/network/dnsimple.py lib/ansible/modules/network/dnsimple.py
lib/ansible/modules/network/dnsmadeeasy.py lib/ansible/modules/network/dnsmadeeasy.py
lib/ansible/modules/network/eos/_eos_template.py lib/ansible/modules/network/eos/_eos_template.py
@ -882,9 +878,7 @@ lib/ansible/plugins/action/asa_config.py
lib/ansible/plugins/action/asa_template.py lib/ansible/plugins/action/asa_template.py
lib/ansible/plugins/action/assemble.py lib/ansible/plugins/action/assemble.py
lib/ansible/plugins/action/copy.py lib/ansible/plugins/action/copy.py
lib/ansible/plugins/action/dellos10_config.py
lib/ansible/plugins/action/dellos6_config.py lib/ansible/plugins/action/dellos6_config.py
lib/ansible/plugins/action/dellos9_config.py
lib/ansible/plugins/action/eos_template.py lib/ansible/plugins/action/eos_template.py
lib/ansible/plugins/action/fetch.py lib/ansible/plugins/action/fetch.py
lib/ansible/plugins/action/group_by.py lib/ansible/plugins/action/group_by.py