From 18503e7ed65f0b3f921e49fe621b5826482d1462 Mon Sep 17 00:00:00 2001 From: rhydian76 Date: Wed, 12 Dec 2018 23:40:13 +0000 Subject: [PATCH] =?UTF-8?q?Create=20Windows=20facts=20for=20ansible=5Fvirt?= =?UTF-8?q?ualization=5Frole=20and=20ansible=5Fvirt=E2=80=A6=20(#49775)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Create Windows facts for ansible_virtualization_role and ansible_virtualization_type * Create Windows facts for ansible_virtualization_role and ansible_virtualization_type Updated formatting * Removed executable flag on setup.ps1 * Changed Get-WMIObject to Get-CimInstance as Get-WMIObject has been deprecated and fails testing. * Changed new variables to snake_case and also changed Get-CimInstance to Get-LazyCimInstance * removed -class from Get-LazyCimInstance call as failed test. --- lib/ansible/modules/windows/setup.ps1 | 38 ++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/windows/setup.ps1 b/lib/ansible/modules/windows/setup.ps1 index c3614485f0e..e81d328b787 100644 --- a/lib/ansible/modules/windows/setup.ps1 +++ b/lib/ansible/modules/windows/setup.ps1 @@ -68,7 +68,7 @@ $result = @{ $grouped_subsets = @{ min=[System.Collections.Generic.List[string]]@('date_time','distribution','dns','env','local','platform','powershell_version','user') network=[System.Collections.Generic.List[string]]@('all_ipv4_addresses','all_ipv6_addresses','interfaces','windows_domain', 'winrm') - hardware=[System.Collections.Generic.List[string]]@('bios','memory','processor','uptime') + hardware=[System.Collections.Generic.List[string]]@('bios','memory','processor','uptime','virtual') external=[System.Collections.Generic.List[string]]@('facter') } @@ -439,6 +439,42 @@ if($gather_subset.Contains('winrm')) { } } +if($gather_subset.Contains('virtual')) { + $machine_info = Get-LazyCimInstance Win32_ComputerSystem + + switch ($machine_info.model) { + "Virtual Machine" { + $machine_type="Hyper-V" + $machine_role="guest" + } + + "VMware Virtual Platform" { + $machine_type="VMware" + $machine_role="guest" + } + + "VirtualBox" { + $machine_type="VirtualBox" + $machine_role="guest" + } + + "HVM domU" { + $machine_type="Xen" + $machine_role="guest" + } + + default { + $machine_type="NA" + $machine_role="NA" + } + } + + $ansible_facts += @{ + ansible_virtualization_role = $machine_role + ansible_virtualization_type = $machine_type + } +} + $result.ansible_facts += $ansible_facts Exit-Json $result