diff --git a/docs/vyos.vyos.vyos_nhrp_module.rst b/docs/vyos.vyos.vyos_nhrp_module.rst index f0f2c35b..2f4f156b 100644 --- a/docs/vyos.vyos.vyos_nhrp_module.rst +++ b/docs/vyos.vyos.vyos_nhrp_module.rst @@ -1,722 +1,724 @@ .. _vyos.vyos.vyos_nhrp_module: ******************* vyos.vyos.vyos_nhrp ******************* **NHRP resource module** Version added: 7.0.0 .. contents:: :local: :depth: 1 Synopsis -------- - This module manages the Next Hop Resolution Protocol (NHRP) configuration of tunnel interfaces on devices running VyOS, as used for DMVPN. Parameters ---------- .. raw:: html
Parameter Choices/Defaults Comments
config
dictionary
The NHRP configuration.
tunnels
list / elements=dictionary
NHRP configuration per tunnel interface.
authentication
string
The authentication password, sent in clear text in NHRP packets.
Up to 8 characters, without spaces.
holdtime
integer
The holding time of NHRP registrations and resolutions, in seconds (1-65000).
map
list / elements=dictionary
Static mappings of tunnel addresses to NBMA addresses.
nbma
string + / required
The NBMA (underlay) address of the peer, or local for this router.
tunnel_ip
string / required
The tunnel (protocol) address of the peer.
mtu
integer
The MTU advertised in NHRP registrations (68-16000).
multicast
list / elements=string
Where multicast traffic is sent, as protocol addresses or dynamic for all dynamically learned peers.
A hub typically uses dynamic, so that routing protocols reach all spokes.
name
string / required
The tunnel interface name, for example tun0.
network_id
integer
The NHRP network identifier of the tunnel.
nhs
list / elements=dictionary
The next hop servers (hubs) this router registers with.
nbma
list / elements=string + / required
The NBMA (underlay) addresses of the server.
tunnel_ip
string / required
The tunnel address of the server, or dynamic to learn it from the server.
redirect
boolean
    Choices:
  • no
  • yes
Whether to send NHRP traffic indication (redirect) messages, typically on a hub.
registration_no_unique
boolean
    Choices:
  • no
  • yes
Whether to allow a peer to register with a changing NBMA address.
shortcut
boolean
    Choices:
  • no
  • yes
Whether to create shortcut routes from NHRP redirects, typically on a spoke.
running_config
string
This option is used only with state parsed.
The value of this option should be the output received from the VyOS device by executing the command show configuration commands | grep 'protocols nhrp'.
The state parsed reads the configuration from running_config option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the parsed key within the result.
state
string
    Choices:
  • merged ←
  • replaced
  • overridden
  • deleted
  • gathered
  • rendered
  • parsed
The state the configuration should be left in.

Notes ----- .. note:: - Tested against VyOS 1.5.1. - This module works with connection ``network_cli``. - The tunnel interfaces themselves (encapsulation, source, addresses) are managed with :ref:`vyos.vyos.vyos_interfaces ` and :ref:`vyos.vyos.vyos_l3_interfaces `, and their IPsec protection with :ref:`vyos.vyos.vyos_vpn_ipsec `. - VyOS requires tunnels used by NHRP to have a /32 address. Examples -------- .. code-block:: yaml # Using merged # Before state: # ------------- # # vyos@vyos:~$ show configuration commands | grep 'protocols nhrp' # - name: Configure a DMVPN hub tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret holdtime: 300 multicast: - dynamic redirect: true registration_no_unique: true state: merged # Task output # ----------- # # commands: # - set protocols nhrp tunnel tun0 authentication 'secret' # - set protocols nhrp tunnel tun0 holdtime 300 # - set protocols nhrp tunnel tun0 redirect # - set protocols nhrp tunnel tun0 registration-no-unique # - set protocols nhrp tunnel tun0 multicast dynamic # Using merged, on a spoke - name: Configure a DMVPN spoke tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret shortcut: true nhs: - tunnel_ip: dynamic nbma: - 192.0.2.1 multicast: - dynamic state: merged # Task output # ----------- # # commands: # - set protocols nhrp tunnel tun0 authentication 'secret' # - set protocols nhrp tunnel tun0 shortcut # - set protocols nhrp tunnel tun0 multicast dynamic # - set protocols nhrp tunnel tun0 nhs tunnel-ip dynamic nbma 192.0.2.1 # Using replaced # Before state: # ------------- # # set protocols nhrp tunnel tun0 authentication 'secret' # set protocols nhrp tunnel tun0 holdtime '300' # set protocols nhrp tunnel tun0 multicast 'dynamic' # set protocols nhrp tunnel tun0 redirect - name: Replace the configuration of tun0 vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret multicast: - dynamic state: replaced # Task output # ----------- # # commands: # - delete protocols nhrp tunnel tun0 holdtime # - delete protocols nhrp tunnel tun0 redirect # Using overridden # Before state: # ------------- # # set protocols nhrp tunnel tun0 authentication 'secret' # set protocols nhrp tunnel tun1 shortcut - name: Override the NHRP configuration with one tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret state: overridden # Task output # ----------- # # commands: # - delete protocols nhrp tunnel tun1 # Using deleted - name: Delete the NHRP configuration of one tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun1 state: deleted - name: Delete all NHRP configuration vyos.vyos.vyos_nhrp: state: deleted # Task output (all NHRP configuration) # ------------------------------------ # # commands: # - delete protocols nhrp # Using gathered - name: Gather the NHRP configuration vyos.vyos.vyos_nhrp: state: gathered # Task output # ----------- # # gathered: # tunnels: # - name: tun0 # authentication: secret # holdtime: 300 # multicast: # - dynamic # redirect: true # Using rendered - name: Render the commands for a spoke tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 nhs: - tunnel_ip: 10.0.0.1 nbma: - 192.0.2.1 state: rendered # Task output # ----------- # # rendered: # - set protocols nhrp tunnel tun0 nhs tunnel-ip 10.0.0.1 nbma 192.0.2.1 # Using parsed - name: Parse NHRP configuration from device output vyos.vyos.vyos_nhrp: running_config: | set protocols nhrp tunnel tun0 map tunnel-ip '10.0.0.2' nbma '192.0.2.2' set protocols nhrp tunnel tun0 shortcut state: parsed # Task output # ----------- # # parsed: # tunnels: # - name: tun0 # map: # - tunnel_ip: 10.0.0.2 # nbma: 192.0.2.2 # shortcut: true Return Values ------------- Common return values are documented `here `_, the following are the fields unique to this module: .. raw:: html
Key Returned Description
after
dictionary
when changed
The resulting configuration after module execution.

Sample:
This output will always be in the same format as the module argspec.
before
dictionary
when state is merged, replaced, overridden or deleted
The configuration prior to the module execution.

Sample:
This output will always be in the same format as the module argspec.
commands
list
when state is merged, replaced, overridden or deleted
The set of commands pushed to the remote device.

Sample:
["set protocols nhrp tunnel tun0 authentication 'secret'", 'set protocols nhrp tunnel tun0 nhs tunnel-ip dynamic nbma 192.0.2.1']
gathered
dictionary
when state is gathered
Facts about the network resource gathered from the remote device as structured data.

Sample:
This output will always be in the same format as the module argspec.
parsed
dictionary
when state is parsed
The device native config provided in running_config option parsed into structured data as per module argspec.

Sample:
This output will always be in the same format as the module argspec.
rendered
list
when state is rendered
The provided configuration in the task rendered in device-native format (offline).

Sample:
['set protocols nhrp tunnel tun0 shortcut']


Status ------ Authors ~~~~~~~ - Evgeny Molotkov (@omnom62) diff --git a/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py b/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py index de3d1c2a..79673943 100644 --- a/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py +++ b/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py @@ -1,70 +1,70 @@ # -*- coding: utf-8 -*- # Copyright 2026 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 arg spec for the vyos_nhrp module """ class NhrpArgs(object): # pylint: disable=R0903 """The arg spec for the vyos_nhrp module""" argument_spec = { "config": { "type": "dict", "options": { "tunnels": { "type": "list", "elements": "dict", "options": { "name": {"type": "str", "required": True}, "authentication": {"type": "str", "no_log": True}, "holdtime": {"type": "int"}, "mtu": {"type": "int"}, "network_id": {"type": "int"}, "multicast": {"type": "list", "elements": "str"}, "redirect": {"type": "bool"}, "shortcut": {"type": "bool"}, "registration_no_unique": {"type": "bool"}, "map": { "type": "list", "elements": "dict", "options": { "tunnel_ip": {"type": "str", "required": True}, - "nbma": {"type": "str"}, + "nbma": {"type": "str", "required": True}, }, }, "nhs": { "type": "list", "elements": "dict", "options": { "tunnel_ip": {"type": "str", "required": True}, - "nbma": {"type": "list", "elements": "str"}, + "nbma": {"type": "list", "elements": "str", "required": True}, }, }, }, }, }, }, "running_config": {"type": "str"}, "state": { "choices": [ "merged", "replaced", "overridden", "deleted", "gathered", "rendered", "parsed", ], "default": "merged", "type": "str", }, } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/nhrp/nhrp.py b/plugins/module_utils/network/vyos/config/nhrp/nhrp.py index 527f1490..aac96b2e 100644 --- a/plugins/module_utils/network/vyos/config/nhrp/nhrp.py +++ b/plugins/module_utils/network/vyos/config/nhrp/nhrp.py @@ -1,152 +1,149 @@ # -*- coding: utf-8 -*- # Copyright 2026 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 vyos_nhrp 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_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.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import Facts from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.nhrp import ( NhrpTemplate, ) class Nhrp(ResourceModule): """ The vyos_nhrp config class """ def __init__(self, module): super(Nhrp, self).__init__( empty_fact_val={}, facts_module=Facts(module), module=module, resource="nhrp", tmplt=NhrpTemplate(), ) self.parsers = [ "authentication", "holdtime", "mtu", "network_id", "redirect", "shortcut", "registration_no_unique", ] 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._tunnels(self.want) haved = self._tunnels(self.have) if self.state == "deleted": if not wantd: # no tunnels given: remove all of protocols nhrp if haved: self.commands.append("delete protocols nhrp") return for name in wantd: if name in haved: self.addcmd({"name": name}, "tunnel", True) return if self.state == "overridden": for name in haved: if name not in wantd: self.addcmd({"name": name}, "tunnel", True) merge = self.state in ("merged", "rendered") for name, want in wantd.items(): - have = haved.get(name, {"name": name}) - self._compare(name, want, have, merge) + if name not in haved and want.keys() == {"name"}: + # a new tunnel without options still needs its node + self.addcmd({"name": name}, "tunnel", False) + continue + self._compare(name, want, haved.get(name, {"name": name}), merge) def _compare(self, name, want, have, merge): """Compare one tunnel: options and flags, then multicast, map and nhs.""" - begin = len(self.commands) scalars = dict_merge(have, want) if merge else want self.compare(parsers=self.parsers, want=scalars, have=have) self._compare_multicast(name, want, have, merge) self._compare_map(name, want, have, merge) self._compare_nhs(name, want, have, merge) - # a new tunnel without any options still needs its node - if len(self.commands) == begin and have.keys() == {"name"} and want.keys() == {"name"}: - self.addcmd({"name": name}, "tunnel", False) def _compare_multicast(self, name, want, have, merge): wantm = set(want.get("multicast") or []) havem = set(have.get("multicast") or []) for value in sorted(wantm - havem): self.addcmd({"name": name, "multicast": value}, "multicast", False) if not merge: for value in sorted(havem - wantm): self.addcmd({"name": name, "multicast": value}, "multicast", True) def _compare_map(self, name, want, have, merge): wantm = {m["tunnel_ip"]: m for m in want.get("map") or []} havem = {m["tunnel_ip"]: m for m in have.get("map") or []} for ip, entry in sorted(wantm.items()): if havem.get(ip) != entry: self.addcmd({"name": name, "map": entry}, "map", False) if not merge: for ip, entry in sorted(havem.items()): if ip not in wantm: self.addcmd({"name": name, "map": entry}, "map", True) def _compare_nhs(self, name, want, have, merge): wantn = {n["tunnel_ip"]: n for n in want.get("nhs") or []} haven = {n["tunnel_ip"]: n for n in have.get("nhs") or []} for ip, entry in sorted(wantn.items()): wnbma = set(entry.get("nbma") or []) hnbma = set((haven.get(ip) or {}).get("nbma") or []) - if ip not in haven and not wnbma: - self.addcmd({"name": name, "nhs": {"tunnel_ip": ip}}, "nhs", False) for nbma in sorted(wnbma - hnbma): self.addcmd({"name": name, "nhs": {"tunnel_ip": ip, "nbma": nbma}}, "nhs", False) if not merge: for nbma in sorted(hnbma - wnbma): self.commands.append( "delete protocols nhrp tunnel {0} nhs tunnel-ip {1} nbma {2}".format( name, ip, nbma, ), ) if not merge: for ip in sorted(haven): if ip not in wantn: self.addcmd({"name": name, "nhs": {"tunnel_ip": ip}}, "nhs", True) def _tunnels(self, data): """Tunnels by name.""" return {t["name"]: t for t in (data or {}).get("tunnels") or []} diff --git a/plugins/modules/vyos_nhrp.py b/plugins/modules/vyos_nhrp.py index 70089668..90de22a7 100644 --- a/plugins/modules/vyos_nhrp.py +++ b/plugins/modules/vyos_nhrp.py @@ -1,388 +1,390 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright 2026 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The module file for vyos_nhrp """ from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = """ module: vyos_nhrp version_added: 7.0.0 short_description: NHRP resource module description: - This module manages the Next Hop Resolution Protocol (NHRP) configuration of tunnel interfaces on devices running VyOS, as used for DMVPN. author: - Evgeny Molotkov (@omnom62) notes: - Tested against VyOS 1.5.1. - This module works with connection C(network_cli). - The tunnel interfaces themselves (encapsulation, source, addresses) are managed with M(vyos.vyos.vyos_interfaces) and M(vyos.vyos.vyos_l3_interfaces), and their IPsec protection with M(vyos.vyos.vyos_vpn_ipsec). - VyOS requires tunnels used by NHRP to have a /32 address. options: config: description: The NHRP configuration. type: dict suboptions: tunnels: description: NHRP configuration per tunnel interface. type: list elements: dict suboptions: name: description: The tunnel interface name, for example C(tun0). type: str required: true authentication: description: - The authentication password, sent in clear text in NHRP packets. - Up to 8 characters, without spaces. type: str holdtime: description: The holding time of NHRP registrations and resolutions, in seconds (1-65000). type: int mtu: description: The MTU advertised in NHRP registrations (68-16000). type: int network_id: description: The NHRP network identifier of the tunnel. type: int multicast: description: - Where multicast traffic is sent, as protocol addresses or C(dynamic) for all dynamically learned peers. - A hub typically uses C(dynamic), so that routing protocols reach all spokes. type: list elements: str redirect: description: Whether to send NHRP traffic indication (redirect) messages, typically on a hub. type: bool shortcut: description: Whether to create shortcut routes from NHRP redirects, typically on a spoke. type: bool registration_no_unique: description: Whether to allow a peer to register with a changing NBMA address. type: bool map: description: Static mappings of tunnel addresses to NBMA addresses. type: list elements: dict suboptions: tunnel_ip: description: The tunnel (protocol) address of the peer. type: str required: true nbma: description: The NBMA (underlay) address of the peer, or C(local) for this router. type: str + required: true nhs: description: The next hop servers (hubs) this router registers with. type: list elements: dict suboptions: tunnel_ip: description: The tunnel address of the server, or C(dynamic) to learn it from the server. type: str required: true nbma: description: The NBMA (underlay) addresses of the server. type: list elements: str + required: true running_config: description: - This option is used only with state I(parsed). - The value of this option should be the output received from the VyOS device by executing the command B(show configuration commands | grep 'protocols nhrp'). - The state I(parsed) reads the configuration from C(running_config) option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the I(parsed) key within the result. type: str state: description: - The state the configuration should be left in. type: str choices: - merged - replaced - overridden - deleted - gathered - rendered - parsed default: merged """ EXAMPLES = """ # Using merged # Before state: # ------------- # # vyos@vyos:~$ show configuration commands | grep 'protocols nhrp' # - name: Configure a DMVPN hub tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret holdtime: 300 multicast: - dynamic redirect: true registration_no_unique: true state: merged # Task output # ----------- # # commands: # - set protocols nhrp tunnel tun0 authentication 'secret' # - set protocols nhrp tunnel tun0 holdtime 300 # - set protocols nhrp tunnel tun0 redirect # - set protocols nhrp tunnel tun0 registration-no-unique # - set protocols nhrp tunnel tun0 multicast dynamic # Using merged, on a spoke - name: Configure a DMVPN spoke tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret shortcut: true nhs: - tunnel_ip: dynamic nbma: - 192.0.2.1 multicast: - dynamic state: merged # Task output # ----------- # # commands: # - set protocols nhrp tunnel tun0 authentication 'secret' # - set protocols nhrp tunnel tun0 shortcut # - set protocols nhrp tunnel tun0 multicast dynamic # - set protocols nhrp tunnel tun0 nhs tunnel-ip dynamic nbma 192.0.2.1 # Using replaced # Before state: # ------------- # # set protocols nhrp tunnel tun0 authentication 'secret' # set protocols nhrp tunnel tun0 holdtime '300' # set protocols nhrp tunnel tun0 multicast 'dynamic' # set protocols nhrp tunnel tun0 redirect - name: Replace the configuration of tun0 vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret multicast: - dynamic state: replaced # Task output # ----------- # # commands: # - delete protocols nhrp tunnel tun0 holdtime # - delete protocols nhrp tunnel tun0 redirect # Using overridden # Before state: # ------------- # # set protocols nhrp tunnel tun0 authentication 'secret' # set protocols nhrp tunnel tun1 shortcut - name: Override the NHRP configuration with one tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 authentication: secret state: overridden # Task output # ----------- # # commands: # - delete protocols nhrp tunnel tun1 # Using deleted - name: Delete the NHRP configuration of one tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun1 state: deleted - name: Delete all NHRP configuration vyos.vyos.vyos_nhrp: state: deleted # Task output (all NHRP configuration) # ------------------------------------ # # commands: # - delete protocols nhrp # Using gathered - name: Gather the NHRP configuration vyos.vyos.vyos_nhrp: state: gathered # Task output # ----------- # # gathered: # tunnels: # - name: tun0 # authentication: secret # holdtime: 300 # multicast: # - dynamic # redirect: true # Using rendered - name: Render the commands for a spoke tunnel vyos.vyos.vyos_nhrp: config: tunnels: - name: tun0 nhs: - tunnel_ip: 10.0.0.1 nbma: - 192.0.2.1 state: rendered # Task output # ----------- # # rendered: # - set protocols nhrp tunnel tun0 nhs tunnel-ip 10.0.0.1 nbma 192.0.2.1 # Using parsed - name: Parse NHRP configuration from device output vyos.vyos.vyos_nhrp: running_config: | set protocols nhrp tunnel tun0 map tunnel-ip '10.0.0.2' nbma '192.0.2.2' set protocols nhrp tunnel tun0 shortcut state: parsed # Task output # ----------- # # parsed: # tunnels: # - name: tun0 # map: # - tunnel_ip: 10.0.0.2 # nbma: 192.0.2.2 # shortcut: true """ RETURN = """ before: description: The configuration prior to the module execution. returned: when I(state) is C(merged), C(replaced), C(overridden) or C(deleted) type: dict sample: > This output will always be in the same format as the module argspec. after: description: The resulting configuration after module execution. returned: when changed type: dict sample: > This output will always be in the same format as the module argspec. commands: description: The set of commands pushed to the remote device. returned: when I(state) is C(merged), C(replaced), C(overridden) or C(deleted) type: list sample: - set protocols nhrp tunnel tun0 authentication 'secret' - set protocols nhrp tunnel tun0 nhs tunnel-ip dynamic nbma 192.0.2.1 rendered: description: The provided configuration in the task rendered in device-native format (offline). returned: when I(state) is C(rendered) type: list sample: - set protocols nhrp tunnel tun0 shortcut gathered: description: Facts about the network resource gathered from the remote device as structured data. returned: when I(state) is C(gathered) type: dict sample: > This output will always be in the same format as the module argspec. parsed: description: The device native config provided in I(running_config) option parsed into structured data as per module argspec. returned: when I(state) is C(parsed) type: dict sample: > This output will always be in the same format as the module argspec. """ from ansible.module_utils.basic import AnsibleModule from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.nhrp.nhrp import ( NhrpArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.nhrp.nhrp import ( Nhrp, ) def main(): """ Main entry point for module execution :returns: the result form module invocation """ module = AnsibleModule( argument_spec=NhrpArgs.argument_spec, mutually_exclusive=[["config", "running_config"]], required_if=[ ["state", "merged", ["config"]], ["state", "replaced", ["config"]], ["state", "overridden", ["config"]], ["state", "rendered", ["config"]], ["state", "parsed", ["running_config"]], ], supports_check_mode=True, ) result = Nhrp(module).execute_module() module.exit_json(**result) if __name__ == "__main__": main() diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_nhrp_config_bare_tunnel.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_nhrp_config_bare_tunnel.cfg new file mode 100644 index 00000000..f027f1a5 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_nhrp_config_bare_tunnel.cfg @@ -0,0 +1 @@ +set protocols nhrp tunnel tun3 diff --git a/tests/unit/modules/network/vyos/test_vyos_nhrp.py b/tests/unit/modules/network/vyos/test_vyos_nhrp.py index 5122c2e3..736b832a 100644 --- a/tests/unit/modules/network/vyos/test_vyos_nhrp.py +++ b/tests/unit/modules/network/vyos/test_vyos_nhrp.py @@ -1,189 +1,209 @@ # -*- coding: utf-8 -*- # Copyright 2026 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 from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_nhrp from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture HUB = dict( name="tun0", authentication="secret", holdtime=300, multicast=["dynamic"], network_id=1, redirect=True, registration_no_unique=True, ) SPOKE = dict( name="tun1", map=[dict(tunnel_ip="10.0.0.2", nbma="192.0.2.2")], mtu=1400, nhs=[dict(tunnel_ip="dynamic", nbma=["192.0.2.1", "198.51.100.1"])], shortcut=True, ) class TestVyosNhrpModule(TestVyosModule): module = vyos_nhrp def setUp(self): super(TestVyosNhrpModule, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_get_resource_connection_facts = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", ) self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() self.mock_get_device_data = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.nhrp.nhrp.NhrpFacts.get_device_data", ) self.get_device_data = self.mock_get_device_data.start() def tearDown(self): super(TestVyosNhrpModule, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_get_device_data.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_nhrp_config.cfg" def load_from_file(*args, **kwargs): return load_fixture(filename) self.get_device_data.side_effect = load_from_file def test_vyos_nhrp_merged_idempotent(self): set_module_args(dict(config=dict(tunnels=[HUB, SPOKE]), state="merged")) self.execute_module(changed=False, commands=[]) def test_vyos_nhrp_merged_list_order_does_not_matter(self): spoke = dict(SPOKE, nhs=[dict(tunnel_ip="dynamic", nbma=["198.51.100.1", "192.0.2.1"])]) set_module_args(dict(config=dict(tunnels=[spoke]), state="merged")) self.execute_module(changed=False, commands=[]) def test_vyos_nhrp_merged(self): set_module_args( dict( config=dict( tunnels=[ dict(name="tun0", holdtime=600, multicast=["10.0.0.255"]), dict( name="tun1", nhs=[dict(tunnel_ip="dynamic", nbma=["203.0.113.1"])], ), dict( name="tun2", map=[dict(tunnel_ip="10.1.0.1", nbma="local")], network_id=2, ), ], ), state="merged", ), ) commands = [ "set protocols nhrp tunnel tun0 holdtime 600", "set protocols nhrp tunnel tun0 multicast 10.0.0.255", "set protocols nhrp tunnel tun1 nhs tunnel-ip dynamic nbma 203.0.113.1", "set protocols nhrp tunnel tun2 network-id 2", "set protocols nhrp tunnel tun2 map tunnel-ip 10.1.0.1 nbma local", ] self.execute_module(changed=True, commands=commands) def test_vyos_nhrp_merged_new_tunnel_without_options(self): set_module_args(dict(config=dict(tunnels=[dict(name="tun3")]), state="merged")) self.execute_module(changed=True, commands=["set protocols nhrp tunnel tun3"]) def test_vyos_nhrp_replaced(self): set_module_args( dict( config=dict( tunnels=[ dict(name="tun0", authentication="newpass", multicast=["dynamic"]), dict( name="tun1", nhs=[dict(tunnel_ip="dynamic", nbma=["192.0.2.1"])], shortcut=True, ), ], ), state="replaced", ), ) commands = [ "set protocols nhrp tunnel tun0 authentication 'newpass'", "delete protocols nhrp tunnel tun0 holdtime", "delete protocols nhrp tunnel tun0 network-id", "delete protocols nhrp tunnel tun0 redirect", "delete protocols nhrp tunnel tun0 registration-no-unique", "delete protocols nhrp tunnel tun1 map tunnel-ip 10.0.0.2", "delete protocols nhrp tunnel tun1 mtu", "delete protocols nhrp tunnel tun1 nhs tunnel-ip dynamic nbma 198.51.100.1", ] self.execute_module(changed=True, commands=commands) def test_vyos_nhrp_replaced_idempotent(self): set_module_args(dict(config=dict(tunnels=[HUB, SPOKE]), state="replaced")) self.execute_module(changed=False, commands=[]) def test_vyos_nhrp_overridden(self): set_module_args(dict(config=dict(tunnels=[HUB]), state="overridden")) self.execute_module(changed=True, commands=["delete protocols nhrp tunnel tun1"]) def test_vyos_nhrp_overridden_idempotent(self): set_module_args(dict(config=dict(tunnels=[HUB, SPOKE]), state="overridden")) self.execute_module(changed=False, commands=[]) def test_vyos_nhrp_deleted_tunnel(self): set_module_args(dict(config=dict(tunnels=[dict(name="tun1")]), state="deleted")) self.execute_module(changed=True, commands=["delete protocols nhrp tunnel tun1"]) def test_vyos_nhrp_deleted_all(self): set_module_args(dict(state="deleted")) self.execute_module(changed=True, commands=["delete protocols nhrp"]) def test_vyos_nhrp_deleted_not_configured(self): set_module_args(dict(config=dict(tunnels=[dict(name="tun9")]), state="deleted")) self.execute_module(changed=False, commands=[]) def test_vyos_nhrp_rendered(self): set_module_args(dict(config=dict(tunnels=[SPOKE]), state="rendered")) commands = [ "set protocols nhrp tunnel tun1 map tunnel-ip 10.0.0.2 nbma 192.0.2.2", "set protocols nhrp tunnel tun1 mtu 1400", "set protocols nhrp tunnel tun1 nhs tunnel-ip dynamic nbma 192.0.2.1", "set protocols nhrp tunnel tun1 nhs tunnel-ip dynamic nbma 198.51.100.1", "set protocols nhrp tunnel tun1 shortcut", ] result = self.execute_module(changed=False) self.assertEqual(sorted(result["rendered"]), sorted(commands)) def test_vyos_nhrp_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) self.assertEqual(result["gathered"], dict(tunnels=[HUB, SPOKE])) def test_vyos_nhrp_parsed(self): set_module_args( dict(running_config=load_fixture("vyos_nhrp_config.cfg"), state="parsed"), ) result = self.execute_module(changed=False) self.assertEqual(result["parsed"], dict(tunnels=[HUB, SPOKE])) + + def test_vyos_nhrp_merged_existing_bare_tunnel_idempotent(self): + # an existing tunnel without options is not re-created on every run + set_module_args(dict(config=dict(tunnels=[dict(name="tun3")]), state="merged")) + self.execute_module( + changed=False, + commands=[], + filename="vyos_nhrp_config_bare_tunnel.cfg", + ) + + def test_vyos_nhrp_map_without_nbma_rejected(self): + # VyOS requires an NBMA address on every map + set_module_args( + dict( + config=dict(tunnels=[dict(name="tun1", map=[dict(tunnel_ip="10.0.0.2")])]), + state="replaced", + ), + ) + result = self.execute_module(failed=True) + self.assertIn("missing required arguments: nbma", result["msg"])