diff options
Diffstat (limited to 'plugins/module_utils')
11 files changed, 150 insertions, 129 deletions
diff --git a/plugins/module_utils/network/vyos/argspec/facts/facts.py b/plugins/module_utils/network/vyos/argspec/facts/facts.py index 456c8bd..84b7135 100644 --- a/plugins/module_utils/network/vyos/argspec/facts/facts.py +++ b/plugins/module_utils/network/vyos/argspec/facts/facts.py @@ -5,7 +5,6 @@ The arg spec for the vyos facts module. """ - from __future__ import absolute_import, division, print_function __metaclass__ = type @@ -13,16 +12,11 @@ __metaclass__ = type class FactsArgs(object): # pylint: disable=R0903 """ The arg spec for the vyos facts module """ - def __init__(self, **kwargs): pass choices = [ - 'all', - 'interfaces', - '!interfaces', - 'l3_interfaces', - '!l3_interfaces' + 'all', 'interfaces', '!interfaces', 'l3_interfaces', '!l3_interfaces' ] argument_spec = { diff --git a/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py b/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py index d6ab446..40df125 100644 --- a/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py @@ -30,7 +30,6 @@ __metaclass__ = type class InterfacesArgs(object): # pylint: disable=R0903 """The arg spec for the vyos_interfaces module """ - def __init__(self, **kwargs): pass diff --git a/plugins/module_utils/network/vyos/argspec/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/vyos/argspec/l3_interfaces/l3_interfaces.py index e5785a8..240f348 100644 --- a/plugins/module_utils/network/vyos/argspec/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/vyos/argspec/l3_interfaces/l3_interfaces.py @@ -25,7 +25,6 @@ The arg spec for the vyos_l3_interfaces module """ - from __future__ import absolute_import, division, print_function __metaclass__ = type @@ -33,7 +32,6 @@ __metaclass__ = type class L3_interfacesArgs(object): # pylint: disable=R0903 """The arg spec for the vyos_l3_interfaces module """ - def __init__(self, **kwargs): pass diff --git a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py index b17971c..c85b865 100644 --- a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py @@ -12,7 +12,6 @@ created from __future__ import absolute_import, division, print_function __metaclass__ = type - from copy import deepcopy from ansible.module_utils.network.common.cfg.base import ConfigBase from ansible.module_utils.network.common.utils import to_list, dict_diff, remove_empties @@ -24,7 +23,6 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network. \ vyos.utils.utils import search_obj_in_list, get_interface_type, dict_delete - class Interfaces(ConfigBase): """ The vyos_interfaces class @@ -35,9 +33,7 @@ class Interfaces(ConfigBase): '!min', ] - gather_network_resources = [ - 'interfaces' - ] + gather_network_resources = ['interfaces'] def __init__(self, module): super(Interfaces, self).__init__(module) @@ -48,8 +44,8 @@ class Interfaces(ConfigBase): :rtype: A dictionary :returns: The current configuration as a dictionary """ - facts, _warnings = Facts(self._module).get_facts(self.gather_subset, - self.gather_network_resources) + facts, _warnings = Facts(self._module).get_facts( + self.gather_subset, self.gather_network_resources) interfaces_facts = facts['ansible_network_resources'].get('interfaces') if not interfaces_facts: return [] @@ -118,19 +114,11 @@ class Interfaces(ConfigBase): if not want: for intf in have: commands.extend( - self._state_deleted( - {'name': intf['name']}, - intf - ) - ) + self._state_deleted({'name': intf['name']}, intf)) else: for item in want: obj_in_have = search_obj_in_list(item['name'], have) - commands.extend( - self._state_deleted( - item, obj_in_have - ) - ) + commands.extend(self._state_deleted(item, obj_in_have)) else: for item in want: name = item['name'] @@ -140,18 +128,10 @@ class Interfaces(ConfigBase): obj_in_have = {'name': item['name']} elif state == 'merged': - commands.extend( - self._state_merged( - item, obj_in_have - ) - ) + commands.extend(self._state_merged(item, obj_in_have)) elif state == 'replaced': - commands.extend( - self._state_replaced( - item, obj_in_have - ) - ) + commands.extend(self._state_replaced(item, obj_in_have)) return commands @@ -182,7 +162,8 @@ class Interfaces(ConfigBase): for intf in have: intf_in_want = search_obj_in_list(intf['name'], want) if not intf_in_want: - commands.extend(self._state_deleted({'name': intf['name']}, intf)) + commands.extend( + self._state_deleted({'name': intf['name']}, intf)) for intf in want: intf_in_have = search_obj_in_list(intf['name'], have) @@ -208,18 +189,30 @@ class Interfaces(ConfigBase): if updates: for key, value in iteritems(updates): - commands.append(self._compute_commands(key=key, value=value, interface=want_copy['name'])) + commands.append( + self._compute_commands(key=key, + value=value, + interface=want_copy['name'])) if want_vifs: for want_vif in want_vifs: - have_vif = search_obj_in_list(want_vif['vlan_id'], have_vifs, key='vlan_id') + have_vif = search_obj_in_list(want_vif['vlan_id'], + have_vifs, + key='vlan_id') if not have_vif: - have_vif = {'vlan_id': want_vif['vlan_id'], 'enabled': True} + have_vif = { + 'vlan_id': want_vif['vlan_id'], + 'enabled': True + } vif_updates = dict_diff(have_vif, want_vif) if vif_updates: for key, value in iteritems(vif_updates): - commands.append(self._compute_commands(key=key, value=value, interface=want_copy['name'], vif=want_vif['vlan_id'])) + commands.append( + self._compute_commands(key=key, + value=value, + interface=want_copy['name'], + vif=want_vif['vlan_id'])) return commands @@ -241,27 +234,52 @@ class Interfaces(ConfigBase): for key in dict_delete(have_copy, want_copy).keys(): if key == 'enabled': continue - commands.append(self._compute_commands(key=key, interface=want_copy['name'], remove=True)) + commands.append( + self._compute_commands(key=key, + interface=want_copy['name'], + remove=True)) if have_copy['enabled'] is False: - commands.append(self._compute_commands(key='enabled', value=True, interface=want_copy['name'])) + commands.append( + self._compute_commands(key='enabled', + value=True, + interface=want_copy['name'])) if have_vifs: for have_vif in have_vifs: - want_vif = search_obj_in_list(have_vif['vlan_id'], want_vifs, key='vlan_id') + want_vif = search_obj_in_list(have_vif['vlan_id'], + want_vifs, + key='vlan_id') if not want_vif: - want_vif = {'vlan_id': have_vif['vlan_id'], 'enabled': True} + want_vif = { + 'vlan_id': have_vif['vlan_id'], + 'enabled': True + } for key in dict_delete(have_vif, want_vif).keys(): if key == 'enabled': continue - commands.append(self._compute_commands(key=key, interface=want_copy['name'], vif=want_vif['vlan_id'], remove=True)) + commands.append( + self._compute_commands(key=key, + interface=want_copy['name'], + vif=want_vif['vlan_id'], + remove=True)) if have_vif['enabled'] is False: - commands.append(self._compute_commands(key='enabled', value=True, interface=want_copy['name'], vif=want_vif['vlan_id'])) + commands.append( + self._compute_commands(key='enabled', + value=True, + interface=want_copy['name'], + vif=want_vif['vlan_id'])) return commands - def _compute_commands(self, interface, key, vif=None, value=None, remove=False): - intf_context = 'interfaces {0} {1}'.format(get_interface_type(interface), interface) + def _compute_commands(self, + interface, + key, + vif=None, + value=None, + remove=False): + intf_context = 'interfaces {0} {1}'.format( + get_interface_type(interface), interface) set_cmd = 'set {0}'.format(intf_context) del_cmd = 'delete {0}'.format(intf_context) diff --git a/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py index 9027c84..7cac4ed 100644 --- a/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py @@ -14,7 +14,6 @@ created from __future__ import absolute_import, division, print_function __metaclass__ = type - from copy import deepcopy from ansible.module_utils.network.common.cfg.base import ConfigBase from ansible.module_utils.network.common.utils import to_list, remove_empties @@ -26,7 +25,6 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network. \ vyos.utils.utils import search_obj_in_list, get_interface_type, diff_list_of_dicts - class L3_interfaces(ConfigBase): """ The vyos_l3_interfaces class @@ -50,8 +48,10 @@ class L3_interfaces(ConfigBase): :rtype: A dictionary :returns: The current configuration as a dictionary """ - facts, _warnings = Facts(self._module).get_facts(self.gather_subset, self.gather_network_resources) - l3_interfaces_facts = facts['ansible_network_resources'].get('l3_interfaces') + facts, _warnings = Facts(self._module).get_facts( + self.gather_subset, self.gather_network_resources) + l3_interfaces_facts = facts['ansible_network_resources'].get( + 'l3_interfaces') if not l3_interfaces_facts: return [] return l3_interfaces_facts @@ -120,19 +120,11 @@ class L3_interfaces(ConfigBase): if not want: for intf in have: commands.extend( - self._state_deleted( - {'name': intf['name']}, - intf - ) - ) + self._state_deleted({'name': intf['name']}, intf)) else: for item in want: obj_in_have = search_obj_in_list(item['name'], have) - commands.extend( - self._state_deleted( - item, obj_in_have - ) - ) + commands.extend(self._state_deleted(item, obj_in_have)) else: for item in want: name = item['name'] @@ -142,18 +134,10 @@ class L3_interfaces(ConfigBase): obj_in_have = {'name': item['name']} if state == 'merged': - commands.extend( - self._state_merged( - item, obj_in_have - ) - ) + commands.extend(self._state_merged(item, obj_in_have)) elif state == 'replaced': - commands.extend( - self._state_replaced( - item, obj_in_have - ) - ) + commands.extend(self._state_replaced(item, obj_in_have)) return commands @@ -184,7 +168,8 @@ class L3_interfaces(ConfigBase): for intf in have: intf_in_want = search_obj_in_list(intf['name'], want) if not intf_in_want: - commands.extend(self._state_deleted({'name': intf['name']}, intf)) + commands.extend( + self._state_deleted({'name': intf['name']}, intf)) for intf in want: intf_in_have = search_obj_in_list(intf['name'], have) @@ -208,17 +193,26 @@ class L3_interfaces(ConfigBase): for update in self._get_updates(want_copy, have_copy): for key, value in iteritems(update): - commands.append(self._compute_commands(key=key, value=value, interface=want_copy['name'])) + commands.append( + self._compute_commands(key=key, + value=value, + interface=want_copy['name'])) if want_vifs: for want_vif in want_vifs: - have_vif = search_obj_in_list(want_vif['vlan_id'], have_vifs, key='vlan_id') + have_vif = search_obj_in_list(want_vif['vlan_id'], + have_vifs, + key='vlan_id') if not have_vif: have_vif = {} for update in self._get_updates(want_vif, have_vif): for key, value in iteritems(update): - commands.append(self._compute_commands(key=key, value=value, interface=want_copy['name'], vif=want_vif['vlan_id'])) + commands.append( + self._compute_commands(key=key, + value=value, + interface=want_copy['name'], + vif=want_vif['vlan_id'])) return commands @@ -238,22 +232,39 @@ class L3_interfaces(ConfigBase): for update in self._get_updates(have_copy, want_copy): for key, value in iteritems(update): - commands.append(self._compute_commands(key=key, value=value, interface=want_copy['name'], remove=True)) + commands.append( + self._compute_commands(key=key, + value=value, + interface=want_copy['name'], + remove=True)) if have_vifs: for have_vif in have_vifs: - want_vif = search_obj_in_list(have_vif['vlan_id'], want_vifs, key='vlan_id') + want_vif = search_obj_in_list(have_vif['vlan_id'], + want_vifs, + key='vlan_id') if not want_vif: want_vif = {'vlan_id': have_vif['vlan_id']} for update in self._get_updates(have_vif, want_vif): for key, value in iteritems(update): - commands.append(self._compute_commands(key=key, interface=want_copy['name'], value=value, vif=want_vif['vlan_id'], remove=True)) + commands.append( + self._compute_commands(key=key, + interface=want_copy['name'], + value=value, + vif=want_vif['vlan_id'], + remove=True)) return commands - def _compute_commands(self, interface, key, vif=None, value=None, remove=False): - intf_context = 'interfaces {0} {1}'.format(get_interface_type(interface), interface) + def _compute_commands(self, + interface, + key, + vif=None, + value=None, + remove=False): + intf_context = 'interfaces {0} {1}'.format( + get_interface_type(interface), interface) set_cmd = 'set {0}'.format(intf_context) del_cmd = 'delete {0}'.format(intf_context) @@ -271,7 +282,9 @@ class L3_interfaces(ConfigBase): def _get_updates(self, want, have): updates = [] - updates = diff_list_of_dicts(want.get('ipv4', []), have.get('ipv4', [])) - updates.extend(diff_list_of_dicts(want.get('ipv6', []), have.get('ipv6', []))) + updates = diff_list_of_dicts(want.get('ipv4', []), + have.get('ipv4', [])) + updates.extend( + diff_list_of_dicts(want.get('ipv6', []), have.get('ipv6', []))) return updates diff --git a/plugins/module_utils/network/vyos/facts/facts.py b/plugins/module_utils/network/vyos/facts/facts.py index a065eaf..bcdc54f 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -27,16 +27,9 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network. \ from ansible.module_utils. \ network.vyos.vyos import run_commands, get_capabilities - -FACT_LEGACY_SUBSETS = dict( - default=Default, - neighbors=Neighbors, - config=Config -) -FACT_RESOURCE_SUBSETS = dict( - interfaces=InterfacesFacts, - l3_interfaces=L3_interfacesFacts -) +FACT_LEGACY_SUBSETS = dict(default=Default, neighbors=Neighbors, config=Config) +FACT_RESOURCE_SUBSETS = dict(interfaces=InterfacesFacts, + l3_interfaces=L3_interfacesFacts) class Facts(FactsBase): @@ -49,7 +42,10 @@ class Facts(FactsBase): def __init__(self, module): super(Facts, self).__init__(module) - def get_facts(self, legacy_facts_type=None, resource_facts_type=None, data=None): + def get_facts(self, + legacy_facts_type=None, + resource_facts_type=None, + data=None): """ Collect the facts for vyos :param legacy_facts_type: List of legacy facts types @@ -58,12 +54,15 @@ class Facts(FactsBase): :rtype: dict :return: the facts gathered """ - netres_choices = FactsArgs.argument_spec['gather_network_resources'].get('choices', []) + netres_choices = FactsArgs.argument_spec[ + 'gather_network_resources'].get('choices', []) if self.VALID_RESOURCE_SUBSETS: - self.get_network_resources_facts(netres_choices, FACT_RESOURCE_SUBSETS, + self.get_network_resources_facts(netres_choices, + FACT_RESOURCE_SUBSETS, resource_facts_type, data) if self.VALID_LEGACY_GATHER_SUBSETS: - self.get_network_legacy_facts(FACT_LEGACY_SUBSETS, legacy_facts_type) + self.get_network_legacy_facts(FACT_LEGACY_SUBSETS, + legacy_facts_type) return self.ansible_facts, self._warnings diff --git a/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py b/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py index cc89e4f..915abaa 100644 --- a/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py @@ -13,7 +13,6 @@ based on the configuration. from __future__ import absolute_import, division, print_function __metaclass__ = type - from re import findall, M from copy import deepcopy from ansible.module_utils.network.common import utils @@ -21,11 +20,9 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network. \ vyos.argspec.interfaces.interfaces import InterfacesArgs - class InterfacesFacts(object): """ The vyos interfaces fact class """ - def __init__(self, module, subspec='config', options='options'): self._module = module self.argument_spec = InterfacesArgs.argument_spec @@ -52,8 +49,9 @@ class InterfacesFacts(object): data = connection.get_config(flags=['| grep interfaces']) objs = [] - interface_names = findall(r'^set interfaces (?:ethernet|bonding|vti|loopback|vxlan) (?:\'*)(\S+)(?:\'*)', - data, M) + interface_names = findall( + r'^set interfaces (?:ethernet|bonding|vti|loopback|vxlan) (?:\'*)(\S+)(?:\'*)', + data, M) if interface_names: for interface in set(interface_names): intf_regex = r' %s .+$' % interface.strip("'") @@ -65,7 +63,8 @@ class InterfacesFacts(object): facts = {} if objs: facts['interfaces'] = [] - params = utils.validate_config(self.argument_spec, {'config': objs}) + params = utils.validate_config(self.argument_spec, + {'config': objs}) for cfg in params['config']: facts['interfaces'].append(utils.remove_empties(cfg)) @@ -84,8 +83,8 @@ class InterfacesFacts(object): """ vif_conf = '\n'.join(filter(lambda x: ('vif' in x), conf)) eth_conf = '\n'.join(filter(lambda x: ('vif' not in x), conf)) - config = self.parse_attribs( - ['description', 'speed', 'mtu', 'duplex'], eth_conf) + config = self.parse_attribs(['description', 'speed', 'mtu', 'duplex'], + eth_conf) config['vifs'] = self.parse_vifs(vif_conf) return utils.remove_empties(config) diff --git a/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py index 61f635b..c082c5b 100644 --- a/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py @@ -13,7 +13,6 @@ based on the configuration. from __future__ import absolute_import, division, print_function __metaclass__ = type - import re from copy import deepcopy from ansible.module_utils.network.common import utils @@ -23,11 +22,9 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network. \ vyos.argspec.l3_interfaces.l3_interfaces import L3_interfacesArgs - class L3_interfacesFacts(object): """ The vyos l3_interfaces fact class """ - def __init__(self, module, subspec='config', options='options'): self._module = module self.argument_spec = L3_interfacesArgs.argument_spec @@ -55,7 +52,9 @@ class L3_interfacesFacts(object): # operate on a collection of resource x objs = [] - interface_names = re.findall(r'set interfaces (?:ethernet|bonding|vti|vxlan) (?:\'*)(\S+)(?:\'*)', data, re.M) + interface_names = re.findall( + r'set interfaces (?:ethernet|bonding|vti|vxlan) (?:\'*)(\S+)(?:\'*)', + data, re.M) if interface_names: for interface in set(interface_names): intf_regex = r' %s .+$' % interface @@ -69,7 +68,8 @@ class L3_interfacesFacts(object): facts = {} if objs: facts['l3_interfaces'] = [] - params = utils.validate_config(self.argument_spec, {'config': objs}) + params = utils.validate_config(self.argument_spec, + {'config': objs}) for cfg in params['config']: facts['l3_interfaces'].append(utils.remove_empties(cfg)) diff --git a/plugins/module_utils/network/vyos/facts/legacy/base.py b/plugins/module_utils/network/vyos/facts/legacy/base.py index cdf0cce..33d094b 100644 --- a/plugins/module_utils/network/vyos/facts/legacy/base.py +++ b/plugins/module_utils/network/vyos/facts/legacy/base.py @@ -12,7 +12,6 @@ based on the configuration. from __future__ import absolute_import, division, print_function __metaclass__ = type - import platform import re from ansible.module_utils. \ @@ -114,9 +113,7 @@ class Neighbors(LegacyFactsBase): all_neighbors = self.responses[0] if 'LLDP not configured' not in all_neighbors: - neighbors = self.parse( - self.responses[1] - ) + neighbors = self.parse(self.responses[1]) self.facts['neighbors'] = self.parse_neighbors(neighbors) def parse(self, data): diff --git a/plugins/module_utils/network/vyos/utils/utils.py b/plugins/module_utils/network/vyos/utils/utils.py index f59c24f..960d268 100644 --- a/plugins/module_utils/network/vyos/utils/utils.py +++ b/plugins/module_utils/network/vyos/utils/utils.py @@ -5,7 +5,6 @@ # utils - from __future__ import absolute_import, division, print_function __metaclass__ = type diff --git a/plugins/module_utils/network/vyos/vyos.py b/plugins/module_utils/network/vyos/vyos.py index 2aa7b49..ee15101 100644 --- a/plugins/module_utils/network/vyos/vyos.py +++ b/plugins/module_utils/network/vyos/vyos.py @@ -34,14 +34,18 @@ from ansible.module_utils.connection import Connection, ConnectionError _DEVICE_CONFIGS = {} vyos_provider_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'), - - 'timeout': dict(type='int'), + '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'), + 'timeout': + dict(type='int'), } vyos_argument_spec = { 'provider': dict(type='dict', options=vyos_provider_spec), @@ -49,11 +53,9 @@ vyos_argument_spec = { vyos_top_spec = { 'host': dict(removed_in_version=2.9), 'port': dict(removed_in_version=2.9, type='int'), - 'username': dict(removed_in_version=2.9), 'password': dict(removed_in_version=2.9, no_log=True), 'ssh_keyfile': dict(removed_in_version=2.9, type='path'), - 'timeout': dict(removed_in_version=2.9, type='int'), } vyos_argument_spec.update(vyos_top_spec) @@ -110,7 +112,8 @@ def get_config(module, flags=None, format=None): def run_commands(module, commands, check_rc=True): connection = get_connection(module) try: - response = connection.run_commands(commands=commands, check_rc=check_rc) + response = connection.run_commands(commands=commands, + check_rc=check_rc) except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors='surrogate_then_replace')) return response @@ -120,7 +123,9 @@ def load_config(module, commands, commit=False, comment=None): connection = get_connection(module) try: - response = connection.edit_config(candidate=commands, commit=commit, comment=comment) + response = connection.edit_config(candidate=commands, + commit=commit, + comment=comment) except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors='surrogate_then_replace')) |