diff --git a/changelogs/fragments/win_route.yaml b/changelogs/fragments/win_route.yaml new file mode 100644 index 00000000000..6a013c563ec --- /dev/null +++ b/changelogs/fragments/win_route.yaml @@ -0,0 +1,2 @@ +bugfixes: +- win_route - Corrected issue where the wrong network interface was used for new static routes. - https://github.com/ansible/ansible/issues/28051 diff --git a/lib/ansible/modules/windows/win_route.ps1 b/lib/ansible/modules/windows/win_route.ps1 index 895d97d7f0a..d2fe5cf989e 100644 --- a/lib/ansible/modules/windows/win_route.ps1 +++ b/lib/ansible/modules/windows/win_route.ps1 @@ -39,7 +39,7 @@ Function Add-Route { if (!($Route)){ try { # Find Interface Index - $InterfaceIndex = Find-NetRoute -RemoteIPAddress $IpAddress | Select -First 1 -ExpandProperty InterfaceIndex + $InterfaceIndex = Find-NetRoute -RemoteIPAddress $Gateway | Select -First 1 -ExpandProperty InterfaceIndex # Add network route New-NetRoute -DestinationPrefix $Destination -NextHop $Gateway -InterfaceIndex $InterfaceIndex -RouteMetric $Metric -ErrorAction Stop -WhatIf:$CheckMode|out-null diff --git a/test/integration/targets/win_route/tasks/tests.yml b/test/integration/targets/win_route/tasks/tests.yml index e893dfd6396..0c365bef549 100644 --- a/test/integration/targets/win_route/tasks/tests.yml +++ b/test/integration/targets/win_route/tasks/tests.yml @@ -7,15 +7,20 @@ state: present register: route -- name: check if route successfully addedd +- name: check if route successfully added win_shell: (Get-CimInstance win32_ip4PersistedrouteTable -Filter "Destination = '{{ destination_ip_address }}'").Caption register: route_added -- name: test if route successfully addedd +- name: check route default gateway + win_shell: (Get-CimInstance win32_ip4PersistedrouteTable -Filter "Destination = '{{ destination_ip_address }}'").NextHop + register: route_gateway + +- name: test if route successfully added assert: that: - route is changed - route_added.stdout_lines[0] == "{{ destination_ip_address }}" + - route_gateway.stdout_lines[0] == "{{ default_gateway }}" - name: add a static route to test idempotency win_route: