diff --git a/README.md b/README.md index 93852a2b..e69e2ea0 100644 --- a/README.md +++ b/README.md @@ -1,298 +1,299 @@ # VyOS Collection [![codecov](https://codecov.io/gh/vyos/vyos.vyos/graph/badge.svg?token=J217GFD69W)](https://codecov.io/gh/vyos/vyos.vyos) [![CI](https://github.com/vyos/vyos.vyos/actions/workflows/tests.yml/badge.svg?branch=main&event=schedule)](https://github.com/vyos/vyos.vyos/actions/workflows/tests.yml) The Ansible VyOS collection includes a variety of Ansible content to help automate the management of VyOS network appliances. This collection has been tested against VyOS 1.3.8, 1.4.1 and the current rolling release for 1.5. Where possible, compatibility with older versions of VyOS are maintained but not guaranteed. ## Communication * Join the VyOS forum: * [FAQ](https://forum.vyos.io/faq): find answers to frequently asked questions. * [Guides and How To](https://forum.vyos.io/c/howto-guies/27): find guides and how-to articles. * [News & Announcements](https://forum.vyos.io/c/announcements/6): track project-wide announcements . ## Ansible version compatibility This collection has been tested against the following Ansible versions: **>=2.16.0**. Plugins and modules within a collection may be tested with only specific Ansible versions. A collection may contain metadata that identifies these versions. PEP440 is the schema used to describe the versions of Ansible. ### Supported connections The VyOS collection supports ``network_cli`` connections. ## Requirements - ansible-core >=2.16.0, see [Ansible version compatibility](#ansible-version-compatibility). - Python >=3.10 on the Ansible controller, as required by ansible-core 2.16. - [ansible.netcommon](https://galaxy.ansible.com/ui/repo/published/ansible/netcommon/) >=2.5.1, installed automatically as a dependency of this collection. - Python packages `paramiko` and `scp`, listed in [requirements.txt](https://github.com/vyos/vyos.vyos/blob/main/requirements.txt). `ansible-builder` picks these up when you build an execution environment that includes this collection. - VyOS devices reachable over SSH with the `ansible.netcommon.network_cli` connection. ## Included content ### Cliconf plugins Name | Description --- | --- [vyos.vyos.vyos](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_cliconf.rst)|Use vyos cliconf to run command on VyOS platform ### Modules Name | Description --- | --- [vyos.vyos.vyos_banner](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_banner_module.rst)|Manage multiline banners on VyOS devices [vyos.vyos.vyos_bgp_address_family](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_bgp_address_family_module.rst)|BGP Address Family resource module [vyos.vyos.vyos_bgp_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_bgp_global_module.rst)|BGP global resource module [vyos.vyos.vyos_command](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_command_module.rst)|Run one or more commands on VyOS devices [vyos.vyos.vyos_config](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_config_module.rst)|Manage VyOS configuration on remote device [vyos.vyos.vyos_facts](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_facts_module.rst)|Get facts about vyos devices. [vyos.vyos.vyos_file](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_file_module.rst)|Manage files, directories, and their ownership on VyOS devices [vyos.vyos.vyos_firewall_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_firewall_global_module.rst)|Firewall global resource module [vyos.vyos.vyos_firewall_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_firewall_interfaces_module.rst)|Firewall interfaces resource module [vyos.vyos.vyos_firewall_rules](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_firewall_rules_module.rst)|Firewall rules resource module [vyos.vyos.vyos_ha](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ha_module.rst)|Manage VRRP and load balancer configuration on VyOS [vyos.vyos.vyos_hostname](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_hostname_module.rst)|Manages hostname resource module [vyos.vyos.vyos_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_interfaces_module.rst)|Manages interface attributes of VyOS network devices. [vyos.vyos.vyos_l3_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_l3_interfaces_module.rst)|Layer 3 interfaces resource module. [vyos.vyos.vyos_lag_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lag_interfaces_module.rst)|LAG interfaces resource module [vyos.vyos.vyos_lldp_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lldp_global_module.rst)|LLDP global resource module [vyos.vyos.vyos_lldp_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lldp_interfaces_module.rst)|LLDP interfaces resource module [vyos.vyos.vyos_logging_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_logging_global_module.rst)|Logging resource module [vyos.vyos.vyos_nat](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_nat_module.rst)|NAT resource module +[vyos.vyos.vyos_nhrp](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_nhrp_module.rst)|NHRP resource module [vyos.vyos.vyos_ntp_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ntp_global_module.rst)|NTP global resource module [vyos.vyos.vyos_ospf_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospf_interfaces_module.rst)|OSPF Interfaces Resource Module. [vyos.vyos.vyos_ospfv2](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospfv2_module.rst)|OSPFv2 resource module [vyos.vyos.vyos_ospfv3](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospfv3_module.rst)|OSPFv3 resource module [vyos.vyos.vyos_ping](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ping_module.rst)|Tests reachability using ping from VyOS network devices [vyos.vyos.vyos_prefix_lists](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_prefix_lists_module.rst)|Prefix-Lists resource module for VyOS [vyos.vyos.vyos_route_maps](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_route_maps_module.rst)|Route Map resource module [vyos.vyos.vyos_snmp_server](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_snmp_server_module.rst)|Manages snmp_server resource module [vyos.vyos.vyos_static_routes](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_static_routes_module.rst)|Static routes resource module [vyos.vyos.vyos_system](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_system_module.rst)|Run `set system` commands on VyOS devices [vyos.vyos.vyos_user](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_user_module.rst)|Manage the collection of local users on VyOS device [vyos.vyos.vyos_vlan](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vlan_module.rst)|Manage VLANs on VyOS network devices [vyos.vyos.vyos_vpn_ipsec](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vpn_ipsec_module.rst)|Manages global IPsec (ike-group, esp-group, profile, authentication, options) attributes of VyOS network devices. [vyos.vyos.vyos_vpn_ipsec_s2s](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vpn_ipsec_s2s_module.rst)|Manages IPsec site-to-site VPN peers on VyOS network devices. [vyos.vyos.vyos_vrf](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vrf_module.rst)|VRF resource module Click the ``Content`` button to see the list of content included in this collection. ## Installing this collection You can install the VyOS collection with the Ansible Galaxy CLI: ansible-galaxy collection install vyos.vyos Red Hat customers should install this collection from [Red Hat Ansible Automation Hub](https://console.redhat.com/ansible/automation-hub/repo/published/vyos/vyos/). Configure Automation Hub as a Galaxy server in `ansible.cfg` as described in [Configuring Ansible Automation Hub](https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/), then use the same commands as below. Red Hat does not support installing collections from GitHub. To install a specific version, for example 6.2.0: ansible-galaxy collection install vyos.vyos:==6.2.0 To upgrade to the latest version: ansible-galaxy collection install vyos.vyos --upgrade You can also include it in a `requirements.yml` file and install it with `ansible-galaxy collection install -r requirements.yml`, using the format: ```yaml --- collections: - name: vyos.vyos ``` ## Using this collection This collection includes [network resource modules](https://docs.ansible.com/ansible/latest/network/user_guide/network_resource_modules.html). ### Using modules from the VyOS collection in your playbooks You can call modules by their Fully Qualified Collection Namespace (FQCN), such as `vyos.vyos.vyos_static_routes`. The following example task replaces configuration changes in the existing configuration on a VyOS network device, using the FQCN: ```yaml --- - name: Replace device configurations of listed static routes with provided configurations register: result vyos.vyos.vyos_static_routes: &id001 config: - address_families: - afi: ipv4 routes: - dest: 192.0.2.32/28 blackhole_config: distance: 2 next_hops: - forward_router_address: 192.0.2.7 - forward_router_address: 192.0.2.8 - forward_router_address: 192.0.2.9 state: replaced ``` **NOTE**: For Ansible 2.9, you may not see deprecation warnings when you run your playbooks with this collection. Use this documentation to track when a module is deprecated. ### See Also: * [VyOS Platform Options](https://docs.ansible.com/ansible/latest/network/user_guide/platform_vyos.html) * [Ansible Using collections](https://docs.ansible.com/ansible/latest/user_guide/collections_using.html) for more details. ## Contributing to this collection We welcome community contributions to this collection. If you find problems, please open an issue or create a PR against the [VyOS collection repository](https://github.com/vyos/vyos.vyos). See [Contributing to VyOS](https://vyos.net/contribute/) for complete details. You can also join us on: - Forum - https://forum.vyos.io See the [Contributing to VyOS](https://vyos.net/contribute/) for details on contributing to Ansible. ### Code of Conduct This collection follows the Ansible project's [Code of Conduct](https://docs.ansible.com/ansible/devel/community/code_of_conduct.html). Please read and familiarize yourself with this document. ### Updating from resource module models Some of our modules were templated using `resource_module_builder`, but some use the newer [`cli_rm_builder`](https://github.com/ansible-network/cli_rm_builder) which tempaltes baed on in-place device information, but also uses a new network parsing engine designed to simplify and standardize the parsing of network configuration. #### Using older *resource_module_builder* modules Last build was with a slightly-modified version of resource_module_builder. This changes the calling parameters for the resources. To update the collection from the resource module models, run the following command: ```bash ansible-playbook -e rm_dest=`pwd` \ -e structure=collection \ -e collection_org=vyos \ -e collection_name=vyos \ -e model=../../../resource_module_models/models/vyos/firewall_rules/vyos_firewall_rules.yaml \ ../../../resource_module_builder/site.yml ``` #### Using *cli_rm_builder* modules The newer `cli_rm_builder` works similarly to the older `resource_module_builder`, but pulls the information directly from the `DOCUMENTATION`, `EXAMPLES` and `RETURN` blocks in the module itself. To update the collection from the `cli_rm_builder` models, run the following command: ```bash ansible-playbook -e rm_dest=`pwd` \ -e collection_org=vyos \ -e collection_name=vyos \ -e resource=bgp_address_family \ ../../../cli_rm_builder/run.yml ``` Unlike the `resource_module_builder`, the `cli_rm_builder` does not require the `model` parameter. Instead, it uses the `resource` parameter to specify the resource to build. ### Testing playbooks You can use `ANSIBLE_COLLECTIONS_PATH` to test the collection locally. For example: ``` ANSIBLE_COLLECTIONS_PATHS=~/my_dev_path ansible-playbook -i inventory.network test.yml ``` ### Integration Tests Integration tests are run using `ansible-test` and require that there be an inventory defined (you can pass this in with `--inventory `) and that the system be configured for access (recommended to use SSH keys). Additionally: - eth0 should be configured for `address dhcp` and should have an assigned address on the local network - eth1 and eth2 should be defined and uncofirgured (they'll be overwritten by the tests) - eth3 and beyond should not be present or interface-related tests will fail - when using VMs for testing, ensure that the interfaces don't use `virtio`, as it will supress some interface configurations. `e1000e` is a good choice for testing. - eth0 is also expected to show `duplex auto` and `speed auto` in the output of `show interfaces`, however others are not due to the fact that they are repeatedly deleted and recreated which causes the default values to be hidden. ## Changelogs Change logs are available [here](https://github.com/vyos/vyos.vyos/blob/main/CHANGELOG.rst). ## Release notes Release notes are available [here](https://github.com/vyos/vyos.vyos/blob/main/CHANGELOG.rst). ## Roadmap Major Version | Ansible Support | VyOS Support | Details --- | --- | --- | --- 4.1.0 | 2.15 | 1.1.2 | Final release for the 4.x series 5.0.0 | 2.16 | 1.1.2 | First relase under VyOS control as a separate collection 6.0.0 | 2.18 | 1.3.8 | *Planned* release for supporting VyOS 1.3.8+ 7.0.0 | x.xx | 1.4.x | *Prospective* release deprecating incompatible 1.3.x modules Note: - Unreleased versions are not guaranteed to be released as described. - Some modules may support a wider variety of versions depending upon the compatibility with prior versions of VyOS. - The roadmap is subject to change based on community feedback and contributions. ## More information VyOS resources - [Contributing to VyOS](https://vyos.net/contribute) - [VyOS documentation](https://docs.vyos.io/en/latest/) - [VyOS forum](https://forum.vyos.io) Ansible Resources - [Ansible network resources](https://docs.ansible.com/ansible/latest/network/getting_started/network_resources.html) - [Ansible Collection overview](https://github.com/ansible-collections/overview) - [Ansible User guide](https://docs.ansible.com/ansible/latest/user_guide/index.html) - [Ansible Developer guide](https://docs.ansible.com/ansible/latest/dev_guide/index.html) - [Ansible Community code of conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html) ## Support As Red Hat Ansible Certified Content, this collection is entitled to support through Ansible Automation Platform (AAP) using the **Create issue** button in the top right corner of the collection page on Automation Hub. The collection is maintained by the VyOS team. If the collection was obtained from Galaxy or GitHub, community help is available through [GitHub issues](https://github.com/vyos/vyos.vyos/issues), the [VyOS forum](https://forum.vyos.io/) and the [Ansible Forum](https://forum.ansible.com/). ## Licensing GNU General Public License v3.0 or later. See [LICENSE](https://www.gnu.org/licenses/gpl-3.0.txt) to see the full text. diff --git a/changelogs/fragments/t9681_nhrp_module.yml b/changelogs/fragments/t9681_nhrp_module.yml new file mode 100644 index 00000000..44c11fa9 --- /dev/null +++ b/changelogs/fragments/t9681_nhrp_module.yml @@ -0,0 +1,5 @@ +--- +minor_changes: + - vyos_nhrp - new resource module for the NHRP configuration of tunnel interfaces (protocols nhrp), + as used for DMVPN - authentication, holdtime, MTU, network ID, multicast, redirect, shortcut, + registration-no-unique, static maps and next hop servers, with all resource module states. diff --git a/docs/vyos.vyos.vyos_nhrp_module.rst b/docs/vyos.vyos.vyos_nhrp_module.rst new file mode 100644 index 00000000..f0f2c35b --- /dev/null +++ b/docs/vyos.vyos.vyos_nhrp_module.rst @@ -0,0 +1,722 @@ +.. _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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsComments
+
+ 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 +
+
+ +
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 +
+
+ +
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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
KeyReturnedDescription
+
+ 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/meta/runtime.yml b/meta/runtime.yml index 7517c7c1..ab648980 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -1,78 +1,80 @@ --- requires_ansible: ">=2.16.0" plugin_routing: modules: banner: redirect: vyos.vyos.vyos_banner bgp_global: redirect: vyos.vyos.vyos_bgp_global bgp_address_family: redirect: vyos.vyos.vyos_bgp_address_family command: redirect: vyos.vyos.vyos_command config: redirect: vyos.vyos.vyos_config facts: redirect: vyos.vyos.vyos_facts file: redirect: vyos.vyos.vyos_file firewall_global: redirect: vyos.vyos.vyos_firewall_global firewall_interfaces: redirect: vyos.vyos.vyos_firewall_interfaces firewall_rules: redirect: vyos.vyos.vyos_firewall_rules hostname: redirect: vyos.vyos.vyos_hostname interfaces: redirect: vyos.vyos.vyos_interfaces l3_interfaces: redirect: vyos.vyos.vyos_l3_interfaces lag_interfaces: redirect: vyos.vyos.vyos_lag_interfaces lldp_global: redirect: vyos.vyos.vyos_lldp_global lldp_interfaces: redirect: vyos.vyos.vyos_lldp_interfaces logging: tombstone: removal_version: 6.0.0 warning_text: use vyos_logging_global instead vyos_logging: tombstone: removal_version: 6.0.0 warning_text: use vyos_logging_global instead logging_global: redirect: vyos.vyos.vyos_logging_global ntp_global: redirect: vyos.vyos.vyos_ntp_global nat: redirect: vyos.vyos.vyos_nat + nhrp: + redirect: vyos.vyos.vyos_nhrp ospfv2: redirect: vyos.vyos.vyos_ospfv2 ospfv3: redirect: vyos.vyos.vyos_ospfv3 ospf_interfaces: redirect: vyos.vyos.vyos_ospf_interfaces ping: redirect: vyos.vyos.vyos_ping prefix_lists: redirect: vyos.vyos.vyos_prefix_lists snmp_server: redirect: vyos.vyos.vyos_snmp_server static_routes: redirect: vyos.vyos.vyos_static_routes system: redirect: vyos.vyos.vyos_system user: redirect: vyos.vyos.vyos_user vlan: redirect: vyos.vyos.vyos_vlan vrf: redirect: vyos.vyos.vyos_vrf vpn_ipsec: redirect: vyos.vyos.vyos_vpn_ipsec vpn_ipsec_s2s: redirect: vyos.vyos.vyos_vpn_ipsec_s2s ha: redirect: vyos.vyos.vyos_ha diff --git a/plugins/action/nhrp.py b/plugins/action/nhrp.py new file mode 100644 index 00000000..24195be9 --- /dev/null +++ b/plugins/action/nhrp.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# 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 ansible_collections.vyos.vyos.plugins.action.vyos import ActionModule + + +__all__ = ["ActionModule"] diff --git a/plugins/module_utils/network/vyos/argspec/nhrp/__init__.py b/plugins/module_utils/network/vyos/argspec/nhrp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py b/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py new file mode 100644 index 00000000..de3d1c2a --- /dev/null +++ b/plugins/module_utils/network/vyos/argspec/nhrp/nhrp.py @@ -0,0 +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"}, + }, + }, + "nhs": { + "type": "list", + "elements": "dict", + "options": { + "tunnel_ip": {"type": "str", "required": True}, + "nbma": {"type": "list", "elements": "str"}, + }, + }, + }, + }, + }, + }, + "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/__init__.py b/plugins/module_utils/network/vyos/config/nhrp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/config/nhrp/nhrp.py b/plugins/module_utils/network/vyos/config/nhrp/nhrp.py new file mode 100644 index 00000000..527f1490 --- /dev/null +++ b/plugins/module_utils/network/vyos/config/nhrp/nhrp.py @@ -0,0 +1,152 @@ +# -*- 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) + + 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/module_utils/network/vyos/facts/facts.py b/plugins/module_utils/network/vyos/facts/facts.py index 3ca031f7..da4e38cf 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -1,150 +1,154 @@ # Copyright 2019 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The facts class for vyos this file validates each subset of facts and selectively calls the appropriate facts gathering function """ from __future__ import absolute_import, division, print_function __metaclass__ = type from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts import ( FactsBase, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family import ( Bgp_address_familyFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_global.bgp_global import ( Bgp_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.firewall_global.firewall_global import ( Firewall_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.firewall_interfaces.firewall_interfaces import ( Firewall_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.firewall_rules.firewall_rules import ( Firewall_rulesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ha.ha import ( HaFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.hostname.hostname import ( HostnameFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.interfaces.interfaces import ( InterfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.l3_interfaces.l3_interfaces import ( L3_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.lag_interfaces.lag_interfaces import ( Lag_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legacy.base import ( Config, Default, Neighbors, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.lldp_global.lldp_global import ( Lldp_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.lldp_interfaces.lldp_interfaces import ( Lldp_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.logging_global.logging_global import ( Logging_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.nat.nat import ( NatFacts, ) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.nhrp.nhrp import ( + NhrpFacts, +) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ntp_global.ntp_global import ( Ntp_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ospf_interfaces.ospf_interfaces import ( Ospf_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ospfv2.ospfv2 import ( Ospfv2Facts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ospfv3.ospfv3 import ( Ospfv3Facts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.prefix_lists.prefix_lists import ( Prefix_listsFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.route_maps.route_maps import ( Route_mapsFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.snmp_server.snmp_server import ( Snmp_serverFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.static_routes.static_routes import ( Static_routesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpn_ipsec.vpn_ipsec import ( Vpn_ipsecFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpn_ipsec_s2s.vpn_ipsec_s2s import ( Vpn_ipsec_s2sFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vrf.vrf import VrfFacts FACT_LEGACY_SUBSETS = dict(default=Default, neighbors=Neighbors, config=Config) FACT_RESOURCE_SUBSETS = dict( interfaces=InterfacesFacts, l3_interfaces=L3_interfacesFacts, lag_interfaces=Lag_interfacesFacts, lldp_global=Lldp_globalFacts, lldp_interfaces=Lldp_interfacesFacts, static_routes=Static_routesFacts, firewall_rules=Firewall_rulesFacts, firewall_global=Firewall_globalFacts, firewall_interfaces=Firewall_interfacesFacts, ha=HaFacts, ospfv3=Ospfv3Facts, ospfv2=Ospfv2Facts, ospf_interfaces=Ospf_interfacesFacts, bgp_global=Bgp_globalFacts, bgp_address_family=Bgp_address_familyFacts, route_maps=Route_mapsFacts, prefix_lists=Prefix_listsFacts, logging_global=Logging_globalFacts, ntp_global=Ntp_globalFacts, nat=NatFacts, + nhrp=NhrpFacts, snmp_server=Snmp_serverFacts, hostname=HostnameFacts, vrf=VrfFacts, vpn_ipsec=Vpn_ipsecFacts, vpn_ipsec_s2s=Vpn_ipsec_s2sFacts, ) class Facts(FactsBase): """The fact class for vyos""" VALID_LEGACY_GATHER_SUBSETS = frozenset(FACT_LEGACY_SUBSETS.keys()) VALID_RESOURCE_SUBSETS = frozenset(FACT_RESOURCE_SUBSETS.keys()) def __init__(self, module): super(Facts, self).__init__(module) def get_facts(self, legacy_facts_type=None, resource_facts_type=None, data=None): """Collect the facts for vyos :param legacy_facts_type: List of legacy facts types :param resource_facts_type: List of resource fact types :param data: previously collected conf :rtype: dict :return: the facts gathered """ if self.VALID_RESOURCE_SUBSETS: self.get_network_resources_facts(FACT_RESOURCE_SUBSETS, resource_facts_type, data) if self.VALID_LEGACY_GATHER_SUBSETS: self.get_network_legacy_facts(FACT_LEGACY_SUBSETS, legacy_facts_type) return self.ansible_facts, self._warnings diff --git a/plugins/module_utils/network/vyos/facts/nhrp/__init__.py b/plugins/module_utils/network/vyos/facts/nhrp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/facts/nhrp/nhrp.py b/plugins/module_utils/network/vyos/facts/nhrp/nhrp.py new file mode 100644 index 00000000..9177117e --- /dev/null +++ b/plugins/module_utils/network/vyos/facts/nhrp/nhrp.py @@ -0,0 +1,77 @@ +# -*- 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 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.vyos.vyos.plugins.module_utils.network.vyos.argspec.nhrp.nhrp import ( + NhrpArgs, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.nhrp import ( + NhrpTemplate, +) + + +class NhrpFacts(object): + """The vyos nhrp facts class""" + + def __init__(self, module, subspec="config", options="options"): + self._module = module + self.argument_spec = NhrpArgs.argument_spec + + def get_device_data(self, connection): + return connection.get("show configuration commands | grep 'protocols nhrp'") + + def populate_facts(self, connection, ansible_facts, data=None): + """Populate the facts for Nhrp network resource + :param connection: the device connection + :param ansible_facts: Facts dictionary + :param data: previously collected conf + :rtype: dictionary + :returns: facts + """ + if not data: + data = self.get_device_data(connection) + + lines = [line.replace("'", "") for line in data.splitlines()] + objs = NhrpTemplate(lines=lines, module=self._module).parse() + + tunnels = [] + for tunnel in (objs.get("tunnels") or {}).values(): + if "multicast" in tunnel: + tunnel["multicast"] = sorted(tunnel["multicast"].values()) + if "map" in tunnel: + tunnel["map"] = sorted(tunnel["map"].values(), key=lambda m: m["tunnel_ip"]) + if "nhs" in tunnel: + for nhs in tunnel["nhs"].values(): + nbma = sorted(v for v in (nhs.get("nbma") or {}).values() if v) + if nbma: + nhs["nbma"] = nbma + else: + nhs.pop("nbma", None) + tunnel["nhs"] = sorted(tunnel["nhs"].values(), key=lambda n: n["tunnel_ip"]) + tunnels.append(tunnel) + + facts = {"nhrp": {}} + if tunnels: + params = utils.validate_config( + self.argument_spec, + {"config": {"tunnels": sorted(tunnels, key=lambda t: t["name"])}}, + ) + facts["nhrp"] = utils.remove_empties(params["config"]) + + ansible_facts["ansible_network_resources"].update(facts) + return ansible_facts diff --git a/plugins/module_utils/network/vyos/rm_templates/nhrp.py b/plugins/module_utils/network/vyos/rm_templates/nhrp.py new file mode 100644 index 00000000..dad6b81f --- /dev/null +++ b/plugins/module_utils/network/vyos/rm_templates/nhrp.py @@ -0,0 +1,258 @@ +# -*- 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 Nhrp 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 NhrpTemplate(NetworkTemplate): + def __init__(self, lines=None, module=None): + prefix = {"set": "set", "remove": "delete"} + super(NhrpTemplate, self).__init__(lines=lines, tmplt=self, prefix=prefix, module=module) + + # Device lines reach the parser with their quotes removed (see the facts class). + # The first matching parser wins for each line, so the tunnel catch-all comes last. + # fmt: off + PARSERS = [ + # set protocols nhrp tunnel authentication + { + "name": "authentication", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+authentication + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} authentication '{{ authentication }}'", + "remval": "protocols nhrp tunnel {{ name }} authentication", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "authentication": "{{ authentication }}"}}}, + }, + # set protocols nhrp tunnel holdtime + { + "name": "holdtime", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+holdtime + \s+(?P\d+) + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} holdtime {{ holdtime }}", + "remval": "protocols nhrp tunnel {{ name }} holdtime", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "holdtime": "{{ holdtime }}"}}}, + }, + # set protocols nhrp tunnel mtu + { + "name": "mtu", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+mtu + \s+(?P\d+) + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} mtu {{ mtu }}", + "remval": "protocols nhrp tunnel {{ name }} mtu", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "mtu": "{{ mtu }}"}}}, + }, + # set protocols nhrp tunnel network-id + { + "name": "network_id", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+network-id + \s+(?P\d+) + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} network-id {{ network_id }}", + "remval": "protocols nhrp tunnel {{ name }} network-id", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "network_id": "{{ network_id }}"}}}, + }, + # set protocols nhrp tunnel redirect + { + "name": "redirect", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+redirect + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} redirect", + "remval": "protocols nhrp tunnel {{ name }} redirect", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "redirect": True}}}, + }, + # set protocols nhrp tunnel shortcut + { + "name": "shortcut", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+shortcut + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} shortcut", + "remval": "protocols nhrp tunnel {{ name }} shortcut", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "shortcut": True}}}, + }, + # set protocols nhrp tunnel registration-no-unique + { + "name": "registration_no_unique", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+registration-no-unique + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} registration-no-unique", + "remval": "protocols nhrp tunnel {{ name }} registration-no-unique", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "registration_no_unique": True}}}, + }, + # set protocols nhrp tunnel multicast (one line per value) + { + "name": "multicast", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+multicast + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} multicast {{ multicast }}", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}", "multicast": {"{{ multicast }}": "{{ multicast }}"}}}}, + }, + # set protocols nhrp tunnel map tunnel-ip
[nbma ] + { + "name": "map", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+map + \s+tunnel-ip + \s+(?P\S+) + (?:\s+nbma\s+(?P\S+))? + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} map tunnel-ip {{ map.tunnel_ip }}" + "{{ ' nbma ' + map.nbma if map.nbma is defined and map.nbma else '' }}", + "remval": "protocols nhrp tunnel {{ name }} map tunnel-ip {{ map.tunnel_ip }}", + "result": { + "tunnels": { + "{{ name }}": { + "name": "{{ name }}", + "map": {"{{ tunnel_ip }}": {"tunnel_ip": "{{ tunnel_ip }}", "nbma": "{{ nbma }}"}}, + }, + }, + }, + }, + # set protocols nhrp tunnel nhs tunnel-ip [nbma
] + # (one line per NBMA address) + { + "name": "nhs", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + \s+nhs + \s+tunnel-ip + \s+(?P\S+) + (?:\s+nbma\s+(?P\S+))? + \s*$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }} nhs tunnel-ip {{ nhs.tunnel_ip }}" + "{{ ' nbma ' + nhs.nbma if nhs.nbma is defined and nhs.nbma else '' }}", + "remval": "protocols nhrp tunnel {{ name }} nhs tunnel-ip {{ nhs.tunnel_ip }}", + "result": { + "tunnels": { + "{{ name }}": { + "name": "{{ name }}", + "nhs": {"{{ tunnel_ip }}": {"tunnel_ip": "{{ tunnel_ip }}", "nbma": {"{{ nbma }}": "{{ nbma }}"}}}, + }, + }, + }, + }, + # any other tunnel line: the tunnel exists + { + "name": "tunnel", + "getval": re.compile( + r""" + ^set + \s+protocols + \s+nhrp + \s+tunnel + \s+(?P\S+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "protocols nhrp tunnel {{ name }}", + "result": {"tunnels": {"{{ name }}": {"name": "{{ name }}"}}}, + }, + ] + # fmt: on diff --git a/plugins/modules/vyos_nhrp.py b/plugins/modules/vyos_nhrp.py new file mode 100644 index 00000000..70089668 --- /dev/null +++ b/plugins/modules/vyos_nhrp.py @@ -0,0 +1,388 @@ +#!/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 + 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 + 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/integration/targets/vyos_nhrp/defaults/main.yaml b/tests/integration/targets/vyos_nhrp/defaults/main.yaml new file mode 100644 index 00000000..164afead --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "[^_].*" +test_items: [] diff --git a/tests/integration/targets/vyos_nhrp/meta/main.yaml b/tests/integration/targets/vyos_nhrp/meta/main.yaml new file mode 100644 index 00000000..7413320e --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - prepare_vyos_tests diff --git a/tests/integration/targets/vyos_nhrp/tasks/cli.yaml b/tests/integration/targets/vyos_nhrp/tasks/cli.yaml new file mode 100644 index 00000000..daccf720 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tasks/cli.yaml @@ -0,0 +1,20 @@ +--- +- name: Collect all cli test cases + ansible.builtin.find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + use_regex: true + register: test_cases + delegate_to: localhost + +- name: Set test_items + ansible.builtin.set_fact: + test_items: "{{ test_cases.files | map(attribute='path') | list }}" + +- name: Run test case (connection=ansible.netcommon.network_cli) + ansible.builtin.include_tasks: "{{ test_case_to_run }}" + vars: + ansible_connection: ansible.netcommon.network_cli + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/tests/integration/targets/vyos_nhrp/tasks/main.yaml b/tests/integration/targets/vyos_nhrp/tasks/main.yaml new file mode 100644 index 00000000..e6378581 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tasks/main.yaml @@ -0,0 +1,5 @@ +--- +- name: Run CLI tests + ansible.builtin.include_tasks: cli.yaml + tags: + - network_cli diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/_create_tunnels.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/_create_tunnels.yaml new file mode 100644 index 00000000..434d84be --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/_create_tunnels.yaml @@ -0,0 +1,14 @@ +--- +- name: Create the tunnels NHRP runs on + vyos.vyos.vyos_config: + lines: + - set interfaces tunnel tun0 encapsulation 'gre' + - set interfaces tunnel tun0 source-interface 'eth0' + - set interfaces tunnel tun0 parameters ip key '1' + - set interfaces tunnel tun0 address '10.0.0.1/32' + - set interfaces tunnel tun1 encapsulation 'gre' + - set interfaces tunnel tun1 source-interface 'eth0' + - set interfaces tunnel tun1 parameters ip key '2' + - set interfaces tunnel tun1 address '10.1.0.1/32' + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/_parsed.cfg b/tests/integration/targets/vyos_nhrp/tests/cli/_parsed.cfg new file mode 100644 index 00000000..74d9adc9 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/_parsed.cfg @@ -0,0 +1,11 @@ +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 network-id '1' +set protocols nhrp tunnel tun0 redirect +set protocols nhrp tunnel tun0 registration-no-unique +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 diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/_populate_config.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/_populate_config.yaml new file mode 100644 index 00000000..8dcb3cdd --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/_populate_config.yaml @@ -0,0 +1,25 @@ +--- +- name: Populate the tunnels and their NHRP configuration + vyos.vyos.vyos_config: + lines: + - set interfaces tunnel tun0 encapsulation 'gre' + - set interfaces tunnel tun0 source-interface 'eth0' + - set interfaces tunnel tun0 parameters ip key '1' + - set interfaces tunnel tun0 address '10.0.0.1/32' + - set interfaces tunnel tun1 encapsulation 'gre' + - set interfaces tunnel tun1 source-interface 'eth0' + - set interfaces tunnel tun1 parameters ip key '2' + - set interfaces tunnel tun1 address '10.1.0.1/32' + - 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 network-id '1' + - set protocols nhrp tunnel tun0 redirect + - set protocols nhrp tunnel tun0 registration-no-unique + - 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 + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/_remove_config.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/_remove_config.yaml new file mode 100644 index 00000000..422aead6 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/_remove_config.yaml @@ -0,0 +1,18 @@ +--- +# NHRP first: VyOS refuses to delete a tunnel that NHRP uses +- name: Remove the NHRP configuration + vyos.vyos.vyos_config: + lines: + - delete protocols nhrp + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli + +- name: Remove the tunnels + vyos.vyos.vyos_config: + lines: + - delete interfaces tunnel tun0 + - delete interfaces tunnel tun1 + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/deleted.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/deleted.yaml new file mode 100644 index 00000000..b6f1c272 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/deleted.yaml @@ -0,0 +1,31 @@ +--- +- debug: + msg: START vyos_nhrp deleted integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Delete all NHRP configuration + register: result + vyos.vyos.vyos_nhrp: &id001 + state: deleted + + - assert: + that: + - result.changed == true + - result.commands == deleted.commands + - result.after == {} + + - name: Delete all NHRP configuration (IDEMPOTENT) + register: result + vyos.vyos.vyos_nhrp: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/empty_config.yaml new file mode 100644 index 00000000..075394f9 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/empty_config.yaml @@ -0,0 +1,58 @@ +--- +- debug: + msg: START vyos_nhrp empty_config integration tests on connection={{ ansible_connection }} + +- name: Merged with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_nhrp: + config: + state: merged + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state merged' + +- name: Replaced with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_nhrp: + config: + state: replaced + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state replaced' + +- name: Overridden with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_nhrp: + config: + state: overridden + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state overridden' + +- name: Rendered with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_nhrp: + config: + state: rendered + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state rendered' + +- name: Parsed with empty running_config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_nhrp: + running_config: + state: parsed + +- assert: + that: + - result.msg == 'value of running_config parameter must not be empty for state parsed' diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/gathered.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/gathered.yaml new file mode 100644 index 00000000..c81344b2 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/gathered.yaml @@ -0,0 +1,25 @@ +--- +- debug: + msg: START vyos_nhrp gathered integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Gather the NHRP configuration + register: result + vyos.vyos.vyos_nhrp: + state: gathered + + - vyos.vyos.vyos_facts: + gather_network_resources: nhrp + + - assert: + that: + - result.changed == false + - result.gathered == parsed.after + - result.gathered == ansible_facts['network_resources']['nhrp'] + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/merged.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/merged.yaml new file mode 100644 index 00000000..2864c491 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/merged.yaml @@ -0,0 +1,39 @@ +--- +- debug: + msg: START vyos_nhrp merged integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _create_tunnels.yaml + +- block: + - name: Merge the provided configuration with the existing running configuration + register: result + vyos.vyos.vyos_nhrp: &id001 + config: + tunnels: + - "{{ hub }}" + - "{{ spoke }}" + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: nhrp + + - assert: + that: + - result.changed == true + - result.before == {} + - result.commands|symmetric_difference(merged.commands) == [] + - result.after == merged.after + + - name: Merge the provided configuration with the existing running configuration (IDEMPOTENT) + register: result + vyos.vyos.vyos_nhrp: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/overridden.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/overridden.yaml new file mode 100644 index 00000000..8f68b18c --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/overridden.yaml @@ -0,0 +1,34 @@ +--- +- debug: + msg: START vyos_nhrp overridden integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Override the NHRP configuration with the hub tunnel only + register: result + vyos.vyos.vyos_nhrp: &id001 + config: + tunnels: + - "{{ hub }}" + state: overridden + + - assert: + that: + - result.changed == true + - result.commands|symmetric_difference(overridden.commands) == [] + - result.after == overridden.after + + - name: Override the NHRP configuration with the hub tunnel only (IDEMPOTENT) + register: result + vyos.vyos.vyos_nhrp: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/parsed.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/parsed.yaml new file mode 100644 index 00000000..e6a89ff2 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/parsed.yaml @@ -0,0 +1,14 @@ +--- +- debug: + msg: START vyos_nhrp parsed integration tests on connection={{ ansible_connection }} + +- name: Parse the provided running configuration + register: result + vyos.vyos.vyos_nhrp: + running_config: "{{ lookup('file', '_parsed.cfg') }}" + state: parsed + +- assert: + that: + - result.changed == false + - result.parsed == parsed.after diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/rendered.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/rendered.yaml new file mode 100644 index 00000000..d262508f --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/rendered.yaml @@ -0,0 +1,17 @@ +--- +- debug: + msg: START vyos_nhrp rendered integration tests on connection={{ ansible_connection }} + +- name: Render the configuration as device commands + register: result + vyos.vyos.vyos_nhrp: + config: + tunnels: + - "{{ hub }}" + - "{{ spoke }}" + state: rendered + +- assert: + that: + - result.changed == false + - result.rendered|symmetric_difference(merged.commands) == [] diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/replaced.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/replaced.yaml new file mode 100644 index 00000000..a4aafd6e --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/replaced.yaml @@ -0,0 +1,43 @@ +--- +- debug: + msg: START vyos_nhrp replaced integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Replace the configuration of both tunnels + register: result + vyos.vyos.vyos_nhrp: &id001 + config: + tunnels: + - name: tun0 + authentication: newpass + multicast: + - dynamic + - name: tun1 + nhs: + - tunnel_ip: dynamic + nbma: + - 192.0.2.1 + shortcut: true + state: replaced + + - assert: + that: + - result.changed == true + - result.commands|symmetric_difference(replaced.commands) == [] + - result.after == replaced.after + + - name: Replace the configuration of both tunnels (IDEMPOTENT) + register: result + vyos.vyos.vyos_nhrp: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_nhrp/tests/cli/rtt.yaml b/tests/integration/targets/vyos_nhrp/tests/cli/rtt.yaml new file mode 100644 index 00000000..12758d75 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/tests/cli/rtt.yaml @@ -0,0 +1,57 @@ +--- +- debug: + msg: START vyos_nhrp round trip integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _create_tunnels.yaml + +- block: + - name: Apply the provided configuration (base config) + register: base_config + vyos.vyos.vyos_nhrp: + config: + tunnels: + - "{{ hub }}" + - "{{ spoke }}" + state: merged + + - name: Gather NHRP facts + vyos.vyos.vyos_facts: + gather_subset: + - default + gather_network_resources: + - nhrp + + - name: Apply the provided configuration (config to be reverted) + register: result + vyos.vyos.vyos_nhrp: + config: + tunnels: + - name: tun0 + holdtime: 600 + multicast: + - 10.0.0.255 + - name: tun1 + nhs: + - tunnel_ip: dynamic + nbma: + - 203.0.113.1 + state: merged + + - name: Assert that changes were applied + assert: + that: result['after'] == round_trip['after'] + + - name: Revert back to base config using facts round trip + register: revert + vyos.vyos.vyos_nhrp: + config: "{{ ansible_facts['network_resources']['nhrp'] }}" + state: overridden + + - name: Assert that config was reverted + assert: + that: revert['after'] == base_config['after'] + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_nhrp/vars/main.yaml b/tests/integration/targets/vyos_nhrp/vars/main.yaml new file mode 100644 index 00000000..6c445081 --- /dev/null +++ b/tests/integration/targets/vyos_nhrp/vars/main.yaml @@ -0,0 +1,127 @@ +--- +hub: &hub + name: tun0 + authentication: secret + holdtime: 300 + multicast: + - dynamic + network_id: 1 + redirect: true + registration_no_unique: true + +spoke: &spoke + name: tun1 + map: + - tunnel_ip: 10.0.0.2 + nbma: 192.0.2.2 + mtu: 1400 + nhs: + - tunnel_ip: dynamic + nbma: + - 192.0.2.1 + - 198.51.100.1 + shortcut: true + +merged: + commands: + - set protocols nhrp tunnel tun0 authentication '********' + - set protocols nhrp tunnel tun0 holdtime 300 + - set protocols nhrp tunnel tun0 network-id 1 + - set protocols nhrp tunnel tun0 redirect + - set protocols nhrp tunnel tun0 registration-no-unique + - set protocols nhrp tunnel tun0 multicast dynamic + - set protocols nhrp tunnel tun1 mtu 1400 + - set protocols nhrp tunnel tun1 shortcut + - set protocols nhrp tunnel tun1 map tunnel-ip 10.0.0.2 nbma 192.0.2.2 + - 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 + after: + tunnels: + - name: tun0 + authentication: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + holdtime: 300 + multicast: + - dynamic + network_id: 1 + redirect: true + registration_no_unique: true + - *spoke + +replaced: + commands: + - set protocols nhrp tunnel tun0 authentication '********' + - 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 + after: + tunnels: + - name: tun0 + authentication: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + multicast: + - dynamic + - name: tun1 + nhs: + - tunnel_ip: dynamic + nbma: + - 192.0.2.1 + shortcut: true + +overridden: + commands: + - delete protocols nhrp tunnel tun1 + after: + tunnels: + - name: tun0 + authentication: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + holdtime: 300 + multicast: + - dynamic + network_id: 1 + redirect: true + registration_no_unique: true + +deleted: + commands: + - delete protocols nhrp + +parsed: + after: + tunnels: + - name: tun0 + authentication: "secret" + holdtime: 300 + multicast: + - dynamic + network_id: 1 + redirect: true + registration_no_unique: true + - *spoke + +round_trip: + after: + tunnels: + - name: tun0 + authentication: secret + holdtime: 600 + multicast: + - 10.0.0.255 + - dynamic + network_id: 1 + redirect: true + registration_no_unique: true + - name: tun1 + map: + - tunnel_ip: 10.0.0.2 + nbma: 192.0.2.2 + mtu: 1400 + nhs: + - tunnel_ip: dynamic + nbma: + - 192.0.2.1 + - 198.51.100.1 + - 203.0.113.1 + shortcut: true diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_nhrp_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_nhrp_config.cfg new file mode 100644 index 00000000..12b4d1f3 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_nhrp_config.cfg @@ -0,0 +1,13 @@ +set protocols nhrp tunnel tun0 authentication 'secret' +set protocols nhrp tunnel tun0 holdtime '600' +set protocols nhrp tunnel tun0 multicast '10.0.0.255' +set protocols nhrp tunnel tun0 multicast 'dynamic' +set protocols nhrp tunnel tun0 network-id '1' +set protocols nhrp tunnel tun0 redirect +set protocols nhrp tunnel tun0 registration-no-unique +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 nhs tunnel-ip 'dynamic' nbma '203.0.113.1' +set protocols nhrp tunnel tun1 shortcut diff --git a/tests/unit/modules/network/vyos/test_vyos_nhrp.py b/tests/unit/modules/network/vyos/test_vyos_nhrp.py new file mode 100644 index 00000000..63a70d78 --- /dev/null +++ b/tests/unit/modules/network/vyos/test_vyos_nhrp.py @@ -0,0 +1,203 @@ +# -*- 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_overridden_round_trip_revert(self): + # the gathered base configuration, fed back with overridden, undoes the changes + set_module_args(dict(config=dict(tunnels=[HUB, SPOKE]), state="overridden")) + commands = [ + "set protocols nhrp tunnel tun0 holdtime 300", + "delete protocols nhrp tunnel tun0 multicast 10.0.0.255", + "delete protocols nhrp tunnel tun1 nhs tunnel-ip dynamic nbma 203.0.113.1", + ] + self.execute_module( + changed=True, + commands=commands, + filename="vyos_nhrp_config_changed.cfg", + )