Beispiel #1
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for ospfv2
        :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_ospfv2_data(connection)

        ipv4 = {"processes": []}
        rmmod = NetworkTemplate(
            lines=data.splitlines(), tmplt=Ospfv2Template()
        )
        current = rmmod.parse()

        # convert some of the dicts to lists
        for key, sortv in [("processes", "process_id")]:
            if key in current and current[key]:
                current[key] = current[key].values()
                current[key] = sorted(
                    current[key], key=lambda k, sk=sortv: k[sk]
                )

        for process in current.get("processes", []):
            if "passive_interfaces" in process and process[
                "passive_interfaces"
            ].get("default"):
                if process["passive_interfaces"].get("interface"):
                    temp = []
                    for each in process["passive_interfaces"]["interface"][
                        "name"
                    ]:
                        if each:
                            temp.append(each)
                    process["passive_interfaces"]["interface"]["name"] = temp
            if "areas" in process:
                process["areas"] = list(process["areas"].values())
                process["areas"] = sorted(
                    process["areas"], key=lambda k, sk="area_id": k[sk]
                )
                for area in process["areas"]:
                    if "filters" in area:
                        area["filters"].sort()
            ipv4["processes"].append(process)

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

            facts["ospfv2"] = params["config"]

            ansible_facts["ansible_network_resources"].update(facts)
        return ansible_facts
Beispiel #2
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for OGs
        :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_og_data(connection)

        rmmod = NetworkTemplate(lines=data.splitlines(), tmplt=OGsTemplate())
        current = rmmod.parse()

        ogs = []
        object_groups = {
            "icmp-type": "icmp_type",
            "network": "network_object",
            "protocol": "protocol_object",
            "security": "security_group",
            "service": "service_object",
            "user": "******",
        }
        if current.get("ogs"):
            for k, v in iteritems(current.get("ogs")):
                obj_gp = {}
                config_dict = {}
                config_dict["object_type"] = k
                config_dict["object_groups"] = []
                for each in iteritems(v):
                    obj_gp["name"] = each[1].pop("name")
                    each[1].pop("object_type")
                    if each[1].get("description"):
                        obj_gp["description"] = each[1].pop("description")
                    if each[1].get("group_object"):
                        obj_gp["group_object"] = each[1].pop("group_object")
                    obj_gp[object_groups.get(k)] = each[1]
                    config_dict["object_groups"].append(obj_gp)
                    obj_gp = {}
                config_dict["object_groups"] = sorted(
                    config_dict["object_groups"],
                    key=lambda k, sk="name": k[sk],
                )
                ogs.append(config_dict)
        # sort the object group list of dict by object_type
        ogs = sorted(ogs, key=lambda i: i["object_type"])
        facts = {}
        if current:
            params = utils.validate_config(self.argument_spec, {"config": ogs})
            params = utils.remove_empties(params)

            facts["ogs"] = params["config"]

            ansible_facts["ansible_network_resources"].update(facts)
        return ansible_facts
Beispiel #3
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_acls_config(connection)

        rmmod = NetworkTemplate(lines=data.splitlines(), tmplt=AclsTemplate())
        current = rmmod.parse()
        acls = list()
        if current.get("acls"):
            for key, val in iteritems(current.get("acls")):
                if val.get("name") == "cached":
                    continue
                for each in val.get("aces"):
                    if "protocol_number" in each:
                        each["protocol_options"] = {
                            "protocol_number": each["protocol_number"]
                        }
                        del each["protocol_number"]
                    if "icmp_icmp6_protocol" in each and each.get("protocol"):
                        each["protocol_options"] = {
                            each.get("protocol"): {
                                each["icmp_icmp6_protocol"].replace("-", "_"):
                                True
                            }
                        }
                        del each["icmp_icmp6_protocol"]
                    elif (each.get("protocol")
                          and each.get("protocol") != "icmp"
                          and each.get("protocol") != "icmp6"):
                        each["protocol_options"] = {each.get("protocol"): True}
                acls.append(val)
        facts = {}
        params = {}
        if acls:
            params = utils.validate_config(self.argument_spec,
                                           {"config": {
                                               "acls": acls
                                           }})
            params = utils.remove_empties(params)
            facts["acls"] = params["config"]

        ansible_facts["ansible_network_resources"].update(facts)
        return ansible_facts
Beispiel #4
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for ospfv3
        :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_ospfv3_data(connection)

        ipv4 = {"processes": []}
        rmmod = NetworkTemplate(lines=data.splitlines(),
                                tmplt=Ospfv3Template())
        current = self.parse(rmmod)
        address_family = self.parse_for_address_family(current)
        if address_family:
            for k, v in iteritems(current["processes"]):
                temp = address_family.pop(k)
                v.update({"address_family": temp})
        # convert some of the dicts to lists
        for key, sortv in [("processes", "process_id")]:
            if key in current and current[key]:
                current[key] = current[key].values()
                current[key] = sorted(current[key],
                                      key=lambda k, sk=sortv: k[sk])

        for process in current.get("processes", []):
            if "areas" in process:
                process["areas"] = list(process["areas"].values())
                process["areas"] = sorted(process["areas"],
                                          key=lambda k, sk="area_id": k[sk])
                for area in process["areas"]:
                    if "filters" in area:
                        area["filters"].sort()
            if "address_family" in process:
                for each in process["address_family"]:
                    if "areas" in each:
                        each["areas"] = list(each["areas"].values())
                        each["areas"] = sorted(
                            each["areas"], key=lambda k, sk="area_id": k[sk])
                        for area in each["areas"]:
                            if "filters" in area:
                                area["filters"].sort()
            ipv4["processes"].append(process)

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

            facts["ospfv3"] = params["config"]

            ansible_facts["ansible_network_resources"].update(facts)
        return ansible_facts
Beispiel #5
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)

        rmmod = NetworkTemplate(lines=data.splitlines(), tmplt=AclsTemplate())
        current = rmmod.parse()

        temp_v4 = []
        temp_v6 = []
        if current.get("acls"):
            for k, v in iteritems(current.get("acls")):
                if v.get("afi") == "ipv4":
                    del v["afi"]
                    temp_v4.append(v)
                elif v.get("afi") == "ipv6":
                    del v["afi"]
                    temp_v6.append(v)
            temp_v4 = sorted(temp_v4, key=lambda i: str(i["name"]))
            temp_v6 = sorted(temp_v6, key=lambda i: str(i["name"]))
            for each in temp_v4:
                for each_ace in each.get("aces"):
                    if each["acl_type"] == "standard":
                        each_ace["source"] = each_ace.pop("std_source")
                    if each_ace.get("icmp_igmp_tcp_protocol"):
                        each_ace["protocol_options"] = {
                            each_ace["protocol"]: {
                                each_ace.pop("icmp_igmp_tcp_protocol").replace(
                                    "-", "_"):
                                True
                            }
                        }
                    if each_ace.get("std_source") == {}:
                        del each_ace["std_source"]
            for each in temp_v6:
                for each_ace in each.get("aces"):
                    if each_ace.get("std_source") == {}:
                        del each_ace["std_source"]
                    if each_ace.get("icmp_igmp_tcp_protocol"):
                        each_ace["protocol_options"] = {
                            each_ace["protocol"]: {
                                each_ace.pop("icmp_igmp_tcp_protocol").replace(
                                    "-", "_"):
                                True
                            }
                        }

        objs = []
        if temp_v4:
            objs.append({"afi": "ipv4", "acls": temp_v4})
        if temp_v6:
            objs.append({"afi": "ipv6", "acls": temp_v6})
        # objs['ipv6'] = {'acls': temp_v6}
        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
Beispiel #6
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
        """

        if not data:
            data = self.get_ospfv3_data(connection)
        end_flag, end_mark, count, v_read = 0, 0, 0, False
        areas, config_commands = [], []
        area_str, process, curr_process = "", "", ""
        data = data.splitlines()
        for line in data:
            if (
                line.startswith("router ospfv3")
                and curr_process != ""
                and curr_process != line
            ):
                end_mark, count, end_flag, area_str = 0, 0, 0, ""
            if (
                end_mark == 0
                and count == 0
                and line.startswith("router ospfv3")
            ):
                curr_process = line
                process = re.sub("\n", "", line)
                count += 1
                config_commands.append(process)
            else:
                if line.startswith(" area") or line.startswith(" vrf"):
                    area_str = process + re.sub("\n", "", line)
                    config_commands.append(area_str.replace("  ", " "))
                    end_flag += 1
                elif line.startswith("  virtual-link"):
                    virtual_str = area_str + re.sub("\n", "", line)
                    config_commands.append(virtual_str.replace("  ", " "))
                    v_read = True
                elif v_read:
                    if "!" not in line:
                        command = virtual_str.replace("  ", " ") + re.sub(
                            "\n", "", line
                        )
                        config_commands.append(command.replace("   ", " "))
                    else:
                        v_read = False
                elif end_flag > 0 and "!" not in line:
                    command = area_str + re.sub("\n", "", line)
                    config_commands.append(command.replace("  ", " "))
                elif "!" in line:
                    end_flag = 0
                    end_mark += 1
                    if end_mark == 3:
                        end_mark, count = 0, 0
                    area_str = ""
                else:
                    command = process + line
                    command.replace("  ", " ")
                    config_commands.append(re.sub("\n", "", command))
                    areas.append(re.sub("\n", "", command))
        data = config_commands
        ipv4 = {"processes": []}
        rmmod = NetworkTemplate(
            lines=data, tmplt=Ospfv3Template(), module=self._module
        )
        current = rmmod.parse()

        # convert some of the dicts to lists
        for key, sortv in [("processes", "process_id")]:
            if key in current and current[key]:
                current[key] = current[key].values()
                current[key] = sorted(
                    current[key], key=lambda k, sk=sortv: k[sk]
                )

        for process in current.get("processes", []):
            if "areas" in process:
                process["areas"] = list(process["areas"].values())
                process["areas"] = sorted(
                    process["areas"], key=lambda k, sk="area_id": k[sk]
                )
                for area in process["areas"]:
                    if "ranges" in area:
                        area["ranges"] = sorted(
                            area["ranges"], key=lambda k, s="ranges": k[s]
                        )
                    if "virtual_link" in area:
                        area["virtual_link"] = list(
                            area["virtual_link"].values()
                        )
                        area["virtual_link"] = sorted(
                            area["virtual_link"], key=lambda k, sk="id": k[sk]
                        )
            ipv4["processes"].append(process)

        ansible_facts["ansible_network_resources"].pop("ospfv3", None)
        facts = {}
        if current:
            params = rmmod.validate_config(
                self.argument_spec, {"config": ipv4}, redact=True
            )
            params = utils.remove_empties(params)

            facts["ospfv3"] = params["config"]

            ansible_facts["ansible_network_resources"].update(facts)
        return ansible_facts
Beispiel #7
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)

        if data:
            data = self.sanitize_data(data)

        rmmod = NetworkTemplate(lines=data.splitlines(), tmplt=AclsTemplate())
        current = rmmod.parse()

        temp_v4 = []
        temp_v6 = []

        if current.get("acls"):
            for k, v in iteritems(current.get("acls")):
                if v.get("afi") == "ipv4" and v.get("acl_type") in [
                        "standard",
                        "extended",
                ]:
                    del v["afi"]
                    temp_v4.append(v)
                elif v.get("afi") == "ipv6":
                    del v["afi"]
                    temp_v6.append(v)

            temp_v4 = sorted(temp_v4, key=lambda i: str(i["name"]))
            temp_v6 = sorted(temp_v6, key=lambda i: str(i["name"]))

            def process_protocol_options(each):
                for each_ace in each.get("aces"):
                    if each_ace.get("source"):
                        if len(each_ace.get("source")) == 1 and each_ace.get(
                                "source",
                            {},
                        ).get("address"):
                            each_ace["source"]["host"] = each_ace[
                                "source"].pop("address")
                        if each_ace.get("source", {}).get("address"):
                            addr = each_ace.get("source", {}).get("address")
                            if addr[-1] == ",":
                                each_ace["source"]["address"] = addr[:-1]
                    if each_ace.get("icmp_igmp_tcp_protocol"):
                        each_ace["protocol_options"] = {
                            each_ace["protocol"]: {
                                each_ace.pop("icmp_igmp_tcp_protocol").replace(
                                    "-",
                                    "_",
                                ):
                                True,
                            },
                        }
                    if each_ace.get("protocol_number"):
                        each_ace["protocol_options"] = {
                            "protocol_number": each_ace.pop("protocol_number"),
                        }

            def collect_remarks(aces):
                """makes remarks list per ace"""
                ace_entry = []
                rem = []
                for i in aces:
                    if i.get("remarks"):
                        rem.append(i.pop("remarks"))
                    else:
                        ace_entry.append(i)
                if rem:
                    ace_entry.append({"remarks": rem})
                return ace_entry

            for each in temp_v4:
                if each.get("aces"):
                    each["aces"] = collect_remarks(each.get("aces"))
                    process_protocol_options(each)

            for each in temp_v6:
                if each.get("aces"):
                    each["aces"] = collect_remarks(each.get("aces"))
                    process_protocol_options(each)

        objs = []
        if temp_v4:
            objs.append({"afi": "ipv4", "acls": temp_v4})
        if temp_v6:
            objs.append({"afi": "ipv6", "acls": temp_v6})

        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