Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vrf_address_family module #1121

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Name | Description
[cisco.ios.ios_user](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_user_module.rst)|Module to manage the aggregates of local users.
[cisco.ios.ios_vlans](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vlans_module.rst)|Resource module to configure VLANs.
[cisco.ios.ios_vrf](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_module.rst)|Module to configure VRF definitions.
[cisco.ios.ios_vrf_address_family](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_address_family_module.rst)|Resource module to configure VRF definitions.
[cisco.ios.ios_vrf_global](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_global_module.rst)|Resource module to configure global VRF definitions.
[cisco.ios.ios_vrf_interfaces](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vrf_interfaces_module.rst)|Manages VRF configuration on interfaces.
[cisco.ios.ios_vxlan_vtep](https://github.com/ansible-collections/cisco.ios/blob/main/docs/cisco.ios.ios_vxlan_vtep_module.rst)|Resource module to configure VXLAN VTEP interface.
Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/add_vrf_address_family.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
Ruchip16 marked this conversation as resolved.
Show resolved Hide resolved
minor_changes:
- Adds a new module `ios_vrf_address_family` to manage VRFs address families on Cisco IOS devices.
7,459 changes: 7,459 additions & 0 deletions docs/cisco.ios.ios_vrf_address_family_module.rst

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ plugin_routing:
redirect: cisco.ios.vrf_interfaces
vxlan_vtep:
redirect: cisco.ios.ios_vxlan_vtep
vrf_address_family:
redirect: cisco.ios.ios_vrf_address_family
vrf_global:
redirect: cisco.ios.ios_vrf_global
requires_ansible: ">=2.15.0"
1 change: 1 addition & 0 deletions plugins/action/vrf_address_family.py
Empty file.

Large diffs are not rendered by default.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#
# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#

from __future__ import absolute_import, division, print_function


__metaclass__ = type

"""
The ios_vrf_address_family config file.
It is in this file where the current configuration (as dict)
is compared to the provided configuration (as dict) and the command set
necessary to bring the current configuration to its desired end-state is
created.
"""

from ansible.module_utils.six import iteritems
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import (
ResourceModule,
)
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import (
dict_merge,
)

from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.facts import Facts
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.vrf_address_family import (
Vrf_address_familyTemplate,
)


class Vrf_address_family(ResourceModule):
"""
The ios_vrf_address_family config class
"""

def __init__(self, module):
super(Vrf_address_family, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="vrf_address_family",
tmplt=Vrf_address_familyTemplate(),
)
self.parsers = [
"address_family",
"bgp.next_hop.loopback",
"export.map",
"import_config.map",
"export.ipv4.multicast",
"export.ipv4.unicast.allow_evpn",
"import_config.ipv4.multicast",
"import_config.ipv4.unicast",
"inter_as_hybrid.csc.next_hop",
"inter_as_hybrid.next_hop",
"mdt.auto_discovery.ingress_replication.inter_as.mdt_hello_enable",
"mdt.auto_discovery.pim.inter_as.mdt_hello_enable",
"mdt.auto_discovery.pim.inter_as.pim_tlv_announce.mdt_hello_enable",
"mdt.auto_discovery.ingress_replication.mdt_hello_enable",
"mdt.auto_discovery.pim.mdt_hello_enable",
"mdt.auto_discovery.pim.pim_tlv_announce.mdt_hello_enable",
"mdt.auto_discovery.receiver_site",
"mdt.data.ingress_replication.number",
"mdt.data.ingress_replication.immediate_switch",
"mdt.data.ingress_replication.number.immediate_switch",
"mdt.data.list.access_list",
"mdt.data.list.access_list_name",
"mdt.data.threshold",
"mdt.default_ingress_replication",
"mdt.direct",
"mdt.log_reuse",
"mdt.mode.gre",
"mdt.mtu.value",
"mdt.overlay.bgp.shared_tree_prune_delay",
"mdt.overlay.bgp.source_tree_prune_delay",
"mdt.overlay.use_bgp_spt_only",
"mdt.partitioned.ingress_replication",
"mdt.strict_rpf_interface",
"protection.local_prefixes",
"route_replicate.recursion_policy.destination",
"route_replicate.from.unicast.all.route_map",
"route_replicate.from.unicast.bgp.asn.route_map",
"route_replicate.from.unicast.connected.route_map",
"route_replicate.from.unicast.eigrp.asn.route_map",
"route_replicate.from.unicast.isis.route_map",
"route_replicate.from.unicast.mobile.route_map",
"route_replicate.from.unicast.odr.route_map",
"route_replicate.from.unicast.ospf.id.route_map",
"route_replicate.from.unicast.rip.route_map",
"route_replicate.from.unicast.static.route_map",
"route_replicate.from.vrf.vrf_name.unicast.all.route_map",
"route_replicate.from.vrf.vrf_name.unicast.bgp.asn.route_map",
"route_replicate.from.vrf.vrf_name.unicast.connected.route_map",
"route_replicate.from.vrf.vrf_name.unicast.eigrp.asn.route_map",
"route_replicate.from.vrf.vrf_name.unicast.isis.route_map",
"route_replicate.from.vrf.vrf_name.unicast.mobile.route_map",
"route_replicate.from.vrf.vrf_name.unicast.odr.route_map",
"route_replicate.from.vrf.vrf_name.unicast.ospf.id.route_map",
"route_replicate.from.vrf.vrf_name.unicast.rip.route_map",
"route_replicate.from.vrf.vrf_name.unicast.static.route_map",
"route_target.export",
"route_target.import_config",
]

def execute_module(self):
"""Execute the module

:rtype: A dictionary
:returns: The result from module execution
"""
if self.state not in ["parsed", "gathered"]:
self.generate_commands()
self.run_commands()
return self.result

def generate_commands(self):
"""Generate configuration commands to send based on
want, have and desired state.
"""
wantd = self.want
haved = self.have

wantd = self._vrf_list_to_dict(wantd)
haved = self._vrf_list_to_dict(haved)

# if state is merged, merge want onto have and then compare
if self.state == "merged":
wantd = dict_merge(haved, wantd)

# if state is deleted, empty out wantd and set haved to wantd
if self.state == "deleted":
for vrfk, vrfv in iteritems(haved):
for afk, afv in iteritems(vrfv.get("address_families", {})):
adrf = wantd.get(vrfk, {}).get("address_families", {})
if afk in adrf or not adrf:
self.addcmd(
{"name": vrfk},
"name",
False,
)
self.addcmd(
{"afi": afv.get("afi"), "safi": afv.get("safi")},
"address_family",
True,
)

if self.state in ["overridden"]:
for vrfk, vrfv in iteritems(haved):
for k, have in iteritems(vrfv.get("address_families", {})):
wantx = wantd.get(vrfk, {}).get("address_families", {})
if k not in wantx:
self.addcmd(
{"name": vrfk},
"name",
False,
)
self.addcmd(
{"afi": have.get("afi"), "safi": have.get("safi")},
"address_family",
False,
)
self.compare(parsers=self.parsers, want={}, have=have)

if self.state != "deleted":
self._compare(want=wantd, have=haved)

def _compare(self, want, have):
"""Leverages the base class `compare()` method and
populates the list of commands to be run by comparing
the `want` and `have` data with the `parsers` defined
for the Vrf network resource.
"""
for name, entry in iteritems(want):
begin = len(self.commands)
vrf_want = entry
vrf_have = have.pop(name, {})
self._compare_afs(vrf_want, vrf_have)
if len(self.commands) != begin:
self.commands.insert(begin, "vrf definition {0}".format(name))

def _compare_afs(self, want, have):
"""Custom handling of afs option
:params want: the want VRF dictionary
:params have: the have VRF dictionary
"""
waafs = want.get("address_families", {})
haafs = have.get("address_families", {})
for afk, afv in iteritems(waafs):
begin = len(self.commands)
self._compare_single_af(want=afv, have=haafs.get(afk, {}))
if len(self.commands) != begin:
self.commands.insert(begin, f"address-family {afv.get('afi')} {afv.get('safi')}")

def _compare_single_af(self, want, have):
"""Custom handling of single af option
:params want: the want VRF dictionary
:params have: the have VRF dictionary
"""
self.compare(parsers=self.parsers[1:], want=want, have=have)

def _vrf_list_to_dict(self, entry):
"""Convert list of items to dict of items
for efficient diff calculation.
:params entry: data dictionary
"""

for vrf in entry:
if "address_families" in vrf:
vrf["address_families"] = {
(x["afi"], x.get("safi")): x for x in vrf["address_families"]
}

entry = {x["name"]: x for x in entry}
return entry
4 changes: 4 additions & 0 deletions plugins/module_utils/network/ios/facts/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vlans.vlans import (
VlansFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vrf_address_family.vrf_address_family import (
Vrf_address_familyFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vrf_global.vrf_global import (
Vrf_globalFacts,
)
Expand Down Expand Up @@ -145,6 +148,7 @@
vxlan_vtep=Vxlan_vtepFacts,
evpn_global=Evpn_globalFacts,
evpn_evi=Evpn_eviFacts,
vrf_address_family=Vrf_address_familyFacts,
vrf_global=Vrf_globalFacts,
vrf_interfaces=Vrf_interfacesFacts,
)
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function


__metaclass__ = type

"""
The ios vrf_address_family fact class
It is in this file the configuration is collected from the device
for a given resource, parsed, and the facts tree is populated
based on the configuration.
"""


from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils

from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.vrf_address_family.vrf_address_family import (
Vrf_address_familyArgs,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.vrf_address_family import (
Vrf_address_familyTemplate,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.utils.utils import (
flatten_config,
)


class Vrf_address_familyFacts(object):
"""The ios vrf_address_family facts class"""

def __init__(self, module, subspec="config", options="options"):
self._module = module
self.argument_spec = Vrf_address_familyArgs.argument_spec

def get_config(self, connection):
"""Get the configuration from the device"""

return connection.get("show running-config | section ^vrf")

def _flatten_config(self, config):
dataLines = config.split("\n")
finalConfig = []

for line in dataLines:
if "address-family" in line and "exit-address-family" not in line:
finalConfig.append(line)

return "\n".join(finalConfig)

def populate_facts(self, connection, ansible_facts, data=None):
"""Populate the facts for Vrf_address_family network resource

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

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

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

address_data = flatten_config(data, "address-family")
data = flatten_config(address_data, "vrf")
finalConfig = self._flatten_config(data)

# parse native config using the Vrf_address_family template
vrf_address_family_parser = Vrf_address_familyTemplate(
lines=finalConfig.splitlines(),
module=self._module,
)
obj = vrf_address_family_parser.parse()
objs = list(obj.values())

for vrf in objs:
af = vrf.get("address_families", {})
if af:
self._post_parse(vrf)
else:
vrf["address_families"] = []

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

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

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

return ansible_facts

def _post_parse(self, af_data):
"""Converts the intermediate data structure
to valid format as per argspec.
:param obj: dict
"""
af = af_data.get("address_families", {})
if af:
af_data["address_families"] = list(af.values())
Loading
Loading