Beispiel #1
0
 def __init__(self, module):
     super(Bgp_af, self).__init__(
         empty_fact_val={},
         facts_module=Facts(module),
         module=module,
         resource="bgp_address_family",
         tmplt=Bgp_afTemplate(),
     )
     self.parsers = [
         "router",
         "address_family",
         "bgp_params_additional_paths",
         "bgp_params.nexthop_address_family",
         "bgp_params.nexthop_unchanged",
         "bgp_params.redistribute_internal",
         "bgp_params.route",
         "graceful_restart",
         "neighbor.activate",
         "neighbor.additional_paths",
         "neighbor.default_originate",
         "neighbor.graceful_restart",
         "neighbor.next_hop_unchanged",
         "neighbor.next_hop_address_family",
         "neighbor.prefix_list",
         "neighbor.route_map",
         "neighbor.weight",
         "neighbor.encapsulation",
         "network",
         "redistribute",
         "route_target",
     ]
Beispiel #2
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for Bgp_af network resource

        :param connection: the device connection
        :param ansible_facts: Facts dictionary
        :param data: previously collected conf

        :rtype: dictionary
        :returns: facts
        """
        facts = {}
        objs = []

        if not data:
            data = self.get_config(connection)

        # remove global configs from bgp_address_family
        bgp_af_config = []
        vrf_set = ""
        start = False
        for bgp_line in data.splitlines():
            if "router bgp" in bgp_line:
                bgp_af_config.append(bgp_line)
            vrf_present = re.search(r"vrf\s\S+", bgp_line)
            if vrf_present:
                vrf_set = vrf_present.group(0)
            if start:
                bgp_af_config.append(bgp_line)
            if "address-family" in bgp_line:
                af_line = vrf_set + bgp_line
                bgp_af_config.append(af_line)
                start = True
            if start and "!" in bgp_line:
                start = False

        # parse native config using the Bgp_af template
        bgp_af_parser = Bgp_afTemplate(lines=bgp_af_config)
        objs = bgp_af_parser.parse()
        if objs:
            if "address_family" in objs:
                objs["address_family"] = list(objs["address_family"].values())
                for af in objs["address_family"]:
                    if "neighbor" in af:
                        af["neighbor"] = list(af["neighbor"].values())
                    if "network" in af:
                        af["network"] = list(af["network"].values())
                        af["network"] = sorted(
                            af["network"], key=lambda k: k["address"]
                        )

        ansible_facts["ansible_network_resources"].pop(
            "bgp_address_family", None
        )

        params = utils.remove_empties(
            utils.validate_config(self.argument_spec, {"config": objs})
        )

        facts["bgp_address_family"] = params.get("config", [])
        ansible_facts["ansible_network_resources"].update(facts)

        return ansible_facts