def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get_config(flags=['| grep interfaces']) objs = [] 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("'") cfg = findall(intf_regex, data, M) obj = self.render_config(cfg) obj['name'] = interface.strip("'") if obj: objs.append(obj) facts = {} if objs: facts['interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for l3_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected configuration :rtype: dictionary :returns: facts """ if not data: data = connection.get('show running-config | section ^interface') # split the config into instances of the resource resource_delim = 'interface' find_pattern = r'(?:^|\n)%s.*?(?=(?:^|\n)%s|$)' % (resource_delim, resource_delim) resources = [ p.strip() for p in re.findall(find_pattern, data, re.DOTALL) ] objs = [] for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['l3_interfaces'] = [ utils.remove_empties(cfg) for cfg in params['config'] ] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_global :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get_config() objs = {} lldp_output = findall(r'^set service lldp (\S+)', data, M) if lldp_output: for item in set(lldp_output): lldp_regex = r' %s .+$' % item cfg = findall(lldp_regex, data, M) obj = self.render_config(cfg) if obj: objs.update(obj) lldp_service = findall(r"^set service (lldp)?('lldp')", data, M) if lldp_service or lldp_output: lldp_obj = {} lldp_obj['enable'] = True objs.update(lldp_obj) facts = {} params = utils.validate_config(self.argument_spec, {'config': objs}) facts['lldp_global'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lacp :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected configuration :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) # split the config into instances of the resource resource_delim = 'lacp' find_pattern = r'(?:^|\n)%s.*?(?=(?:^|\n)%s|$)' % (resource_delim, resource_delim) resources = [p.strip() for p in re.findall(find_pattern, data, re.DOTALL)] objs = {} for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj: objs.update(obj) ansible_facts['ansible_network_resources'].pop('lacp', None) facts = {'lacp': {}} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['lacp'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for vlans :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if connection: pass objs = [] mtu_objs = [] remote_objs = [] final_objs = [] if not data: data = connection.get('show vlan') # operate on a collection of resource x config = data.split('\n') # Get individual vlan configs separately vlan_info = '' for conf in config: if 'Name' in conf: vlan_info = 'Name' elif 'Type' in conf: vlan_info = 'Type' elif 'Remote' in conf: vlan_info = 'Remote' if conf and ' ' not in filter(None, conf.split('-')): obj = self.render_config(self.generated_spec, conf, vlan_info) if 'mtu' in obj: mtu_objs.append(obj) elif 'remote_span' in obj: remote_objs = obj elif obj: objs.append(obj) # Appending MTU value to the retrieved dictionary for o, m in zip(objs, mtu_objs): o.update(m) final_objs.append(o) # Appending Remote Span value to related VLAN: if remote_objs: if remote_objs.get('remote_span'): for each in remote_objs.get('remote_span'): for every in final_objs: if each == every.get('vlan_id'): every.update({'remote_span': True}) break facts = {} if final_objs: facts['vlans'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['vlans'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_global :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = dict() if not data: data = connection.get('show running-config | section ^lldp') # operate on a collection of resource x config = data.split('\n') for conf in config: if conf: obj = self.render_config(self.generated_spec, conf) if obj: objs.update(obj) facts = {} if objs: params = utils.validate_config( self.argument_spec, {'config': utils.remove_empties(objs)}) facts['lldp_global'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for l2_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: request = [{ "path": "/rest/restconf/data/openconfig-interfaces:interfaces", "method": "GET" }] data = send_requests(self._module, requests=request) objs = [] if data: for d in data[0]["openconfig-interfaces:interfaces"]["interface"]: obj = self.render_config(self.generated_spec, d) if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('l2_interfaces', None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['l2_interfaces'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if connection: pass objs = [] if not data: data = connection.get('show lldp interface') # operate on a collection of resource x config = data.split('\n\n') for conf in config: if conf: obj = self.render_config(self.generated_spec, conf) if obj: objs.append(obj) facts = {} if objs: facts['lldp_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['lldp_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get_config(flags='interface') interfaces = data.split('interface ') objs = [] for interface in interfaces: obj = self.render_config(self.generated_spec, interface) if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('lldp_interfaces', None) facts = {} if objs: facts['lldp_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['lldp_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_global :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get('show running-config | section lldp') obj = {} if data: obj.update(self.render_config(self.generated_spec, data)) ansible_facts['ansible_network_resources'].pop('lldp_global', None) facts = {} if obj: params = utils.validate_config(self.argument_spec, {'config': obj}) facts['lldp_global'] = utils.remove_empties(params['config']) else: facts['lldp_global'] = {} ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lacp :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get_config(flags='lacp') obj = {} if data: lacp_obj = self.render_config(self.generated_spec, data) if lacp_obj: obj = lacp_obj ansible_facts['ansible_network_resources'].pop('lacp', None) facts = {} params = utils.validate_config(self.argument_spec, {'config': obj}) facts['lacp'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lldp_global :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: request = { "path": "/rest/restconf/data/openconfig-lldp:lldp/config/", "method": "GET", } data = send_requests(self._module, request) obj = {} if data: lldp_obj = self.render_config(self.generated_spec, data[0]) if lldp_obj: obj = lldp_obj ansible_facts['ansible_network_resources'].pop('lldp_global', None) facts = {} params = utils.validate_config(self.argument_spec, {'config': obj}) facts['lldp_global'] = params['config'] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for l2_interfaces :param connection: the device connection :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] if not data: data = connection.get('show running-config | section ^interface') config = data.split('interface ') for conf in config: conf = conf.strip() if conf: obj = self.render_config(self.generated_spec, conf) if obj and len(obj.keys()) > 1: objs.append(obj) ansible_facts['ansible_network_resources'].pop('l2_interfaces', None) facts = {} if objs: facts['l2_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['l2_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lag_interfaces :param module: the module instance :param connection: the device connection :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get_config() objs = [] lag_names = findall(r'^set interfaces bonding (\S+)', data, M) if lag_names: for lag in set(lag_names): lag_regex = r' %s .+$' % lag cfg = findall(lag_regex, data, M) obj = self.render_config(cfg) output = connection.run_commands( ['show interfaces bonding ' + lag + ' slaves']) lines = output[0].splitlines() members = [] member = {} if len(lines) > 1: for line in lines[2:]: splitted_line = line.split() if len(splitted_line) > 1: member['member'] = splitted_line[0] members.append(member) else: members = [] member = {} obj['name'] = lag.strip("'") if members: obj['members'] = members if obj: objs.append(obj) facts = {} if objs: facts['lag_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['lag_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param data: previously collected configuration as lxml ElementTree root instance or valid xml sting :rtype: dictionary :returns: facts """ if not HAS_LXML: self._module.fail_json(msg='lxml is not installed.') if not data: config_filter = """ <configuration> <chassis> <aggregated-devices> <ethernet> <lacp> </lacp> </ethernet> </aggregated-devices> </chassis> </configuration> """ data = get_resource_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring(to_bytes(data, errors='surrogate_then_replace')) facts = {} config = deepcopy(self.generated_spec) resources = data.xpath('configuration/chassis/aggregated-devices/ethernet/lacp') if resources: lacp_root = resources[0] config['system_priority'] = utils.get_xml_conf_arg(lacp_root, 'system-priority') if utils.get_xml_conf_arg(lacp_root, 'link-protection/non-revertive', data='tag'): config['link_protection'] = "non-revertive" elif utils.get_xml_conf_arg(lacp_root, 'link-protection'): config['link_protection'] = "revertive" params = utils.validate_config(self.argument_spec, {'config': utils.remove_empties(config)}) facts['lacp'] = {} facts['lacp'].update(utils.remove_empties(params['config'])) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param data: previously collected configuration as lxml ElementTree root instance or valid xml sting :rtype: dictionary :returns: facts """ if not HAS_LXML: self._module.fail_json(msg='lxml is not installed.') if not data: config_filter = """ <configuration> <protocols> <lldp> </lldp> </protocols> </configuration> """ data = get_resource_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring( to_bytes(data, errors='surrogate_then_replace')) facts = {} config = deepcopy(self.generated_spec) resources = data.xpath('configuration/protocols/lldp') if resources: lldp_root = resources[0] config['address'] = utils.get_xml_conf_arg(lldp_root, 'management-address') config['interval'] = utils.get_xml_conf_arg( lldp_root, 'advertisement-interval') config['transmit_delay'] = utils.get_xml_conf_arg( lldp_root, 'transmit-delay') config['hold_multiplier'] = utils.get_xml_conf_arg( lldp_root, 'hold-multiplier') if utils.get_xml_conf_arg(lldp_root, 'disable', data='tag'): config['enable'] = False params = utils.validate_config( self.argument_spec, {'config': utils.remove_empties(config)}) facts['lldp_global'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] if not data: data = connection.get( "show running-config all | incl 'system default switchport'") data += connection.get('show running-config | section ^interface') # Collect device defaults & per-intf defaults self.render_system_defaults(data) intf_defs = {'sysdefs': self.sysdefs} config = data.split('interface ') default_interfaces = [] for conf in config: conf = conf.strip() if conf: obj = self.render_config(self.generated_spec, conf) if obj: intf_defs[obj['name']] = obj.pop('enabled_def', None) if len(obj.keys()) > 1: objs.append(obj) elif len(obj.keys()) == 1: # Existing-but-default interfaces are not included in the # objs list; however a list of default interfaces is # necessary to prevent idempotence issues and to help # with virtual interfaces that haven't been created yet. default_interfaces.append(obj['name']) ansible_facts['ansible_network_resources'].pop('interfaces', None) facts = {} facts['interfaces'] = [] if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) ansible_facts['ansible_network_resources'][ 'default_interfaces'] = default_interfaces ansible_facts['intf_defs'] = intf_defs return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for static_routes :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not HAS_LXML: self._module.fail_json(msg='lxml is not installed.') if not HAS_XMLTODICT: self._module.fail_json(msg='xmltodict is not installed.') if not data: config_filter = """ <configuration> <routing-instances/> <routing-options/> </configuration> """ data = connection.get_configuration(filter=config_filter) if isinstance(data, string_types): data = etree.fromstring( to_bytes(data, errors='surrogate_then_replace')) resources = data.xpath('configuration/routing-options') vrf_resources = data.xpath('configuration/routing-instances') resources.extend(vrf_resources) objs = [] for resource in resources: if resource is not None: xml = self._get_xml_dict(resource) obj = self.render_config(self.generated_spec, xml) if obj: objs.append(obj) facts = {} if objs: facts['static_routes'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['static_routes'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param data: previously collected configuration as lxml ElementTree root instance or valid xml sting :rtype: dictionary :returns: facts """ if not HAS_LXML: self._module.fail_json(msg='lxml is not installed.') if not data: config_filter = """ <configuration> <protocols> <lldp> <interface> </interface> </lldp> </protocols> </configuration> """ data = get_resource_config(connection, config_filter=config_filter) if isinstance(data, string_types): data = etree.fromstring( to_bytes(data, errors='surrogate_then_replace')) self._resources = data.xpath('configuration/protocols/lldp/interface') objs = [] for resource in self._resources: if resource is not None: obj = self.render_config(self.generated_spec, resource) if obj: objs.append(obj) facts = {} if objs: facts['lldp_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['lldp_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lag_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected configuration :rtype: dictionary :returns: facts """ if not data: data = connection.get('show running-config | section ^interface') # split the config into instances of the resource resource_delim = 'interface' find_pattern = r'(?:^|\n)%s.*?(?=(?:^|\n)%s|$)' % (resource_delim, resource_delim) resources = [ p.strip() for p in re.findall(find_pattern, data, re.DOTALL) ] objs = {} for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj: group_name = obj['name'] if group_name in objs and "members" in obj: config = objs[group_name] if "members" not in config: config["members"] = [] objs[group_name]['members'].extend(obj['members']) else: objs[group_name] = obj objs = list(objs.values()) facts = {'lag_interfaces': []} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['lag_interfaces'] = [ utils.remove_empties(cfg) for cfg in params['config'] ] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lacp :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = connection.get("show running-config | include lacp") resources = data.strip() objs = self.render_config(self.generated_spec, resources) ansible_facts['ansible_network_resources'].pop('lacp', None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['lacp'] = utils.remove_empties(params['config']) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] if not data: data = connection.get('show running-config | section ^interface') # operate on a collection of resource x config = data.split('interface ') for conf in config: if conf: obj = self.render_config(self.generated_spec, conf) if obj: if not obj.get('members'): obj.update({'members': []}) objs.append(obj) # for appending members configured with same channel-group for each in range(len(objs)): if each < (len(objs) - 1): if objs[each]['name'] == objs[each + 1]['name']: objs[each]['members'].append(objs[each + 1]['members'][0]) del objs[each + 1] facts = {} if objs: facts['lag_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['lag_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts
def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for bfd_interfaces :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ objs = [] if not data: data = connection.get( "show running-config | section '^interface|^feature bfd'") # Some of the bfd attributes if 'feature bfd' in data.split('\n'): resources = data.split('interface ') resources.pop(0) else: resources = [] for resource in resources: if resource: obj = self.render_config(self.generated_spec, resource) if obj and len(obj.keys()) > 1: objs.append(obj) ansible_facts['ansible_network_resources'].pop('bfd_interfaces', None) facts = {} if objs: facts['bfd_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['bfd_interfaces'].append(utils.remove_empties(cfg)) ansible_facts['ansible_network_resources'].update(facts) return ansible_facts