Network module code cleanup (#5061)

* Fix imports in junos_template

* Python 3 compatibility in eos_command

* Python 3 compatibility for ios_command

* Clean up issues with ios_facts

* Python 3 compatibility for ios_facts

* Import shuffle in ios_template

* Python 3 compatibility for iosxr_command

* Clean up iosxr_facts.py

* Python 3 compatibility for iosxr_facts

* Python 3 compatibility for junos_command

* Python 3 compatibility for ops_command

* Cleanup issues with ops_facts

* Python 3 compatibility for ops_facts

* Cleanup issues with ops_template

* Python 3 compatibility for vyos_command

* Cleanup issues with vyos_facts

* Python 3 compatibility for vyos_facts
This commit is contained in:
Nathaniel Case 2016-09-27 17:06:03 -04:00 committed by Matt Clay
parent 6ae0342a0a
commit aa82f48dc8
12 changed files with 74 additions and 48 deletions

View file

@ -152,18 +152,19 @@ from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.netcli import AddCommandError from ansible.module_utils.netcli import AddCommandError
from ansible.module_utils.netcli import FailedConditionsError from ansible.module_utils.netcli import FailedConditionsError
from ansible.module_utils.netcli import FailedConditionalError from ansible.module_utils.netcli import FailedConditionalError
from ansible.module_utils.six import string_types
VALID_KEYS = ['command', 'output', 'prompt', 'response'] VALID_KEYS = ['command', 'output', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
if isinstance(item, basestring): if isinstance(item, string_types):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module): def parse_commands(module):
for cmd in module.params['commands']: for cmd in module.params['commands']:
if isinstance(cmd, basestring): if isinstance(cmd, string_types):
cmd = dict(command=cmd, output=None) cmd = dict(command=cmd, output=None)
elif 'command' not in cmd: elif 'command' not in cmd:
module.fail_json(msg='command keyword argument is required') module.fail_json(msg='command keyword argument is required')

View file

@ -139,22 +139,24 @@ failed_conditions:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import ansible.module_utils.ios
from ansible.module_utils.basic import get_exception from ansible.module_utils.basic import get_exception
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
from ansible.module_utils.ios import NetworkModule, NetworkError from ansible.module_utils.network import NetworkModule, NetworkError
from ansible.module_utils.six import string_types
VALID_KEYS = ['command', 'prompt', 'response'] VALID_KEYS = ['command', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
if isinstance(item, basestring): if isinstance(item, string_types):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module): def parse_commands(module):
for cmd in module.params['commands']: for cmd in module.params['commands']:
if isinstance(cmd, basestring): if isinstance(cmd, string_types):
cmd = dict(command=cmd, output=None) cmd = dict(command=cmd, output=None)
elif 'command' not in cmd: elif 'command' not in cmd:
module.fail_json(msg='command keyword argument is required') module.fail_json(msg='command keyword argument is required')

View file

@ -126,9 +126,11 @@ ansible_net_neighbors:
import re import re
import itertools import itertools
from ansible.module_utils.basic import get_exception import ansible.module_utils.ios
from ansible.module_utils.netcli import CommandRunner, AddCommandError from ansible.module_utils.netcli import CommandRunner, AddCommandError
from ansible.module_utils.ios import NetworkModule from ansible.module_utils.network import NetworkModule
from ansible.module_utils.six import iteritems
from ansible.module_utils.six.moves import zip
def add_command(runner, command): def add_command(runner, command):
@ -147,6 +149,9 @@ class FactsBase(object):
self.commands() self.commands()
def commands(self):
raise NotImplementedError
class Default(FactsBase): class Default(FactsBase):
def commands(self): def commands(self):
@ -199,7 +204,7 @@ class Hardware(FactsBase):
self.facts['filesystems'] = self.parse_filesystems(data) self.facts['filesystems'] = self.parse_filesystems(data)
data = self.runner.get_command('show memory statistics | include Processor') data = self.runner.get_command('show memory statistics | include Processor')
match = re.findall('\s(\d+)\s', data) match = re.findall(r'\s(\d+)\s', data)
if match: if match:
self.facts['memtotal_mb'] = int(match[0]) / 1024 self.facts['memtotal_mb'] = int(match[0]) / 1024
self.facts['memfree_mb'] = int(match[1]) / 1024 self.facts['memfree_mb'] = int(match[1]) / 1024
@ -244,7 +249,7 @@ class Interfaces(FactsBase):
def populate_interfaces(self, interfaces): def populate_interfaces(self, interfaces):
facts = dict() facts = dict()
for key, value in interfaces.iteritems(): for key, value in iteritems(interfaces):
intf = dict() intf = dict()
intf['description'] = self.parse_description(value) intf['description'] = self.parse_description(value)
intf['macaddress'] = self.parse_macaddress(value) intf['macaddress'] = self.parse_macaddress(value)
@ -266,11 +271,11 @@ class Interfaces(FactsBase):
return facts return facts
def populate_ipv6_interfaces(self, data): def populate_ipv6_interfaces(self, data):
for key, value in data.iteritems(): for key, value in iteritems(data):
self.facts['interfaces'][key]['ipv6'] = list() self.facts['interfaces'][key]['ipv6'] = list()
addresses = re.findall(r'\s+(.+), subnet', value, re.M) addresses = re.findall(r'\s+(.+), subnet', value, re.M)
subnets = re.findall(r', subnet is (.+)$', value, re.M) subnets = re.findall(r', subnet is (.+)$', value, re.M)
for addr, subnet in itertools.izip(addresses, subnets): for addr, subnet in zip(addresses, subnets):
ipv6 = dict(address=addr.strip(), subnet=subnet.strip()) ipv6 = dict(address=addr.strip(), subnet=subnet.strip())
self.add_ip_address(addr.strip(), 'ipv6') self.add_ip_address(addr.strip(), 'ipv6')
self.facts['interfaces'][key]['ipv6'].append(ipv6) self.facts['interfaces'][key]['ipv6'].append(ipv6)
@ -297,6 +302,7 @@ class Interfaces(FactsBase):
def parse_interfaces(self, data): def parse_interfaces(self, data):
parsed = dict() parsed = dict()
key = ''
for line in data.split('\n'): for line in data.split('\n'):
if len(line) == 0: if len(line) == 0:
continue continue
@ -444,7 +450,7 @@ def main():
module.exit_json(out=module.from_json(runner.items)) module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict() ansible_facts = dict()
for key, value in facts.iteritems(): for key, value in iteritems(facts):
key = 'ansible_net_%s' % key key = 'ansible_net_%s' % key
ansible_facts[key] = value ansible_facts[key] = value

View file

@ -114,8 +114,9 @@ responses:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import ansible.module_utils.ios
from ansible.module_utils.netcfg import NetworkConfig, dumps from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.ios import NetworkModule, NetworkError from ansible.module_utils.ios import NetworkModule
def get_config(module): def get_config(module):
config = module.params['config'] or dict() config = module.params['config'] or dict()

View file

@ -138,22 +138,24 @@ failed_conditions:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import ansible.module_utils.iosxr
from ansible.module_utils.basic import get_exception from ansible.module_utils.basic import get_exception
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
from ansible.module_utils.iosxr import NetworkModule, NetworkError from ansible.module_utils.network import NetworkModule, NetworkError
from ansible.module_utils.six import string_types
VALID_KEYS = ['command', 'output', 'prompt', 'response'] VALID_KEYS = ['command', 'output', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
if isinstance(item, basestring): if isinstance(item, string_types):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module): def parse_commands(module):
for cmd in module.params['commands']: for cmd in module.params['commands']:
if isinstance(cmd, basestring): if isinstance(cmd, string_types):
cmd = dict(command=cmd, output=None) cmd = dict(command=cmd, output=None)
elif 'command' not in cmd: elif 'command' not in cmd:
module.fail_json(msg='command keyword argument is required') module.fail_json(msg='command keyword argument is required')

View file

@ -116,11 +116,12 @@ ansible_net_neighbors:
type: dict type: dict
""" """
import re import re
import itertools
from ansible.module_utils.basic import get_exception import ansible.module_utils.iosxr
from ansible.module_utils.netcli import CommandRunner, AddCommandError from ansible.module_utils.netcli import CommandRunner, AddCommandError
from ansible.module_utils.iosxr import NetworkModule from ansible.module_utils.network import NetworkModule
from ansible.module_utils.six import iteritems
from ansible.module_utils.six.moves import zip
def add_command(runner, command): def add_command(runner, command):
@ -139,6 +140,9 @@ class FactsBase(object):
self.commands() self.commands()
def commands(self):
raise NotImplementedError
class Default(FactsBase): class Default(FactsBase):
def commands(self): def commands(self):
@ -223,7 +227,7 @@ class Interfaces(FactsBase):
def populate_interfaces(self, interfaces): def populate_interfaces(self, interfaces):
facts = dict() facts = dict()
for key, value in interfaces.iteritems(): for key, value in iteritems(interfaces):
intf = dict() intf = dict()
intf['description'] = self.parse_description(value) intf['description'] = self.parse_description(value)
intf['macaddress'] = self.parse_macaddress(value) intf['macaddress'] = self.parse_macaddress(value)
@ -244,11 +248,11 @@ class Interfaces(FactsBase):
return facts return facts
def populate_ipv6_interfaces(self, data): def populate_ipv6_interfaces(self, data):
for key, value in data.iteritems(): for key, value in iteritems(data):
self.facts['interfaces'][key]['ipv6'] = list() self.facts['interfaces'][key]['ipv6'] = list()
addresses = re.findall(r'\s+(.+), subnet', value, re.M) addresses = re.findall(r'\s+(.+), subnet', value, re.M)
subnets = re.findall(r', subnet is (.+)$', value, re.M) subnets = re.findall(r', subnet is (.+)$', value, re.M)
for addr, subnet in itertools.izip(addresses, subnets): for addr, subnet in zip(addresses, subnets):
ipv6 = dict(address=addr.strip(), subnet=subnet.strip()) ipv6 = dict(address=addr.strip(), subnet=subnet.strip())
self.add_ip_address(addr.strip(), 'ipv6') self.add_ip_address(addr.strip(), 'ipv6')
self.facts['interfaces'][key]['ipv6'].append(ipv6) self.facts['interfaces'][key]['ipv6'].append(ipv6)
@ -276,6 +280,7 @@ class Interfaces(FactsBase):
def parse_interfaces(self, data): def parse_interfaces(self, data):
parsed = dict() parsed = dict()
key = ''
for line in data.split('\n'): for line in data.split('\n'):
if len(line) == 0: if len(line) == 0:
continue continue
@ -416,11 +421,10 @@ def main():
inst.populate() inst.populate()
facts.update(inst.facts) facts.update(inst.facts)
except Exception: except Exception:
raise
module.exit_json(out=module.from_json(runner.items)) module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict() ansible_facts = dict()
for key, value in facts.iteritems(): for key, value in iteritems(facts):
key = 'ansible_net_%s' % key key = 'ansible_net_%s' % key
ansible_facts[key] = value ansible_facts[key] = value

View file

@ -158,16 +158,14 @@ xml:
type: list type: list
sample: [['...', '...'], ['...', '...']] sample: [['...', '...'], ['...', '...']]
""" """
import re
import ansible.module_utils.junos import ansible.module_utils.junos
from ansible.module_utils.basic import get_exception from ansible.module_utils.basic import get_exception
from ansible.module_utils.network import NetworkModule, NetworkError from ansible.module_utils.network import NetworkModule, NetworkError
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
from ansible.module_utils.junos import xml_to_json from ansible.module_utils.junos import xml_to_json
from ansible.module_utils.six import string_types
VALID_KEYS = { VALID_KEYS = {
'cli': frozenset(['command', 'output', 'prompt', 'response']), 'cli': frozenset(['command', 'output', 'prompt', 'response']),
@ -177,7 +175,7 @@ VALID_KEYS = {
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
if isinstance(item, basestring): if isinstance(item, string_types):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
@ -189,7 +187,7 @@ def parse(module, command_type):
parsed = list() parsed = list()
for item in (items or list()): for item in (items or list()):
if isinstance(item, basestring): if isinstance(item, string_types):
item = dict(command=item, output=None) item = dict(command=item, output=None)
elif 'command' not in item: elif 'command' not in item:
module.fail_json(msg='command keyword argument is required') module.fail_json(msg='command keyword argument is required')
@ -302,4 +300,3 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -126,22 +126,24 @@ failed_conditions:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import ansible.module_utils.openswitch
from ansible.module_utils.basic import get_exception from ansible.module_utils.basic import get_exception
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
from ansible.module_utils.openswitch import NetworkModule, NetworkError from ansible.module_utils.network import NetworkModule, NetworkError
from ansible.module_utils.six import string_types
VALID_KEYS = ['command', 'prompt', 'response'] VALID_KEYS = ['command', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
if isinstance(item, basestring): if isinstance(item, string_types):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module): def parse_commands(module):
for cmd in module.params['commands']: for cmd in module.params['commands']:
if isinstance(cmd, basestring): if isinstance(cmd, string_types):
cmd = dict(command=cmd, output=None) cmd = dict(command=cmd, output=None)
elif 'command' not in cmd: elif 'command' not in cmd:
module.fail_json(msg='command keyword argument is required') module.fail_json(msg='command keyword argument is required')
@ -220,4 +222,3 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -171,9 +171,10 @@ endpoints:
""" """
import re import re
from ansible.module_utils.basic import get_exception import ansible.module_utils.openswitch
from ansible.module_utils.netcli import CommandRunner, AddCommandError from ansible.module_utils.netcli import CommandRunner, AddCommandError
from ansible.module_utils.openswitch import NetworkModule from ansible.module_utils.network import NetworkModule
from ansible.module_utils.six import iteritems
def add_command(runner, command): def add_command(runner, command):
@ -196,6 +197,9 @@ class FactsBase(object):
if self.transport == 'cli': if self.transport == 'cli':
self.commands() self.commands()
def commands(self):
raise NotImplementedError
def populate(self): def populate(self):
getattr(self, self.transport)() getattr(self, self.transport)()
@ -395,11 +399,10 @@ def main():
inst.populate() inst.populate()
facts.update(inst.facts) facts.update(inst.facts)
except Exception: except Exception:
raise
module.exit_json(out=module.from_json(runner.items)) module.exit_json(out=module.from_json(runner.items))
ansible_facts = dict() ansible_facts = dict()
for key, value in facts.iteritems(): for key, value in iteritems(facts):
# this is to maintain capability with ops_facts 2.1 # this is to maintain capability with ops_facts 2.1
if key.startswith('_'): if key.startswith('_'):
ansible_facts[key[1:]] = value ansible_facts[key[1:]] = value

View file

@ -95,10 +95,11 @@ responses:
type: list type: list
sample: [...] sample: [...]
""" """
import copy
import ansible.module_utils.openswitch
from ansible.module_utils.netcfg import NetworkConfig, dumps from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.openswitch import NetworkModule from ansible.module_utils.network import NetworkModule
from ansible.module_utils.openswitch import HAS_OPS
def get_config(module): def get_config(module):

View file

@ -126,22 +126,24 @@ warnings:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
import ansible.module_utils.vyos
from ansible.module_utils.basic import get_exception from ansible.module_utils.basic import get_exception
from ansible.module_utils.netcli import CommandRunner from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
from ansible.module_utils.vyos import NetworkModule, NetworkError from ansible.module_utils.network import NetworkModule, NetworkError
from ansible.module_utils.six import string_types
VALID_KEYS = ['command', 'output', 'prompt', 'response'] VALID_KEYS = ['command', 'output', 'prompt', 'response']
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
if isinstance(item, basestring): if isinstance(item, string_types):
item = str(item).split('\n') item = str(item).split('\n')
yield item yield item
def parse_commands(module): def parse_commands(module):
for cmd in module.params['commands']: for cmd in module.params['commands']:
if isinstance(cmd, basestring): if isinstance(cmd, string_types):
cmd = dict(command=cmd, output=None) cmd = dict(command=cmd, output=None)
elif 'command' not in cmd: elif 'command' not in cmd:
module.fail_json(msg='command keyword argument is required') module.fail_json(msg='command keyword argument is required')

View file

@ -100,8 +100,11 @@ ansible_net_gather_subset:
""" """
import re import re
from ansible.module_utils.netcmd import CommandRunner import ansible.module_utils.vyos
from ansible.module_utils.vyos import NetworkModule from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.netcli import CommandRunner
from ansible.module_utils.network import NetworkModule
from ansible.module_utils.six import iteritems
class FactsBase(object): class FactsBase(object):
@ -112,6 +115,9 @@ class FactsBase(object):
self.commands() self.commands()
def commands(self):
raise NotImplementedError
class Default(FactsBase): class Default(FactsBase):
@ -160,7 +166,7 @@ class Config(FactsBase):
entry = None entry = None
for line in commits.split('\n'): for line in commits.split('\n'):
match = re.match('(\d+)\s+(.+)by(.+)via(.+)', line) match = re.match(r'(\d+)\s+(.+)by(.+)via(.+)', line)
if match: if match:
if entry: if entry:
entries.append(entry) entries.append(entry)
@ -288,7 +294,7 @@ def main():
for key in runable_subsets: for key in runable_subsets:
instances.append(FACT_SUBSETS[key](runner)) instances.append(FACT_SUBSETS[key](runner))
runner.run_commands() runner.run()
try: try:
for inst in instances: for inst in instances:
@ -299,7 +305,7 @@ def main():
module.fail_json(msg='unknown failure', output=runner.items, exc=str(exc)) module.fail_json(msg='unknown failure', output=runner.items, exc=str(exc))
ansible_facts = dict() ansible_facts = dict()
for key, value in facts.iteritems(): for key, value in iteritems(facts):
key = 'ansible_net_%s' % key key = 'ansible_net_%s' % key
ansible_facts[key] = value ansible_facts[key] = value