We use cloning to create new VMs in our vsphere cluster. The template is usually powered on (to receive regular updates), so we power it off before we create a clone:
-
name: power off the template
vsphere_guest:
vcenter_hostname: “{{ vcenter }}”
username: “{{ user }}”
password: “{{ pass }}”
guest: “{{ template }}”
validate_certs: False
state: powered_off
force: yes
-
debug: msg=“template state is {{ hw_power_status }}.”
-
name: ensure the template is powered off
assert: { that: hw_power_status == “POWERED OFF” }
This used to work for VSphere 5.x, since the upgrade to VSphere 6.0 the assert fails:
TASK [debug] *******************************************************************
ok: [localhost] => {
“msg”: “template state is POWERED ON.”
}
TASK [ensure the template is powered off] **************************************
fatal: [localhost]: FAILED! => {
“assertion”: “hw_power_status == "POWERED OFF"”,
“changed”: false,
“evaluated_to”: false,
“failed”: true
}
VSphere shuts down the VM as requested, so maybe it is just a timing problem?
So how can I ensure the VM is really powered off before the next task starts? Is there a way to wait till hw_power_status changes?
Mathias
Something like this.
- name: Check if {{ template }} is down
vsphere_guest:
vcenter_hostname: "{{ vcenter }}"
username: "{{ user }}"
password: "{{ pass }}"
guest: "{{ template }}"
vmware_guest_facts: yes
register: result
until: result.hw_power_status == "POWERED OFF"
reties: 10
delay: 5
@Mathias, as an aside, what version of Ansible are you running? I ask because the vmware_guest module is available as of 2.2 as a replacement of this module.
Hi Kai,
thank you, this works for me (with a very small modification:
- name: wait till {{ template }} is powered off
vsphere_guest:
vcenter_hostname: “{{vcenter}}”
username: “{{user}}”
password: “{{passw}}”
guest: “{{template}}”
validate_certs: False
vmware_guest_facts: yes
register: result
until: result.ansible_facts.hw_power_status == “POWERED OFF”
retries: 10
delay: 5
).
Mathias
Hi Dylan,
I am using 2.2. Thanks for pointing me at vmware_guest, but vsphere_guest works for me (at moment).
Thanks for that info!
Just a heads up, we’re deprecating the vsphere_guest module in a couple of releases. Please take a look at this post for more information.
~@thaumos
Hi, I am trying to use this code to check VM power state and its failing for me with this error…
“msg”: “The conditional check ‘result.hw_power_status == "POWERED OFF"’ failed. The error was: error while evaluating conditional (result.hw_power_status == "POWERED OFF"): ‘dict object’ has no attribute ‘hw_power_status’”
My playbook…
`
Enter code here… - name: Wait till {{ target }} is powered off
vsphere_guest:
vcenter_hostname=“{{ vcenter_server }}”
username=ansible@mbu.local
password=“{{ vc_passwd }}”
guest=mbu-zabbix-ccp
validate_certs=False
vmware_guest_facts=yes
register: result
until: result.hw_power_status == “POWERED OFF”
retries: 10
delay: 5
i tried with
result.hw_power_status == “POWERED OFF”
and`
result.````ansible_facts.hw_power_status == "POWERED OFF"
both fail with a same error..
Regards
change result to vm_state if youre using vsphere_guest
register: vm_state
until: vm_state.ansible_facts.hw_power_status == ‘POWERED OFF’
retries: 10
delay: 5
Hi, thank you, your suggestion works.
I am wondering are this options documented some where? or how can I list this option for a module?
Ansible documentation is not covering all this options or I haven’t been able to find it.
here is a working code
`
- name: Wait till {{ target }} is powered off
vsphere_guest:
vcenter_hostname=“{{ vcenter_server }}”
username=ansible@local.domain
password=“{{ vc_passwd }}”
guest=“{{ ansible_hostname }}”
validate_certs=False
vmware_guest_facts=yes
register: vm_state
until: vm_state.ansible_facts.hw_power_status == ‘POWERED OFF’
retries: 10
delay: 5
delegate_to: localhost
`
Best regards
The directives one the same level as vsphere_guest is not part of the module they are part of the task.
The directive you can use and where is documented here
https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html
The documentation for until you'll find here
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#do-until-loops
Hi,
thank you for the info.
one more question, for vsphere_guest module, how to know that “state” parameter should be referenced as
“vm_state.ansible_facts.hw_power_status”, this part a have not find in the documentation.? how is this contracted for all other parameters for particular module?
Best regards
Some module has this documented, but most of them don't unfortunately.
So you are left with finding it yourself.
To do that you run the module with "register: ..." directive and print the content of the variable.
The content you print like this
- debug: var=vm_state
Hi, thank you for your replay
Do you know is it possible to list all undocumented parameters of a module and its states?
Regards
I don't think there are any undocumented parameters so in that regard the module documentation is very good.
But the output they generate is lacking on many of them, and the only way to find this is to read the code or run it and print out the result as I described in previous post.