示例#1
0
    def populate_facts(self, connection, assible_facts, data=None):
        """ Populate the facts for interfaces
        :param connection: the device connection
        :param assible_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))

        assible_facts["assible_network_resources"].update(facts)
        return assible_facts
示例#2
0
    def populate_facts(self, connection, assible_facts, data=None):
        """ Populate the facts for lldp_global
        :param connection: the device connection
        :param assible_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"])

        assible_facts["assible_network_resources"].update(facts)

        return assible_facts
示例#3
0
 def parse_attribs(self, attribs, conf):
     config = {}
     for item in attribs:
         value = utils.parse_conf_arg(conf, item)
         if value:
             config[item] = value.strip("'")
         else:
             config[item] = None
     return utils.remove_empties(config)
示例#4
0
    def populate_facts(self, connection, assible_facts, data=None):
        """ Populate the facts for firewall_rules
        :param connection: the device connection
        :param assible_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)
        # split the config into instances of the resource
        objs = []
        v6_rules = findall(r"^set firewall ipv6-name (?:\'*)(\S+)(?:\'*)",
                           data, M)
        v4_rules = findall(r"^set firewall name (?:\'*)(\S+)(?:\'*)", data, M)
        if v6_rules:
            config = self.get_rules(data, v6_rules, type="ipv6")
            if config:
                config = utils.remove_empties(config)
                objs.append(config)
        if v4_rules:
            config = self.get_rules(data, v4_rules, type="ipv4")
            if config:
                config = utils.remove_empties(config)
                objs.append(config)

        assible_facts["assible_network_resources"].pop("firewall_rules", None)
        facts = {}
        if objs:
            facts["firewall_rules"] = []
            params = utils.validate_config(self.argument_spec,
                                           {"config": objs})
            for cfg in params["config"]:
                facts["firewall_rules"].append(utils.remove_empties(cfg))

        assible_facts["assible_network_resources"].update(facts)
        return assible_facts
示例#5
0
    def populate_facts(self, connection, assible_facts, data=None):
        """ Populate the facts for static_routes
        :param connection: the device connection
        :param assible_facts: Facts dictionary
        :param data: previously collected conf
        :rtype: dictionary
        :returns: facts
        """
        if not data:
            data = self.get_device_data(connection)
            # typically data is populated from the current device configuration
            # data = connection.get('show running-config | section ^interface')
            # using mock data instead
        objs = []
        r_v4 = []
        r_v6 = []
        af = []
        static_routes = findall(r"set protocols static route(6)? (\S+)", data,
                                M)
        if static_routes:
            for route in set(static_routes):
                route_regex = r" %s .+$" % route[1]
                cfg = findall(route_regex, data, M)
                sr = self.render_config(cfg)
                sr["dest"] = route[1].strip("'")
                afi = self.get_afi(sr["dest"])
                if afi == "ipv4":
                    r_v4.append(sr)
                else:
                    r_v6.append(sr)
            if r_v4:
                afi_v4 = {"afi": "ipv4", "routes": r_v4}
                af.append(afi_v4)
            if r_v6:
                afi_v6 = {"afi": "ipv6", "routes": r_v6}
                af.append(afi_v6)
            config = {"address_families": af}
            if config:
                objs.append(config)

        assible_facts["assible_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))

        assible_facts["assible_network_resources"].update(facts)
        return assible_facts
示例#6
0
    def render_config(self, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        vif_conf = "\n".join(filter(lambda x: ("vif" in x), conf))
        eth_conf = "\n".join(filter(lambda x: ("vif" not in x), conf))
        config = self.parse_attribs(eth_conf)
        config["vifs"] = self.parse_vifs(vif_conf)

        return utils.remove_empties(config)
示例#7
0
    def parse_attribs(self, attribs, conf):
        config = {}
        for item in attribs:
            value = utils.parse_conf_arg(conf, item)
            if value and item == "mtu":
                config[item] = int(value.strip("'"))
            elif value:
                config[item] = value.strip("'")
            else:
                config[item] = None
        if "disable" in conf:
            config["enabled"] = False
        else:
            config["enabled"] = True

        return utils.remove_empties(config)
示例#8
0
 def render_config(self, conf):
     """
      Render config as dictionary structure and delete keys
        from spec for null values
      :param spec: The facts tree, generated from the argspec
      :param conf: The configuration
      :rtype: dictionary
      :returns: The generated config
      """
     protocol_conf = "\n".join(
         filter(lambda x: ("legacy-protocols" in x), conf))
     att_conf = "\n".join(
         filter(lambda x: ("legacy-protocols" not in x), conf))
     config = self.parse_attribs(["snmp", "address"], att_conf)
     config["legacy_protocols"] = self.parse_protocols(protocol_conf)
     return utils.remove_empties(config)
示例#9
0
    def render_config(self, conf):
        """
        Render config as dictionary structure and delete keys
          from spec for null values

        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = {}
        location = {}

        civic_conf = "\n".join(filter(lambda x: ("civic-based" in x), conf))
        elin_conf = "\n".join(filter(lambda x: ("elin" in x), conf))
        coordinate_conf = "\n".join(
            filter(lambda x: ("coordinate-based" in x), conf))
        disable = "\n".join(filter(lambda x: ("disable" in x), conf))

        coordinate_based_conf = self.parse_attribs(
            ["altitude", "datum", "longitude", "latitude"], coordinate_conf)
        elin_based_conf = self.parse_lldp_elin_based(elin_conf)
        civic_based_conf = self.parse_lldp_civic_based(civic_conf)
        if disable:
            config["enable"] = False
        if coordinate_conf:
            location["coordinate_based"] = coordinate_based_conf
            config["location"] = location
        elif civic_based_conf:
            location["civic_based"] = civic_based_conf
            config["location"] = location
        elif elin_conf:
            location["elin"] = elin_based_conf
            config["location"] = location

        return utils.remove_empties(config)
示例#10
0
    def parse_attribs(self, conf):
        config = {}
        ipaddrs = re.findall(r"address (\S+)", conf, re.M)
        config["ipv4"] = []
        config["ipv6"] = []

        for item in ipaddrs:
            item = item.strip("'")
            if item == "dhcp":
                config["ipv4"].append({"address": item})
            elif item == "dhcpv6":
                config["ipv6"].append({"address": item})
            else:
                ip_version = ipaddress.ip_address(item.split("/")[0]).version
                if ip_version == 4:
                    config["ipv4"].append({"address": item})
                else:
                    config["ipv6"].append({"address": item})

        for key, value in iteritems(config):
            if value == []:
                config[key] = None

        return utils.remove_empties(config)