예제 #1
0
    def gen_config(self):
        wantd = self.xform(self.want)
        haved = self.xform(self.have)

        # if state is merged, merge want onto have
        if self.state == 'merged':
            wantd = deepcopy(dict_merge(haved, wantd))
            # validate the merged data through the argument_spec
            validate_config(AclsArgs.argument_spec,
                            {"config": self.deform(deepcopy(wantd))})

        # if state is deleted, limit the have to anything in want
        # set want to nothing
        if self.state == 'deleted':
            haved = {k: v for k, v in haved.items() if k in wantd or not wantd}
            wantd = {}

        # handle everything common to want and have
        for k, want in wantd.items():
            self._compare_acl(want=want, have=haved.pop(k, {}))

        # anything left should be deleted if deleted or overridden
        if self.state in ['deleted', 'overridden']:
            for k, have in haved.items():
                self.addcmd(have, 'name', True)
예제 #2
0
    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
예제 #3
0
    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 conf
        :rtype: dictionary
        :returns: facts
        """

        if not data:
            data = connection.get_config(flags="interface")
            interfaces = data.split("interface ")

        objs = []

        for interface in interfaces:
            if interface.startswith("Bundle-Ether"):
                obj = self.render_config(self.generated_spec, interface,
                                         interfaces)
                if obj:
                    objs.append(obj)

        ansible_facts["ansible_network_resources"].pop("lag_interfaces", None)
        facts = {}

        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
예제 #4
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 conf
        :rtype: dictionary
        :returns: facts
        """
        if connection:
            pass

        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["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 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
예제 #6
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('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
예제 #7
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 data:
            data = self.get_device_data(connection)

        objs = []

        if "No such configuration" not in data:
            for entry in re.compile(r"(\s) vrf").split(data):
                obj = self.render_config(self.generated_spec, entry)
                if obj:
                    objs.append(obj)

        ansible_facts["ansible_network_resources"].pop("static_routes", None)
        facts = {}

        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
예제 #8
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for auto_save_cfg
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        if not data:
            # typically data is populated from the current device configuration
            # data = connection.get('show running-config | section ^interface')
            # using mock data instead
            data = run_commands(self._module,
                                commands={
                                    "command":
                                    "debug cfgmgr show one cfgmgr.autoSaveCfg",
                                    "output": "json"
                                })

        obj = {}
        if data:
            cfg_obj = self.render_config(self.generated_spec,
                                         data[0]['data'][0])
            if cfg_obj:
                obj = cfg_obj

        ansible_facts['ansible_network_resources'].pop('auto_save_cfg', None)
        facts = {}

        params = utils.validate_config(self.argument_spec, {'config': obj})
        facts['auto_save_cfg'] = params['config']

        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
        """
        objs = dict()
        if not data:
            data = connection.get('display lldp local')
        # 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
예제 #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
    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
예제 #12
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 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 and re.search(r'lacp', 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('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
예제 #13
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for firewall_interfaces
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        if not data:
            # typically data is populated from the current device configuration
            # data = connection.get('show running-config | section ^interface')
            # using mock data instead
            data = self.get_device_data(connection)
        objs = []
        interfaces = findall(r'^set interfaces ethernet (?:\'*)(\S+)(?:\'*)',
                             data, M)
        if interfaces:
            objs = self.get_names(data, interfaces)
        ansible_facts['ansible_network_resources'].pop('firewall_interfaces',
                                                       None)
        facts = {}
        if objs:
            facts['firewall_interfaces'] = []
            params = utils.validate_config(self.argument_spec,
                                           {'config': objs})
            for cfg in params['config']:
                facts['firewall_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 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('show running-config | section lacp')

        # 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
예제 #15
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
        """
        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 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('display current-configuration 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['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
        """
        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
예제 #18
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for twamp
        :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 oam twamp | details | nomore | display json'
            )

        objs = []
        try:
            data_dict = json.loads(data)['data']
            data_list = [
                data_dict['dmos-base:config']['oam']['dmos-twamp-app:twamp']
            ]
        except:
            pass
        else:
            for each in data_list:
                obj = self.render_config(self.generated_spec, each)
                if obj:
                    objs.append(obj)

        facts = {}
        if objs:
            params = utils.validate_config(self.argument_spec,
                                           {'config': objs})
            facts['twamp'] = 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
        """
        data = connection.get('sho run | section interface')
        rmmod = RmModuleParse(lines=data.splitlines(),
                              tmplt=InterfacesTemplate())
        current = list(rmmod.parse().values())

        for interface in current:
            if 'enable' not in interface:
                interface['enable'] = True

        ansible_facts['ansible_network_resources'].pop('interfaces', None)
        facts = {}
        if current:
            params = utils.validate_config(self.argument_spec,
                                           {'config': current})
            params = utils.remove_empties(params)

            facts['interfaces'] = params['config']

        ansible_facts['ansible_network_resources'].update(facts)
        return ansible_facts
예제 #20
0
    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
예제 #21
0
    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
예제 #22
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for user
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        if connection:  # just for linting purposes, remove
            pass

        # split the config into instances of the resource
        if not data:
            data = connection.get(
                'show running-config user | details | nomore | display keypath'
            )

        config = parse_current_config(data, self.argument_spec, 'user', {
            'user': '******',
            'alias': 'name'
        })

        facts = {}
        if config:
            params = utils.validate_config(self.argument_spec, config)
            facts['user'] = params['config']

        ansible_facts['ansible_network_resources'].update(facts)
        return ansible_facts
예제 #23
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 connection:
            pass

        if not data:
            data = connection.get('show lacp sys-id')

        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
예제 #24
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for acls
        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """

        if not data:
            data = self.get_acl_data(connection)
        # operate on a collection of resource x
        config = data.split('\n')
        spec = {'acls': list(), 'afi': None}
        if config:
            objs = self.render_config(spec, config)
        # check if rendered config list has only empty dict
        if len(objs) == 1 and objs[0] == {}:
            objs = []
        facts = {}

        if objs:
            facts['acls'] = []
            params = utils.validate_config(self.argument_spec, {'config': objs})
            for cfg in params['config']:
                facts['acls'].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
        """
        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
예제 #26
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_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
예제 #27
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
예제 #28
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
        """
        vrf_data = []
        non_vrf_data = []
        if not data:
            non_vrf_data = connection.get(
                "show running-config | include '^ip(v6)* route'")
            vrf_data = connection.get(
                "show running-config | section '^vrf context'")
            non_vrf_data = non_vrf_data.split('\n')
            vrf_data = vrf_data.split('\nvrf context')
            # as we split based on 'vrf context', it is stripped from the data except the first element
        else:
            # used for parsed state where data is from the 'running-config' key
            data = data.split('\n')
            i = 0
            while i <= (len(data) - 1):
                if 'vrf context ' in data[i]:
                    vrf_conf = data[i]
                    j = i + 1
                    while j < len(data) and 'vrf context ' not in data[j]:
                        vrf_conf += '\n' + data[j]
                        j += 1
                    i = j
                    vrf_data.append(vrf_conf)
                else:
                    non_vrf_data.append(data[i])
                    i += 1
        objs = []
        new_vrf_data = []
        for v in vrf_data:
            if re.search('\n\s*ip(v6)? route', v):
                new_vrf_data.append(v)
                # dont consider vrf if it does not have routes
        for i in range(len(new_vrf_data)):
            if not re.search('^vrf context', new_vrf_data[i]):
                new_vrf_data[i] = 'vrf context' + new_vrf_data[i]

        resources = non_vrf_data + new_vrf_data
        objs = self.render_config(self.generated_spec, resources)

        ansible_facts['ansible_network_resources'].pop('static_routes', None)
        facts = {}
        if objs:
            params = utils.validate_config(self.argument_spec,
                                           {'config': objs})
            params = utils.remove_empties(params)
            for c in params['config']:
                if c == {'vrf': '__global__'}:
                    params['config'].remove(c)
            facts['static_routes'] = params['config']
        ansible_facts['ansible_network_resources'].update(facts)
        return ansible_facts
예제 #29
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 = connection.get_configuration(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
예제 #30
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 data:
            data = self.get_device_data(connection)

        # split the config into instances of the resource
        resource_delim = 'ip.* route'
        find_pattern = r'(?:^|\n)%s.*?(?=(?:^|\n)%s|$)' % (resource_delim,
                                                           resource_delim)
        resources = [p.strip() for p in re.findall(find_pattern, data)]
        resources_without_vrf = []
        resource_vrf = {}
        for resource in resources:
            if resource and "vrf" not in resource:
                resources_without_vrf.append(resource)
            else:
                vrf = re.search(r'ip(v6)* route vrf (.*?) .*', resource)
                if vrf.group(2) in resource_vrf.keys():
                    vrf_val = resource_vrf[vrf.group(2)]
                    vrf_val.append(resource)
                    resource_vrf.update({vrf.group(2): vrf_val})
                else:
                    resource_vrf.update({vrf.group(2): [resource]})
        resources_without_vrf = ["\n".join(resources_without_vrf)]
        for vrf in resource_vrf.keys():
            vrflist = ["\n".join(resource_vrf[vrf])]
            resource_vrf.update({vrf: vrflist})
        objs = []
        for resource in resources_without_vrf:
            if resource:
                obj = self.render_config(self.generated_spec, resource)
                if obj:
                    objs.append(obj)
        for resource in resource_vrf.keys():
            if resource:
                obj = self.render_config(self.generated_spec,
                                         resource_vrf[resource][0])
                if obj:
                    objs.append(obj)
        ansible_facts['ansible_network_resources'].pop('static_routes', None)
        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