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 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
コード例 #3
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for lldp
        :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='lldp')

        obj = {}
        if data:
            lldp_obj = self.render_config(self.generated_spec, data)
            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'] = 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
        """
        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
コード例 #5
0
    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 configuration
        :rtype: dictionary
        :returns: facts
        """
        if not data:
            data = self.get_device_data(connection)

        # 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)

        ansible_facts['ansible_network_resources'].pop('lacp_interfaces', None)
        facts = {}
        if objs:
            params = utils.validate_config(self.argument_spec, {'config': objs})
            facts['lacp_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
        """
        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 hsrp_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')

        resources = data.split('interface ')
        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('hsrp_interfaces', None)
        facts = {}
        if objs:
            facts['hsrp_interfaces'] = []
            params = utils.validate_config(self.argument_spec,
                                           {'config': objs})
            for cfg in params['config']:
                facts['hsrp_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 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
コード例 #9
0
    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
コード例 #10
0
    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
コード例 #11
0
    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
        """
        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:
                    objs.append(obj)
        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
コード例 #12
0
    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
コード例 #13
0
    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
コード例 #14
0
    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
コード例 #15
0
    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
コード例 #17
0
    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 = []
        # **TBD**
        # N7K EOL/legacy image 6.2 does not support show vlan | json output.
        # If support is still required for this image then:
        # - Wrapp the json calls below in a try/except
        # - When excepted, use a helper method to parse the run_cfg_output,
        #   using the run_cfg_output data to generate compatible json data that
        #   can be read by normalize_table_data.
        if not data:
            # Use structured for most of the vlan parameter states.
            # This data is consistent across the supported nxos platforms.
            structured = self.get_device_data(connection, 'show vlan | json')

            # Raw cli config is needed for mapped_vni, which is not included in structured.
            run_cfg_output = self.get_device_data(
                connection, 'show running-config | section ^vlan')

            # Create a single dictionary from all data sources
            data = self.normalize_table_data(structured, run_cfg_output)

        for vlan in data:
            obj = self.render_config(self.generated_spec, vlan)
            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
コード例 #18
0
 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
コード例 #19
0
    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
コード例 #21
0
    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