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

Cisco.ios.vrf interfaces #1132

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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 @@ -93,6 +93,7 @@ Name | Description
[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_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.

<!--end collection content-->
Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/rm_vrf_interfaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Added ios_vrf_interfaces resource module,that helps with configuration of vrfs within interface
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- 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

#############################################
# WARNING #
#############################################
#
# This file is auto generated by the
# ansible.content_builder.
#
# Manually editing this file is not advised.
#
# To update the argspec make the desired changes
# in the documentation in the module file and re-run
# ansible.content_builder commenting out
# the path to external 'docstring' in build.yaml.
#
##############################################

"""
The arg spec for the ios_vrf_interfaces module
"""


class Vrf_interfacesArgs(object): # pylint: disable=R0903
"""The arg spec for the ios_vrf_interfaces module"""

argument_spec = {
"config": {
"type": "list",
"elements": "dict",
"options": {
"name": {"type": "str", "required": True},
"vrf_name": {"type": "str"},
},
},
"running_config": {"type": "str"},
"state": {
"type": "str",
"choices": [
"merged",
"replaced",
"overridden",
"deleted",
"gathered",
"rendered",
"parsed",
],
"default": "merged",
},
} # pylint: disable=C0301
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#
# -*- 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_interfaces 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_interfaces import (
Vrf_interfacesTemplate,
)


class Vrf_interfaces(ResourceModule):
"""
The ios_vrf_interfaces config class
"""

def __init__(self, module):
super(Vrf_interfaces, self).__init__(
empty_fact_val={},
facts_module=Facts(module),
module=module,
resource="vrf_interfaces",
tmplt=Vrf_interfacesTemplate(),
)
self.parsers = [
"interface",
"vrf_name",
]

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 = {entry["name"]: entry for entry in self.want}
haved = {entry["name"]: entry for entry in self.have}

# 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":
haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd}
wantd = {}

# remove superfluous config for overridden and deleted
if self.state in ["overridden", "deleted"]:
for k, have in iteritems(haved):
if k not in wantd:
self._compare(want={}, have=have)

for k, want in iteritems(wantd):
self._compare(want=want, have=haved.pop(k, {}))

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_interfaces network resource.
"""
begin = len(self.commands)
self.compare(parsers=self.parsers, want=want, have=have)
if len(self.commands) != begin:
self.commands.insert(begin, self._tmplt.render(want or have, "interface", False))
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 @@ -102,6 +102,9 @@
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vrf_global.vrf_global import (
Vrf_globalFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vrf_interfaces.vrf_interfaces import (
Vrf_interfacesFacts,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.vxlan_vtep.vxlan_vtep import (
Vxlan_vtepFacts,
)
Expand Down Expand Up @@ -143,6 +146,7 @@
evpn_global=Evpn_globalFacts,
evpn_evi=Evpn_eviFacts,
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,81 @@
# -*- 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 cisco.ios vrf_interfaces facts class
It is in this file that 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_interfaces.vrf_interfaces import (
Vrf_interfacesArgs,
)
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.vrf_interfaces import (
Vrf_interfacesTemplate,
)


class Vrf_interfacesFacts(object):
"""The cisco.ios vrf_interfaces facts class"""

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

def get_vrf_interfaces_data(self, connection):
"""Fetch the configuration data for VRF interfaces.

:param connection: The device connection object
:returns: The raw configuration data from the device
"""
return connection.get("show running-config | section ^interface")

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

:param connection: the device connection
:param ansible_facts: Facts dictionary
:param data: previously collected config (optional)

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

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

# Parse native config using the Vrf_interfaces template
vrf_interfaces_parser = Vrf_interfacesTemplate(
lines=data.splitlines(),
module=self._module,
)

objs = list(vrf_interfaces_parser.parse().values())
# Ensure previous facts are removed to avoid duplication
ansible_facts["ansible_network_resources"].pop("vrf_interfaces", None)

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

# Update the ansible_facts dictionary with the VRF interface facts
facts["vrf_interfaces"] = params.get("config", [])
ansible_facts["ansible_network_resources"].update(facts)

return ansible_facts
62 changes: 62 additions & 0 deletions plugins/module_utils/network/ios/rm_templates/vrf_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- 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 Vrf_interfaces parser templates file. This contains
a list of parser definitions and associated functions that
facilitates both facts gathering and native command generation for
the given network resource.
"""

import re

from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import (
NetworkTemplate,
)


class Vrf_interfacesTemplate(NetworkTemplate):
def __init__(self, lines=None, module=None):
super(Vrf_interfacesTemplate, self).__init__(lines=lines, tmplt=self, module=module)

# fmt: off
PARSERS = [
{
"name": "interface",
"getval": re.compile(
r"""
^interface\s(?P<name>\S+)$""",
re.VERBOSE,
),
"setval": "interface {{ name }}",
'result': {
'{{ name }}': {
'name': '{{ name }}',
},
},
"shared": True,
},
{
"name": "vrf_name",
"getval": re.compile(
r"""
\s*vrf\sforwarding\s(?P<vrf_name>\S+)$
""",
re.VERBOSE,
),
"setval": "vrf forwarding {{ vrf_name }}",
"result": {
'{{ name }}': {
'vrf_name': '{{ vrf_name }}',
},
},
},
]
# fmt: on
Loading
Loading