From 049d2db9efae302a7a0013309d9ef7f498e70ad2 Mon Sep 17 00:00:00 2001 From: Geoff O'Callaghan Date: Mon, 10 Jun 2024 11:31:43 +1000 Subject: [PATCH 1/2] Turned off gather_facts to speed up test --- molecule/vm_info/cleanup.yml | 1 + molecule/vm_info/converge.yml | 1 + molecule/vm_info/create.yml | 1 + 3 files changed, 3 insertions(+) diff --git a/molecule/vm_info/cleanup.yml b/molecule/vm_info/cleanup.yml index 7bfbb3b..01f15ba 100644 --- a/molecule/vm_info/cleanup.yml +++ b/molecule/vm_info/cleanup.yml @@ -1,6 +1,7 @@ --- - name: Cleanup hosts: all + gather_facts: false vars: ansible_connection: winrm ansible_port: 5986 diff --git a/molecule/vm_info/converge.yml b/molecule/vm_info/converge.yml index 496f81f..cae1812 100644 --- a/molecule/vm_info/converge.yml +++ b/molecule/vm_info/converge.yml @@ -1,6 +1,7 @@ --- - name: Converge hosts: all + gather_facts: false vars: ansible_connection: winrm ansible_port: 5986 diff --git a/molecule/vm_info/create.yml b/molecule/vm_info/create.yml index 6669ca6..036f5c3 100644 --- a/molecule/vm_info/create.yml +++ b/molecule/vm_info/create.yml @@ -1,6 +1,7 @@ --- - name: Create hosts: all + gather_facts: false vars: ansible_connection: winrm ansible_port: 5986 From 08399fab5125ab30d003fd958d1760554b093504 Mon Sep 17 00:00:00 2001 From: Geoff O'Callaghan Date: Mon, 10 Jun 2024 11:32:35 +1000 Subject: [PATCH 2/2] Standardise vm_info to return a Count (0 or above) and a value list (null or containing the vm_info data) --- molecule/vm_info/verify.yml | 28 ++++++++++++++-------------- plugins/modules/vm_info.ps1 | 20 +++++++++++++++++++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/molecule/vm_info/verify.yml b/molecule/vm_info/verify.yml index 318891c..f453d8a 100644 --- a/molecule/vm_info/verify.yml +++ b/molecule/vm_info/verify.yml @@ -1,6 +1,7 @@ --- -- name: Converge +- name: Verify hosts: all + gather_facts: false vars: ansible_connection: winrm ansible_port: 5986 @@ -14,7 +15,6 @@ names: - LAB-Test register: info - - ansible.builtin.assert: that: - info is defined @@ -26,30 +26,30 @@ - info.output.value[0].VMName == "LAB-Test" - info.output.value[1].VMName == "LAB-Test" - - name: Get information about the VM called LAB-Test that is 'Running' + - name: Collect information about a specific VM, 1st in list gocallag.hyperv.vm_info: - names: - - LAB-Test - power_state: 'Running' + id: '{{ info.output.value[0].Id }}' register: info - - debug: msg="{{ info }}" - ansible.builtin.assert: that: - info is defined - info.output is defined - - info.output.Count is defined and info.output.Count == 1 - info.output.value is defined - info.output.value[0].Id is defined - info.output.value[0].VMName == "LAB-Test" - - - name: Collect information about a specific VM, 1st in list + + - name: Get information about the VM called LAB-Test that is 'Running' (should be none) gocallag.hyperv.vm_info: - id: '{{ info.output.value[0].Id }}' - register: myvm + names: + - LAB-Test + power_state: 'Running' + register: info - ansible.builtin.assert: that: - info is defined - info.output is defined + - info.output.Count is defined and info.output.Count == 0 - info.output.value is defined - - info.output.value.Id is defined - - info.output.value.VMName == "LAB-Test" + - info.output.value | length == 0 + + diff --git a/plugins/modules/vm_info.ps1 b/plugins/modules/vm_info.ps1 index f57f357..b075c54 100644 --- a/plugins/modules/vm_info.ps1 +++ b/plugins/modules/vm_info.ps1 @@ -59,7 +59,25 @@ Try { $output = invoke-expression -Command "$cmd -ErrorAction SilentlyContinue" $result.cmd = $cmd - $result.output = $output | ConvertTo-Json | ConvertFrom-Json # note this has to be done otherwise we will hang in Exit-Json + $output = $output | ConvertTo-Json | ConvertFrom-Json # note this has to be done otherwise we will hang in Exit-Json + # $result.output = $output + if ( $null -eq $output ) { + $result.output = @{ "Count" = 0 + "value" = @() + } + } + else { + Try { + if ( $output.Count -gt 0 ) { # if there is a Count then set the result as expected, else the Catch will trigger and convert the non-list to a list with 1 element + $result.output = $output + } + } + Catch { + $result.output = @{ "Count" = 1 + "value" = @($output) } + } + + } } Catch { Fail-Json -obj $result -message "an error occurred when attempting to Get-VM - $($_.Exception.Message)"