diff options
| author | omnom62 <75066712+omnom62@users.noreply.github.com> | 2026-03-08 20:53:15 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-08 12:53:15 +0200 |
| commit | ab0aa4bd1a16b044f061053aa78b0fa865312545 (patch) | |
| tree | fc8c29f22e4c35738ff24bc3cc5cc254d61e9f75 | |
| parent | a8e5dac21090e9b0ddf876214a9a1522079ecde1 (diff) | |
| download | vyos.vyos-ab0aa4bd1a16b044f061053aa78b0fa865312545.tar.gz vyos.vyos-ab0aa4bd1a16b044f061053aa78b0fa865312545.zip | |
T8104: Add static routes multiple next-hop targets (#444)
* Mid fixes
* Ready for testing
* Fixing SR interface
* Fix is ready
* changelog
* Integration tests for static_routes interface next-hop v.1.4+ WIP
* branching 1.3- and 1.4+
* Integration tests for T8104 complete
* Comments are resolved
* Comments are resolved
13 files changed, 335 insertions, 9 deletions
diff --git a/changelogs/fragments/t8104-vyos_static_routes.yaml b/changelogs/fragments/t8104-vyos_static_routes.yaml new file mode 100644 index 00000000..2928b324 --- /dev/null +++ b/changelogs/fragments/t8104-vyos_static_routes.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - vyos_static_routes - Fixed interface handling in next-hop parsing diff --git a/plugins/module_utils/network/vyos/facts/static_routes/static_routes.py b/plugins/module_utils/network/vyos/facts/static_routes/static_routes.py index 99b3917b..710d91fe 100644 --- a/plugins/module_utils/network/vyos/facts/static_routes/static_routes.py +++ b/plugins/module_utils/network/vyos/facts/static_routes/static_routes.py @@ -164,9 +164,9 @@ class Static_routesFacts(object): elif dis >= 1: nh_info["enabled"] = False for element in nh_list: - if element["forward_router_address"] == nh_info["forward_router_address"]: - if "interface" in nh_info.keys(): - element["interface"] = nh_info["interface"] + if element.get("forward_router_address") == nh_info.get( + "forward_router_address", + ): if "admin_distance" in nh_info.keys(): element["admin_distance"] = nh_info["admin_distance"] if "enabled" in nh_info.keys(): @@ -174,4 +174,5 @@ class Static_routesFacts(object): nh_info = None if nh_info is not None: nh_list.append(nh_info) + nh_info = {} return nh_list diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/_get_version.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/_get_version.yaml new file mode 100644 index 00000000..2588b194 --- /dev/null +++ b/tests/integration/targets/vyos_static_routes/tests/cli/_get_version.yaml @@ -0,0 +1,28 @@ +- name: make sure to get facts + vyos.vyos.vyos_facts: + vars: + ansible_connection: ansible.netcommon.network_cli + register: vyos_facts + when: vyos_version is not defined + +- name: debug vyos_facts + debug: + var: vyos_facts + +- name: pull version from facts + set_fact: + vyos_version: "{{ vyos_facts.ansible_facts.ansible_net_version.split('-')[0].split(' ')[-1] }}" + when: vyos_version is not defined + +- name: fix '.0' versions + set_fact: + vyos_version: "{{ vyos_version }}.0" + when: vyos_version.count('.') == 1 + +- name: include correct vars + include_vars: pre-v1_4.yaml + when: vyos_version is version('1.4.0', '<', version_type='semver') + +- name: include correct vars + include_vars: v1_4.yaml + when: vyos_version is version('1.4.0', '>=', version_type='semver') diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/_parsed_config.cfg b/tests/integration/targets/vyos_static_routes/tests/cli/_parsed_config.cfg index b2ecd4e9..8d6638cb 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/_parsed_config.cfg +++ b/tests/integration/targets/vyos_static_routes/tests/cli/_parsed_config.cfg @@ -1,3 +1,4 @@ +set protocols static route 192.0.2.32/28 interface 'eth1' set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' set protocols static route 192.0.2.32/28 blackhole diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/_populate.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/_populate.yaml index 52d760da..4b57d5f2 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/_populate.yaml +++ b/tests/integration/targets/vyos_static_routes/tests/cli/_populate.yaml @@ -1,11 +1,31 @@ --- - ansible.builtin.include_tasks: _remove_config.yaml -- name: Setup +- name: ensure facts + include_tasks: _get_version.yaml + +- name: Setup 1.4- + vyos.vyos.vyos_config: + lines: + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' + - set protocols static interface-route 192.0.2.32/28 next-hop-interface eth1 + - set protocols static route 192.0.2.32/28 blackhole + - set protocols static route 192.0.2.32/28 + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' + - set protocols static route6 2001:db8:1000::/36 blackhole distance '2' + - set protocols static route6 2001:db8:1000::/36 + vars: + ansible_connection: ansible.netcommon.network_cli + when: vyos_version is version('1.4.0', '<', version_type='semver') + +- name: Setup 1.4+ vyos.vyos.vyos_config: lines: - set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' - set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' + - set protocols static route 192.0.2.32/28 interface eth1 - set protocols static route 192.0.2.32/28 blackhole - set protocols static route 192.0.2.32/28 - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' @@ -14,3 +34,4 @@ - set protocols static route6 2001:db8:1000::/36 vars: ansible_connection: ansible.netcommon.network_cli + when: vyos_version is version('1.4.0', '>=', version_type='semver') diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/merged.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/merged.yaml index 273f4608..0398a030 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/merged.yaml +++ b/tests/integration/targets/vyos_static_routes/tests/cli/merged.yaml @@ -2,6 +2,8 @@ - debug: msg: START vyos_static_routes merged integration tests on connection={{ ansible_connection }} +- include_tasks: _populate.yaml + - include_tasks: _remove_config.yaml - block: @@ -17,8 +19,8 @@ type: blackhole next_hops: - forward_router_address: 192.0.2.10 - - forward_router_address: 192.0.2.9 + - interface: eth2 - address_families: - afi: ipv6 @@ -28,7 +30,6 @@ distance: 2 next_hops: - forward_router_address: 2001:db8:2000:2::1 - - forward_router_address: 2001:db8:2000:2::2 state: merged @@ -39,7 +40,7 @@ - name: Assert that correct set of commands were generated assert: that: - - "{{ merged['commands'] | symmetric_difference(result['commands']) |length == 0 }}" + - "{{ merged.commands | symmetric_difference(result.commands) | length == 0 }}" - name: Assert that after dicts was correctly generated assert: diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/overridden.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/overridden.yaml index da5aff47..19f6774a 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/overridden.yaml +++ b/tests/integration/targets/vyos_static_routes/tests/cli/overridden.yaml @@ -15,6 +15,7 @@ - dest: 198.0.2.48/28 next_hops: - forward_router_address: 192.0.2.18 + - interface: eth2 state: overridden - name: Assert that before dicts were correctly generated diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/rendered.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/rendered.yaml index 761cff7a..9d933140 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/rendered.yaml +++ b/tests/integration/targets/vyos_static_routes/tests/cli/rendered.yaml @@ -17,7 +17,6 @@ type: blackhole next_hops: - forward_router_address: 192.0.2.10 - - forward_router_address: 192.0.2.9 - address_families: @@ -28,8 +27,8 @@ distance: 2 next_hops: - forward_router_address: 2001:db8:2000:2::1 - - forward_router_address: 2001:db8:2000:2::2 + - interface: eth2 state: rendered - name: Assert that correct set of commands were generated diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/replaced.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/replaced.yaml index 26150e57..8f2a3c57 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/replaced.yaml +++ b/tests/integration/targets/vyos_static_routes/tests/cli/replaced.yaml @@ -21,6 +21,8 @@ - forward_router_address: 192.0.2.8 - forward_router_address: 192.0.2.9 + + - interface: eth2 state: replaced - name: Assert that correct set of commands were generated diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/rtt.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/rtt.yaml index 2f8d475b..c59165ef 100644 --- a/tests/integration/targets/vyos_static_routes/tests/cli/rtt.yaml +++ b/tests/integration/targets/vyos_static_routes/tests/cli/rtt.yaml @@ -2,6 +2,8 @@ - debug: msg: START vyos_static_routes round trip integration tests on connection={{ ansible_connection }} +- include_tasks: _get_version.yaml + - include_tasks: _remove_config.yaml - block: @@ -52,6 +54,8 @@ next_hops: - forward_router_address: 192.0.2.7 + - interface: "eth2" + - forward_router_address: 192.0.2.8 - forward_router_address: 192.0.2.9 diff --git a/tests/integration/targets/vyos_static_routes/vars/main.yaml b/tests/integration/targets/vyos_static_routes/vars/main.bak index 6ce4cea6..6ce4cea6 100644 --- a/tests/integration/targets/vyos_static_routes/vars/main.yaml +++ b/tests/integration/targets/vyos_static_routes/vars/main.bak diff --git a/tests/integration/targets/vyos_static_routes/vars/pre-v1_4.yaml b/tests/integration/targets/vyos_static_routes/vars/pre-v1_4.yaml new file mode 100644 index 00000000..f739eb26 --- /dev/null +++ b/tests/integration/targets/vyos_static_routes/vars/pre-v1_4.yaml @@ -0,0 +1,131 @@ +--- +merged: + before: [] + commands: + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' + - set protocols static interface-route 192.0.2.32/28 next-hop-interface 'eth2' + - set protocols static route 192.0.2.32/28 blackhole + - set protocols static route 192.0.2.32/28 + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' + - set protocols static route6 2001:db8:1000::/36 blackhole distance '2' + - set protocols static route6 2001:db8:1000::/36 + after: + - address_families: + - afi: ipv4 + routes: + - dest: 192.0.2.32/28 + blackhole_config: + type: blackhole + next_hops: + - interface: eth2 + - forward_router_address: 192.0.2.9 + - forward_router_address: 192.0.2.10 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 +populate: + - address_families: + - afi: ipv4 + routes: + - dest: 192.0.2.32/28 + blackhole_config: + type: blackhole + next_hops: + - forward_router_address: 192.0.2.9 + - forward_router_address: 192.0.2.10 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - interface: eth1 + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 +replaced: + commands: + - delete protocols static route 192.0.2.32/28 next-hop-interface 'eth1' + - delete protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.7' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.8' + - set protocols static route 192.0.2.32/28 blackhole distance '2' + - set protocols static interface-route 192.0.2.32/28 next-hop-interface 'eth2' + after: + - 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 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 +overridden: + commands: + - delete protocols static route 192.0.2.32/28 + - delete protocols static route6 2001:db8:1000::/36 + - set protocols static route 198.0.2.48/28 next-hop '192.0.2.18' + - set protocols static route 198.0.2.48/28 + - set protocols static interface-route 198.0.2.48/28 next-hop-interface 'eth2' + after: + - address_families: + - afi: ipv4 + routes: + - dest: 198.0.2.48/28 + next_hops: + - interface: eth2 + - forward_router_address: 192.0.2.18 +rendered: + commands: + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' + - set protocols static route 192.0.2.32/28 blackhole + - set protocols static route 192.0.2.32/28 + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' + - set protocols static route6 2001:db8:1000::/36 blackhole distance '2' + - set protocols static route6 2001:db8:1000::/36 + - set protocols static interface-route6 2001:db8:1000::/36 next-hop-interface 'eth2' + +deleted_afi_all: + commands: + - delete protocols static route + - delete protocols static route6 + after: [] +round_trip: + after: + - 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 + - forward_router_address: 192.0.2.10 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 diff --git a/tests/integration/targets/vyos_static_routes/vars/v1_4.yaml b/tests/integration/targets/vyos_static_routes/vars/v1_4.yaml new file mode 100644 index 00000000..ae88711b --- /dev/null +++ b/tests/integration/targets/vyos_static_routes/vars/v1_4.yaml @@ -0,0 +1,134 @@ +--- +merged: + before: [] + commands: + - set protocols static route 192.0.2.32/28 + - set protocols static route 192.0.2.32/28 blackhole + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' + - set protocols static route 192.0.2.32/28 interface 'eth2' + - set protocols static route6 2001:db8:1000::/36 + - set protocols static route6 2001:db8:1000::/36 blackhole distance '2' + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' + after: + - address_families: + - afi: ipv4 + routes: + - dest: 192.0.2.32/28 + blackhole_config: + type: blackhole + next_hops: + - interface: eth2 + - forward_router_address: 192.0.2.9 + - forward_router_address: 192.0.2.10 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 +populate: + - address_families: + - afi: ipv4 + routes: + - dest: 192.0.2.32/28 + blackhole_config: + type: blackhole + next_hops: + - interface: eth1 + - forward_router_address: 192.0.2.9 + - forward_router_address: 192.0.2.10 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 +replaced: + commands: + - delete protocols static route 192.0.2.32/28 interface 'eth1' + - delete protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.7' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.8' + - set protocols static route 192.0.2.32/28 blackhole distance '2' + - set protocols static route 192.0.2.32/28 interface 'eth2' + + after: + - address_families: + - afi: ipv4 + routes: + - dest: 192.0.2.32/28 + blackhole_config: + distance: 2 + next_hops: + - interface: eth2 + - forward_router_address: 192.0.2.7 + - forward_router_address: 192.0.2.8 + - forward_router_address: 192.0.2.9 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 +overridden: + commands: + - delete protocols static route 192.0.2.32/28 + - delete protocols static route6 2001:db8:1000::/36 + - set protocols static route 198.0.2.48/28 next-hop '192.0.2.18' + - set protocols static route 198.0.2.48/28 + - set protocols static route 198.0.2.48/28 interface 'eth2' + after: + - address_families: + - afi: ipv4 + routes: + - dest: 198.0.2.48/28 + next_hops: + - interface: eth2 + - forward_router_address: 192.0.2.18 +rendered: + commands: + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.10' + - set protocols static route 192.0.2.32/28 next-hop '192.0.2.9' + - set protocols static route 192.0.2.32/28 blackhole + - set protocols static route 192.0.2.32/28 + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' + - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' + - set protocols static route6 2001:db8:1000::/36 blackhole distance '2' + - set protocols static route6 2001:db8:1000::/36 + - set protocols static route6 2001:db8:1000::/36 interface 'eth2' + +deleted_afi_all: + commands: + - delete protocols static route + - delete protocols static route6 + after: [] +round_trip: + after: + - address_families: + - afi: ipv4 + routes: + - dest: 192.0.2.32/28 + blackhole_config: + distance: 2 + next_hops: + - interface: eth2 + - forward_router_address: 192.0.2.7 + - forward_router_address: 192.0.2.8 + - forward_router_address: 192.0.2.9 + - forward_router_address: 192.0.2.10 + - afi: ipv6 + routes: + - dest: 2001:db8:1000::/36 + blackhole_config: + distance: 2 + next_hops: + - forward_router_address: 2001:db8:2000:2::1 + - forward_router_address: 2001:db8:2000:2::2 |
