mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
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:
parent
f9b75d44e8
commit
dd63dfcf1e
17 changed files with 1083 additions and 416 deletions
|
@ -2,7 +2,7 @@
|
|||
#
|
||||
# (c) 2015 Peter Sprygada, <psprygada@ansible.com>
|
||||
#
|
||||
# Copyright (c) 2016 Dell Inc.
|
||||
# Copyright (c) 2017 Dell Inc.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
|
@ -140,14 +140,14 @@ warnings:
|
|||
type: list
|
||||
sample: ['...', '...']
|
||||
"""
|
||||
import time
|
||||
|
||||
from ansible.module_utils.basic import get_exception
|
||||
from ansible.module_utils.netcli import CommandRunner, FailedConditionsError
|
||||
from ansible.module_utils.network import NetworkModule, NetworkError
|
||||
from ansible.module_utils.six import string_types
|
||||
import ansible.module_utils.dellos10
|
||||
from ansible.module_utils.dellos10 import run_commands
|
||||
from ansible.module_utils.dellos10 import dellos10_argument_spec, check_args
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network_common import ComplexList
|
||||
from ansible.module_utils.netcli import Conditional
|
||||
|
||||
VALID_KEYS = ['command', 'prompt', 'response']
|
||||
|
||||
def to_lines(stdout):
|
||||
for item in stdout:
|
||||
|
@ -155,75 +155,87 @@ def to_lines(stdout):
|
|||
item = str(item).split('\n')
|
||||
yield item
|
||||
|
||||
def parse_commands(module):
|
||||
for cmd in module.params['commands']:
|
||||
if isinstance(cmd, string_types):
|
||||
cmd = dict(command=cmd, output=None)
|
||||
elif 'command' not in cmd:
|
||||
module.fail_json(msg='command keyword argument is required')
|
||||
elif not set(cmd.keys()).issubset(VALID_KEYS):
|
||||
module.fail_json(msg='unknown keyword specified')
|
||||
yield cmd
|
||||
|
||||
def parse_commands(module, warnings):
|
||||
command = ComplexList(dict(
|
||||
command=dict(key=True),
|
||||
prompt=dict(),
|
||||
answer=dict()
|
||||
), module)
|
||||
commands = command(module.params['commands'])
|
||||
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():
|
||||
spec = dict(
|
||||
"""main entry point for module execution
|
||||
"""
|
||||
argument_spec = dict(
|
||||
# { command: <str>, prompt: <str>, response: <str> }
|
||||
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'),
|
||||
interval=dict(default=1, type='int')
|
||||
)
|
||||
|
||||
module = NetworkModule(argument_spec=spec,
|
||||
connect_on_load=False,
|
||||
argument_spec.update(dellos10_argument_spec)
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
supports_check_mode=True)
|
||||
|
||||
commands = list(parse_commands(module))
|
||||
conditionals = module.params['wait_for'] or list()
|
||||
result = {'changed': False}
|
||||
|
||||
warnings = list()
|
||||
|
||||
runner = CommandRunner(module)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
check_args(module, warnings)
|
||||
commands = parse_commands(module, 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)
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#
|
||||
# (c) 2015 Peter Sprygada, <psprygada@ansible.com>
|
||||
#
|
||||
# Copyright (c) 2016 Dell Inc.
|
||||
# Copyright (c) 2017 Dell Inc.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
|
@ -195,9 +195,13 @@ saved:
|
|||
sample: True
|
||||
|
||||
"""
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
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 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):
|
||||
candidate = NetworkConfig(indent=1)
|
||||
|
@ -223,16 +227,18 @@ def main():
|
|||
match=dict(default='line',
|
||||
choices=['line', 'strict', 'exact', 'none']),
|
||||
replace=dict(default='line', choices=['line', 'block']),
|
||||
|
||||
update=dict(choices=['merge', 'check'], default='merge'),
|
||||
save=dict(type='bool', default=False),
|
||||
config=dict(),
|
||||
backup=dict(type='bool', default=False)
|
||||
)
|
||||
|
||||
argument_spec.update(dellos10_argument_spec)
|
||||
|
||||
mutually_exclusive = [('lines', 'src')]
|
||||
|
||||
module = NetworkModule(argument_spec=argument_spec,
|
||||
connect_on_load=False,
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
supports_check_mode=True)
|
||||
|
||||
|
@ -240,24 +246,30 @@ def main():
|
|||
|
||||
match = module.params['match']
|
||||
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)
|
||||
|
||||
if match != 'none':
|
||||
config = get_config(module)
|
||||
if parents:
|
||||
contents = get_sublevel_config(config, module)
|
||||
config = NetworkConfig(contents=contents, indent=1)
|
||||
else:
|
||||
config = NetworkConfig(contents=config, indent=1)
|
||||
configobjs = candidate.difference(config, match=match, replace=replace)
|
||||
|
||||
else:
|
||||
configobjs = candidate.items
|
||||
|
||||
if module.params['backup']:
|
||||
result['__backup__'] = module.cli('show running-config')[0]
|
||||
result['__backup__'] = get_config(module)
|
||||
|
||||
commands = list()
|
||||
|
||||
if configobjs:
|
||||
commands = dumps(configobjs, 'commands')
|
||||
commands = commands.split('\n')
|
||||
|
@ -269,11 +281,11 @@ def main():
|
|||
commands.extend(module.params['after'])
|
||||
|
||||
if not module.check_mode and module.params['update'] == 'merge':
|
||||
response = module.config.load_config(commands)
|
||||
result['responses'] = response
|
||||
load_config(module, commands)
|
||||
|
||||
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['changed'] = True
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#
|
||||
# (c) 2015 Peter Sprygada, <psprygada@ansible.com>
|
||||
#
|
||||
# Copyright (c) 2016 Dell Inc.
|
||||
# Copyright (c) 2017 Dell Inc.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
|
@ -134,47 +134,57 @@ ansible_net_neighbors:
|
|||
|
||||
import re
|
||||
|
||||
from ansible.module_utils.basic import get_exception
|
||||
from ansible.module_utils.netcli import CommandRunner
|
||||
from ansible.module_utils.network import NetworkModule
|
||||
import ansible.module_utils.dellos10
|
||||
from ansible.module_utils.dellos10 import run_commands
|
||||
from ansible.module_utils.dellos10 import dellos10_argument_spec, check_args
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.six import iteritems
|
||||
from ansible.module_utils.six.moves import zip
|
||||
|
||||
try:
|
||||
from lxml import etree as ET
|
||||
except ImportError:
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
class FactsBase(object):
|
||||
|
||||
def __init__(self, runner):
|
||||
self.runner = runner
|
||||
self.facts = dict()
|
||||
COMMANDS = list()
|
||||
|
||||
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):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show version | display-xml')
|
||||
self.runner.add_command('show system | display-xml')
|
||||
self.runner.add_command('show running-configuration | grep hostname')
|
||||
COMMANDS = [
|
||||
'show version | display-xml',
|
||||
'show system | display-xml',
|
||||
'show running-configuration | grep hostname'
|
||||
]
|
||||
|
||||
def populate(self):
|
||||
|
||||
data = self.runner.get_command('show version | display-xml')
|
||||
super(Default, self).populate()
|
||||
data = self.responses[0]
|
||||
xml_data = ET.fromstring(data)
|
||||
|
||||
self.facts['name'] = self.parse_name(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)
|
||||
|
||||
self.facts['servicetag'] = self.parse_serialnum(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)
|
||||
|
||||
def parse_name(self, data):
|
||||
|
@ -213,18 +223,21 @@ class Default(FactsBase):
|
|||
|
||||
class Hardware(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show processes memory | grep Total')
|
||||
COMMANDS = [
|
||||
'show version | display-xml',
|
||||
'show processes memory | grep Total'
|
||||
]
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
if match:
|
||||
self.facts['memtotal_mb'] = int(match[0]) / 1024
|
||||
|
@ -243,24 +256,25 @@ class Hardware(FactsBase):
|
|||
|
||||
class Config(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show running-config')
|
||||
COMMANDS = ['show running-config']
|
||||
|
||||
def populate(self):
|
||||
config = self.runner.get_command('show running-config')
|
||||
self.facts['config'] = config
|
||||
super(Config, self).populate()
|
||||
self.facts['config'] = self.responses[0]
|
||||
|
||||
|
||||
class Interfaces(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show interface | display-xml')
|
||||
COMMANDS = [
|
||||
'show interface | display-xml',
|
||||
]
|
||||
|
||||
def populate(self):
|
||||
super(Interfaces, self).populate()
|
||||
self.facts['all_ipv4_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)
|
||||
|
||||
|
@ -294,21 +308,23 @@ class Interfaces(FactsBase):
|
|||
|
||||
for interface in interfaces.findall('./data/ports/ports-state/port'):
|
||||
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')
|
||||
|
||||
typ, sname = name.split('-eth')
|
||||
|
||||
if fanout == "BREAKOUT_1x1":
|
||||
name = "ethernet" + sname
|
||||
name = "ethernet" + sname
|
||||
try:
|
||||
intf = int_facts[name]
|
||||
intf['mediatype'] = mediatype
|
||||
else:
|
||||
# TODO: Loop for the exact subport
|
||||
except:
|
||||
# fanout
|
||||
for subport in xrange(1, 5):
|
||||
name = "ethernet" + sname + ":" + str(subport)
|
||||
intf = int_facts[name]
|
||||
intf['mediatype'] = mediatype
|
||||
try:
|
||||
intf = int_facts[name]
|
||||
intf['mediatype'] = mediatype
|
||||
except:
|
||||
pass
|
||||
|
||||
return int_facts
|
||||
|
||||
|
@ -329,7 +345,7 @@ class Interfaces(FactsBase):
|
|||
ipv4 = interface.find('ipv4')
|
||||
ip_address = ""
|
||||
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:
|
||||
ip_address = prim_ipaddr.text
|
||||
self.add_ip_address(ip_address, 'ipv4')
|
||||
|
@ -340,7 +356,7 @@ class Interfaces(FactsBase):
|
|||
ipv4 = interface.find('ipv4')
|
||||
ip_address = ""
|
||||
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:
|
||||
ip_address = sec_ipaddr.text
|
||||
self.add_ip_address(ip_address, 'ipv4')
|
||||
|
@ -351,7 +367,7 @@ class Interfaces(FactsBase):
|
|||
ipv6 = interface.find('ipv6')
|
||||
ip_address = ""
|
||||
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:
|
||||
ip_address = ipv6_addr.text
|
||||
self.add_ip_address(ip_address, 'ipv6')
|
||||
|
@ -384,11 +400,16 @@ VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
|
|||
|
||||
|
||||
def main():
|
||||
spec = dict(
|
||||
"""main entry point for module execution
|
||||
"""
|
||||
argument_spec = dict(
|
||||
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']
|
||||
|
||||
|
@ -426,28 +447,23 @@ def main():
|
|||
facts = dict()
|
||||
facts['gather_subset'] = list(runable_subsets)
|
||||
|
||||
runner = CommandRunner(module)
|
||||
|
||||
instances = list()
|
||||
for key in runable_subsets:
|
||||
runs = FACT_SUBSETS[key](runner)
|
||||
instances.append(runs)
|
||||
instances.append(FACT_SUBSETS[key](module))
|
||||
|
||||
runner.run()
|
||||
|
||||
try:
|
||||
for inst in instances:
|
||||
inst.populate()
|
||||
facts.update(inst.facts)
|
||||
except Exception:
|
||||
module.exit_json(out=module.from_json(runner.items))
|
||||
for inst in instances:
|
||||
inst.populate()
|
||||
facts.update(inst.facts)
|
||||
|
||||
ansible_facts = dict()
|
||||
for key, value in facts.items():
|
||||
for key, value in iteritems(facts):
|
||||
key = 'ansible_net_%s' % key
|
||||
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__':
|
||||
|
|
|
@ -20,10 +20,9 @@
|
|||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
ANSIBLE_METADATA = {'status': ['preview'],
|
||||
'supported_by': 'community',
|
||||
'metadata_version': '1.0'}
|
||||
|
||||
DOCUMENTATION = """
|
||||
---
|
||||
|
@ -149,14 +148,14 @@ warnings:
|
|||
type: list
|
||||
sample: ['...', '...']
|
||||
"""
|
||||
import time
|
||||
|
||||
from ansible.module_utils.basic import get_exception
|
||||
from ansible.module_utils.netcli import CommandRunner, FailedConditionsError
|
||||
from ansible.module_utils.network import NetworkModule, NetworkError
|
||||
from ansible.module_utils.six import string_types
|
||||
import ansible.module_utils.dellos9
|
||||
from ansible.module_utils.dellos9 import run_commands
|
||||
from ansible.module_utils.dellos9 import dellos9_argument_spec, check_args
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network_common import ComplexList
|
||||
from ansible.module_utils.netcli import Conditional
|
||||
|
||||
VALID_KEYS = ['command', 'prompt', 'response']
|
||||
|
||||
def to_lines(stdout):
|
||||
for item in stdout:
|
||||
|
@ -164,74 +163,87 @@ def to_lines(stdout):
|
|||
item = str(item).split('\n')
|
||||
yield item
|
||||
|
||||
def parse_commands(module):
|
||||
for cmd in module.params['commands']:
|
||||
if isinstance(cmd, string_types):
|
||||
cmd = dict(command=cmd, output=None)
|
||||
elif 'command' not in cmd:
|
||||
module.fail_json(msg='command keyword argument is required')
|
||||
elif not set(cmd.keys()).issubset(VALID_KEYS):
|
||||
module.fail_json(msg='unknown keyword specified')
|
||||
yield cmd
|
||||
|
||||
def parse_commands(module, warnings):
|
||||
command = ComplexList(dict(
|
||||
command=dict(key=True),
|
||||
prompt=dict(),
|
||||
answer=dict()
|
||||
), module)
|
||||
commands = command(module.params['commands'])
|
||||
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():
|
||||
spec = dict(
|
||||
"""main entry point for module execution
|
||||
"""
|
||||
argument_spec = dict(
|
||||
# { command: <str>, prompt: <str>, response: <str> }
|
||||
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'),
|
||||
interval=dict(default=1, type='int')
|
||||
)
|
||||
|
||||
module = NetworkModule(argument_spec=spec,
|
||||
connect_on_load=False,
|
||||
argument_spec.update(dellos9_argument_spec)
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
supports_check_mode=True)
|
||||
|
||||
commands = list(parse_commands(module))
|
||||
conditionals = module.params['wait_for'] or list()
|
||||
result = {'changed': False}
|
||||
|
||||
warnings = list()
|
||||
|
||||
runner = CommandRunner(module)
|
||||
|
||||
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)
|
||||
|
||||
check_args(module, warnings)
|
||||
commands = parse_commands(module, 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)
|
||||
|
||||
|
|
|
@ -201,9 +201,12 @@ saved:
|
|||
sample: True
|
||||
|
||||
"""
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
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 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):
|
||||
|
@ -237,10 +240,11 @@ def main():
|
|||
backup=dict(type='bool', default=False)
|
||||
)
|
||||
|
||||
argument_spec.update(dellos9_argument_spec)
|
||||
|
||||
mutually_exclusive = [('lines', 'src')]
|
||||
|
||||
module = NetworkModule(argument_spec=argument_spec,
|
||||
connect_on_load=False,
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
supports_check_mode=True)
|
||||
|
||||
|
@ -248,7 +252,11 @@ def main():
|
|||
|
||||
match = module.params['match']
|
||||
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)
|
||||
|
||||
|
@ -257,15 +265,18 @@ def main():
|
|||
if parents:
|
||||
contents = get_sublevel_config(config, module)
|
||||
config = NetworkConfig(contents=contents, indent=1)
|
||||
else:
|
||||
config = NetworkConfig(contents=config, indent=1)
|
||||
configobjs = candidate.difference(config, match=match, replace=replace)
|
||||
|
||||
else:
|
||||
configobjs = candidate.items
|
||||
|
||||
if module.params['backup']:
|
||||
result['__backup__'] = module.cli('show running-config')[0]
|
||||
result['__backup__'] = get_config(module)
|
||||
|
||||
commands = list()
|
||||
|
||||
if configobjs:
|
||||
commands = dumps(configobjs, 'commands')
|
||||
commands = commands.split('\n')
|
||||
|
@ -277,11 +288,11 @@ def main():
|
|||
commands.extend(module.params['after'])
|
||||
|
||||
if not module.check_mode and module.params['update'] == 'merge':
|
||||
response = module.config.load_config(commands)
|
||||
result['responses'] = response
|
||||
load_config(module, commands)
|
||||
|
||||
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['changed'] = True
|
||||
|
|
|
@ -142,37 +142,48 @@ ansible_net_neighbors:
|
|||
import re
|
||||
import itertools
|
||||
|
||||
from ansible.module_utils.netcli import CommandRunner
|
||||
from ansible.module_utils.network import NetworkModule
|
||||
import ansible.module_utils.dellos9
|
||||
from ansible.module_utils.dellos9 import run_commands
|
||||
from ansible.module_utils.dellos9 import dellos9_argument_spec, check_args
|
||||
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):
|
||||
|
||||
def __init__(self, runner):
|
||||
self.runner = runner
|
||||
self.facts = dict()
|
||||
COMMANDS = list()
|
||||
|
||||
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):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show version')
|
||||
self.runner.add_command('show inventory')
|
||||
self.runner.add_command('show running-config | grep hostname')
|
||||
COMMANDS = [
|
||||
'show version',
|
||||
'show inventory',
|
||||
'show running-config | grep hostname'
|
||||
]
|
||||
|
||||
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['model'] = self.parse_model(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)
|
||||
|
||||
data = self.runner.get_command('show running-config | grep hostname')
|
||||
data = self.responses[2]
|
||||
self.facts['hostname'] = self.parse_hostname(data)
|
||||
|
||||
def parse_version(self, data):
|
||||
|
@ -206,15 +217,17 @@ class Default(FactsBase):
|
|||
|
||||
class Hardware(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show file-systems')
|
||||
self.runner.add_command('show memory | except Processor')
|
||||
COMMANDS = [
|
||||
'show file-systems',
|
||||
'show memory | except Processor'
|
||||
]
|
||||
|
||||
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)
|
||||
|
||||
data = self.runner.get_command('show memory | except Processor')
|
||||
data = self.responses[1]
|
||||
match = re.findall('\s(\d+)\s', data)
|
||||
if match:
|
||||
self.facts['memtotal_mb'] = int(match[0]) / 1024
|
||||
|
@ -226,25 +239,28 @@ class Hardware(FactsBase):
|
|||
|
||||
class Config(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show running-config')
|
||||
COMMANDS = ['show running-config']
|
||||
|
||||
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):
|
||||
|
||||
def commands(self):
|
||||
self.runner.add_command('show interfaces')
|
||||
self.runner.add_command('show ipv6 interface')
|
||||
self.runner.add_command('show lldp neighbors detail')
|
||||
COMMANDS = [
|
||||
'show interfaces',
|
||||
'show ipv6 interface',
|
||||
'show lldp neighbors detail',
|
||||
'show inventory'
|
||||
]
|
||||
|
||||
def populate(self):
|
||||
super(Interfaces, self).populate()
|
||||
self.facts['all_ipv4_addresses'] = list()
|
||||
self.facts['all_ipv6_addresses'] = list()
|
||||
|
||||
data = self.runner.get_command('show interfaces')
|
||||
data = self.responses[0]
|
||||
interfaces = self.parse_interfaces(data)
|
||||
|
||||
for key in interfaces.keys():
|
||||
|
@ -261,14 +277,14 @@ class Interfaces(FactsBase):
|
|||
|
||||
self.facts['interfaces'] = self.populate_interfaces(interfaces)
|
||||
|
||||
data = self.runner.get_command('show ipv6 interface')
|
||||
data = self.responses[1]
|
||||
if len(data) > 0:
|
||||
data = self.parse_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):
|
||||
neighbors = self.runner.get_command('show lldp neighbors detail')
|
||||
neighbors = self.responses[2]
|
||||
self.facts['neighbors'] = self.parse_neighbors(neighbors)
|
||||
|
||||
def get_protocol_list(self, data):
|
||||
|
@ -499,11 +515,16 @@ VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
|
|||
|
||||
|
||||
def main():
|
||||
spec = dict(
|
||||
"""main entry point for module execution
|
||||
"""
|
||||
argument_spec = dict(
|
||||
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']
|
||||
|
||||
|
@ -541,28 +562,23 @@ def main():
|
|||
facts = dict()
|
||||
facts['gather_subset'] = list(runable_subsets)
|
||||
|
||||
runner = CommandRunner(module)
|
||||
|
||||
instances = list()
|
||||
for key in runable_subsets:
|
||||
runs = FACT_SUBSETS[key](runner)
|
||||
instances.append(runs)
|
||||
instances.append(FACT_SUBSETS[key](module))
|
||||
|
||||
runner.run()
|
||||
|
||||
try:
|
||||
for inst in instances:
|
||||
inst.populate()
|
||||
facts.update(inst.facts)
|
||||
except Exception:
|
||||
module.exit_json(out=module.from_json(runner.items))
|
||||
for inst in instances:
|
||||
inst.populate()
|
||||
facts.update(inst.facts)
|
||||
|
||||
ansible_facts = dict()
|
||||
for key, value in facts.items():
|
||||
for key, value in iteritems(facts):
|
||||
key = 'ansible_net_%s' % key
|
||||
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__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue