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 | 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('interfaces', None) 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 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_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('lacp_interfaces', None) facts = {} if objs: facts['lacp_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['lacp_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_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 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') # operate on a collection of resource x config = data.split('interface ') objs = [] for conf in config: if conf: obj = self.render_config(self.generated_spec, conf) if obj: objs.append(obj) 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) 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 lag_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 | include channel-group') config = re.split('(\n |)channel-group ', data) config = list(dict.fromkeys(config)) for conf in config: if conf: obj = self.render_config(self.generated_spec, conf, connection) if obj and len(obj.keys()) > 1: objs.append(obj) ansible_facts['ansible_network_resources'].pop('lag_interfaces', None) 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 l3_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() # operate on a collection of resource x objs = [] 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 cfg = re.findall(intf_regex, data, re.M) obj = self.render_config(cfg) obj['name'] = interface.strip("'") if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('l3_interfaces', None) facts = {} if objs: facts['l3_interfaces'] = [] params = utils.validate_config(self.argument_spec, {'config': objs}) for cfg in params['config']: facts['l3_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 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 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 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 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> <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 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 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 vlans :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 ^vlan') vlans = re.split(r'(,|-)', data.split()[1]) for v in vlans: if not v.isdigit(): vlans.remove(v) config = re.split(r'(^|\n)vlan', data) for conf in config: conf = conf.strip() if conf: if conf[0] in vlans: vlans.remove(conf[0]) obj = self.render_config(self.generated_spec, conf) if obj and len(obj.keys()) > 1: objs.append(obj) for v in vlans: obj = self.render_config(self.generated_spec, v) if obj: objs.append(obj) ansible_facts['ansible_network_resources'].pop('vlans', None) facts = {} if 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 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 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 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
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 not data: data = connection.get('show running-config | section ^vlan') # split the config into instances of the resource resource_delim = 'vlan' 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.extend(obj) ansible_facts['ansible_network_resources'].pop('vlans', None) facts = {} if objs: params = utils.validate_config(self.argument_spec, {'config': objs}) facts['vlans'] = [ utils.remove_empties(cfg) for cfg in params['config'] ] ansible_facts['ansible_network_resources'].update(facts) return ansible_facts