diff --git a/plugins/module_utils/network/iosxr/config/static_routes/static_routes.py b/plugins/module_utils/network/iosxr/config/static_routes/static_routes.py index 0d56acc5c..a3b0cc35c 100644 --- a/plugins/module_utils/network/iosxr/config/static_routes/static_routes.py +++ b/plugins/module_utils/network/iosxr/config/static_routes/static_routes.py @@ -231,7 +231,10 @@ def _state_replaced(self, want, have): if "." in item or ":" in item or "/" in item: cmd += " {0}".format(item) else: - cmd += " vrf {0}".format(item) + if "vrf" in want: + cmd += " vrf {0}".format(item) + else: + cmd += " {0}".format(item) update_commands.append(cmd) for key, value in iteritems(rotated_want_next_hops): @@ -251,6 +254,7 @@ def _state_replaced(self, want, have): dest=want_route["dest"], next_hop=key, updates=updates, + in_vrf="vrf" in want, ), ) @@ -421,6 +425,7 @@ def _state_merged(self, want, have): rotated_have_next_hops.get(key, {}), updates, ), + in_vrf="vrf" in want, ), ) @@ -476,7 +481,10 @@ def _static_route_popper(self, want_afi, have_afi): if "." in item or ":" in item or "/" in item: cmd += " {0}".format(item) else: - cmd += " vrf {0}".format(item) + if "vrf" in want_afi: + cmd += " vrf {0}".format(item) + else: + cmd += " {0}".format(item) update_commands.append(cmd) if update_commands: update_commands.insert( @@ -580,7 +588,7 @@ def rotate_next_hops(self, next_hops): return next_hops_dict - def _compute_commands(self, dest, next_hop, updates=None): + def _compute_commands(self, dest, next_hop, updates=None, in_vrf=False): """This method computes a static route entry command from the specified `dest`, `next_hop` and `updates` @@ -593,7 +601,14 @@ def _compute_commands(self, dest, next_hop, updates=None): command = dest for x in next_hop: - command += " {0}".format(x) + if "." in x or ":" in x or "/" in x: + command += " {0}".format(x) + else: + # Only add 'vrf' if in VRF context and not in normal context + if in_vrf: + command += " vrf {0}".format(x) + else: + command += " {0}".format(x) for key in sorted(updates): if key == "admin_distance": diff --git a/tests/integration/targets/iosxr_static_routes/tests/cli/merged.yaml b/tests/integration/targets/iosxr_static_routes/tests/cli/merged.yaml index c848e7c2b..4533f6ffb 100644 --- a/tests/integration/targets/iosxr_static_routes/tests/cli/merged.yaml +++ b/tests/integration/targets/iosxr_static_routes/tests/cli/merged.yaml @@ -29,6 +29,10 @@ - forward_router_address: 192.0.2.11 admin_distance: 100 + - dest: 192.168.17.0/24 + next_hops: + - interface: Loopback0 + - afi: ipv6 safi: unicast routes: @@ -39,6 +43,9 @@ - interface: FastEthernet0/0/0/8 forward_router_address: 2001:db8:2000:2::1 + - dest: 2001:db8::/64 + next_hops: + - interface: Loopback0 - vrf: DEV_SITE address_families: @@ -58,6 +65,14 @@ dest_vrf: test_1 track: ip_sla_2 vrflabel: 124 + - vrf: TEST_VRF + address_families: + - afi: ipv4 + safi: unicast + routes: + - dest: 192.1.0.0/24 + next_hops: + - interface: Loopback1 state: merged - name: Assert that before dicts were correctly generated diff --git a/tests/integration/targets/iosxr_static_routes/vars/main.yaml b/tests/integration/targets/iosxr_static_routes/vars/main.yaml index b12c66984..a82b405c8 100644 --- a/tests/integration/targets/iosxr_static_routes/vars/main.yaml +++ b/tests/integration/targets/iosxr_static_routes/vars/main.yaml @@ -8,9 +8,11 @@ merged: 10 - 192.0.2.16/28 FastEthernet0/0/0/5 track ip_sla_1 - 192.0.2.32/28 192.0.2.11 100 + - 192.168.17.0/24 Loopback0 - address-family ipv6 unicast - 2001:db8:1000::/36 FastEthernet0/0/0/7 description DC - 2001:db8:1000::/36 2001:db8:2000:2::1 FastEthernet0/0/0/8 + - 2001:db8::/64 Loopback0 - vrf DEV_SITE - address-family ipv4 unicast - 192.0.2.48/28 vrf test_1 192.0.2.12 description DEV @@ -23,6 +25,9 @@ merged: - 192.0.2.48/28 vrf test_1 192.0.2.12 description DEV vrflabel 2301 - 192.0.2.80/28 vrf test_1 192.0.2.14 FastEthernet0/0/0/2 description rt_test_1 track ip_sla_2 vrflabel 124 + - vrf TEST_VRF + - address-family ipv4 unicast + - 192.1.0.0/24 Loopback1 after: - address_families: - afi: ipv4 @@ -40,6 +45,9 @@ merged: next_hops: - admin_distance: 100 forward_router_address: 192.0.2.11 + - dest: 192.168.17.0/24 + next_hops: + - interface: Loopback0 safi: unicast - afi: ipv6 routes: @@ -49,6 +57,9 @@ merged: interface: FastEthernet0/0/0/7 - forward_router_address: 2001:db8:2000:2::1 interface: FastEthernet0/0/0/8 + - dest: 2001:db8::/64 + next_hops: + - interface: Loopback0 safi: unicast - address_families: - afi: ipv4 @@ -67,6 +78,14 @@ merged: vrflabel: 124 safi: unicast vrf: DEV_SITE + - address_families: + - afi: ipv4 + routes: + - dest: 192.1.0.0/24 + next_hops: + - interface: Loopback1 + safi: unicast + vrf: TEST_VRF update_after: - address_families: - afi: ipv4