Skip to content

Commit

Permalink
Merge pull request #18 from gocallag/12-bug-vm_info-fails-molecule-ve…
Browse files Browse the repository at this point in the history
…rifier-on-running-vm-check

12 bug vm info fails molecule verifier on running vm check
  • Loading branch information
gocallag authored Jun 10, 2024
2 parents abc166f + 08399fa commit 10fe119
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions molecule/vm_info/cleanup.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- name: Cleanup
hosts: all
gather_facts: false
vars:
ansible_connection: winrm
ansible_port: 5986
Expand Down
1 change: 1 addition & 0 deletions molecule/vm_info/converge.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- name: Converge
hosts: all
gather_facts: false
vars:
ansible_connection: winrm
ansible_port: 5986
Expand Down
1 change: 1 addition & 0 deletions molecule/vm_info/create.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- name: Create
hosts: all
gather_facts: false
vars:
ansible_connection: winrm
ansible_port: 5986
Expand Down
28 changes: 14 additions & 14 deletions molecule/vm_info/verify.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- name: Converge
- name: Verify
hosts: all
gather_facts: false
vars:
ansible_connection: winrm
ansible_port: 5986
Expand All @@ -14,7 +15,6 @@
names:
- LAB-Test
register: info

- ansible.builtin.assert:
that:
- info is defined
Expand All @@ -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


20 changes: 19 additions & 1 deletion plugins/modules/vm_info.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit 10fe119

Please sign in to comment.