Exemplo n.º 1
0
 def update_netmask_to_cidr(self, filter, pos, del_pos):
     netmask = filter.split(" ")
     dest = netmask[pos] + "/" + netmask_to_cidr(netmask[del_pos])
     netmask[pos] = dest
     del netmask[del_pos]
     filter_vrf = " "
     return filter_vrf.join(netmask), dest
    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 = self.get_l3_interfaces_data(connection)

        # parse native config using the l3_interfaces template
        l3_interfaces_parser = L3_interfacesTemplate(lines=data.splitlines())
        objs = l3_interfaces_parser.parse()

        objs = utils.remove_empties(objs)
        temp = []
        for k, v in iteritems(objs):
            if v.get("ipv4"):
                for each in v["ipv4"]:
                    if each.get("netmask"):
                        cidr_val = netmask_to_cidr(each["netmask"])
                        each["address"] = each["address"].strip(
                            " ") + "/" + cidr_val
                        del each["netmask"]
            temp.append(v)
        # sorting the dict by interface name
        temp = sorted(temp, key=lambda k, sk="name": k[sk])

        objs = temp
        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))
            facts["l3_interfaces"] = sorted(
                facts["l3_interfaces"],
                key=lambda k, sk="name": k[sk],
            )
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts