From ff03fc0f6758126bef093197c46fa040a86ec348 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 6 Apr 2018 08:38:13 +1000 Subject: [PATCH] win_setup: backport Added connection name to interfaces (#38362) * win_setup: Added connection name to interfaces (#37327) (cherry picked from commit fd4d264253f6faba2fb30b4a3c469e13d411d0fc) * Added changelog fragment --- .../fragments/win_setup-connection-name.yaml | 3 +++ lib/ansible/modules/windows/setup.ps1 | 21 ++++++++++++++++--- .../targets/win_setup/tasks/main.yml | 2 ++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/win_setup-connection-name.yaml diff --git a/changelogs/fragments/win_setup-connection-name.yaml b/changelogs/fragments/win_setup-connection-name.yaml new file mode 100644 index 00000000000..960ea7347a1 --- /dev/null +++ b/changelogs/fragments/win_setup-connection-name.yaml @@ -0,0 +1,3 @@ +bugfixes: +- win_setup - Use connection name for network interfaces as interface name isn't helpful + https://github.com/ansible/ansible/pull/37327 diff --git a/lib/ansible/modules/windows/setup.ps1 b/lib/ansible/modules/windows/setup.ps1 index ae618d8fd46..fe67ce289fd 100644 --- a/lib/ansible/modules/windows/setup.ps1 +++ b/lib/ansible/modules/windows/setup.ps1 @@ -44,9 +44,9 @@ Function Get-MachineSid { $cim_instances = @{} -Function Get-LazyCimInstance([string]$instance_name) { +Function Get-LazyCimInstance([string]$instance_name, [string]$namespace="Root\CIMV2") { if(-not $cim_instances.ContainsKey($instance_name)) { - $cim_instances[$instance_name] = $(Get-CimInstance $instance_name) + $cim_instances[$instance_name] = $(Get-CimInstance -Namespace $namespace -ClassName $instance_name) } return $cim_instances[$instance_name] @@ -229,15 +229,26 @@ if($gather_subset.Contains('facter')) { if($gather_subset.Contains('interfaces')) { $netcfg = Get-LazyCimInstance Win32_NetworkAdapterConfiguration - $ActiveNetcfg = @() $ActiveNetcfg += $netcfg | where {$_.ipaddress -ne $null} + $namespaces = Get-LazyCimInstance __Namespace -namespace root + if ($namespaces | Where-Object { $_.Name -eq "StandardCimv" }) { + $net_adapters = Get-LazyCimInstance MSFT_NetAdapter -namespace Root\StandardCimv2 + $guid_key = "InterfaceGUID" + $name_key = "Name" + } else { + $net_adapters = Get-LazyCimInstance Win32_NetworkAdapter + $guid_key = "GUID" + $name_key = "NetConnectionID" + } + $formattednetcfg = @() foreach ($adapter in $ActiveNetcfg) { $thisadapter = @{ default_gateway = $null + connection_name = $null dns_domain = $adapter.dnsdomain interface_index = $adapter.InterfaceIndex interface_name = $adapter.description @@ -248,6 +259,10 @@ if($gather_subset.Contains('interfaces')) { { $thisadapter.default_gateway = $adapter.DefaultIPGateway[0].ToString() } + $net_adapter = $net_adapters | Where-Object { $_.$guid_key -eq $adapter.SettingID } + if ($net_adapter) { + $thisadapter.connection_name = $net_adapter.$name_key + } $formattednetcfg += $thisadapter } diff --git a/test/integration/targets/win_setup/tasks/main.yml b/test/integration/targets/win_setup/tasks/main.yml index 516fbb41305..92f9a234445 100644 --- a/test/integration/targets/win_setup/tasks/main.yml +++ b/test/integration/targets/win_setup/tasks/main.yml @@ -45,6 +45,7 @@ - setup_result.ansible_facts.ansible_interfaces - setup_result.ansible_facts.ansible_interfaces[0] - setup_result.ansible_facts.ansible_interfaces[0].interface_name + - setup_result.ansible_facts.ansible_interfaces[0].connection_name - setup_result.ansible_facts.ansible_interfaces[0].interface_index - setup_result.ansible_facts.ansible_architecture - setup_result.ansible_facts.ansible_os_name @@ -114,6 +115,7 @@ - setup_result.ansible_facts.ansible_interfaces - setup_result.ansible_facts.ansible_interfaces[0] - setup_result.ansible_facts.ansible_interfaces[0].interface_name + - setup_result.ansible_facts.ansible_interfaces[0].connection_name - setup_result.ansible_facts.ansible_interfaces[0].interface_index - setup_result.ansible_facts.keys() | union(['ansible_interfaces','gather_subset','module_setup']) | length == 3