# vsphere wait for vm powered off

**URL:** <https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007>\
**Category:** Ansible Project\
**Created:** [November 11, 2016, 10:36am UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007 "2016-11-11T10:36:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias\_Waack](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mathias\_Waack](https://forum.ansible.com/u/Mathias_Waack)\
**Post date:** [November 11, 2016, 10:36am UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/1 "2016-11-11T10:36:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 11, 2016, 4:40pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/2 "2016-11-11T16:40:23Z")

</div>

Something like this.

- name: Check if {{ template }} is down  
&nbsp;&nbsp;&nbsp;vsphere\_guest:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vcenter\_hostname: "{{ vcenter }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ user }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ pass }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guest: "{{ template }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vmware\_guest\_facts: yes  
&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;until: result.hw\_power\_status == "POWERED OFF"  
&nbsp;&nbsp;&nbsp;reties: 10  
&nbsp;&nbsp;&nbsp;delay: 5

---

<div class="post-metadata">

**Author:** ![Dylan\_Silva1](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Dylan\_Silva1](https://forum.ansible.com/u/Dylan_Silva1)\
**Post date:** [November 14, 2016, 2:59pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/3 "2016-11-14T14:59:25Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Mathias\_Waack](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mathias\_Waack](https://forum.ansible.com/u/Mathias_Waack)\
**Post date:** [November 15, 2016, 8:35am UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/4 "2016-11-15T08:35:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mathias\_Waack](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@Mathias\_Waack](https://forum.ansible.com/u/Mathias_Waack)\
**Post date:** [November 15, 2016, 8:53am UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/5 "2016-11-15T08:53:39Z")

</div>

Hi Dylan,

I am using 2.2. Thanks for pointing me at vmware\_guest, but vsphere\_guest works for me (at moment).

---

<div class="post-metadata">

**Author:** ![Dylan\_Silva1](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Dylan\_Silva1](https://forum.ansible.com/u/Dylan_Silva1)\
**Post date:** [November 15, 2016, 1:52pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/6 "2016-11-15T13:52:49Z")

</div>

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](https://groups.google.com/d/topic/ansible-project/8ygV2J4hRl8/discussion) for more information.

~@thaumos

---

<div class="post-metadata">

**Author:** ![Zeljko\_Dokman](https://avatars.discourse-cdn.com/v4/letter/z/f04885/32.png) [@Zeljko\_Dokman](https://forum.ansible.com/u/Zeljko_Dokman)\
**Post date:** [April 4, 2018, 1:34pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/7 "2018-04-04T13:34:32Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![robert\_sanders](https://avatars.discourse-cdn.com/v4/letter/r/7feea3/32.png) [@robert\_sanders](https://forum.ansible.com/u/robert_sanders)\
**Post date:** [April 10, 2018, 7:09pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/8 "2018-04-10T19:09:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zeljko\_Dokman](https://avatars.discourse-cdn.com/v4/letter/z/f04885/32.png) [@Zeljko\_Dokman](https://forum.ansible.com/u/Zeljko_Dokman)\
**Post date:** [April 11, 2018, 9:05am UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/9 "2018-04-11T09:05:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 11, 2018, 5:08pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/10 "2018-04-11T17:08:40Z")

</div>

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](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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#do-until-loops)

---

<div class="post-metadata">

**Author:** ![Zeljko\_Dokman](https://avatars.discourse-cdn.com/v4/letter/z/f04885/32.png) [@Zeljko\_Dokman](https://forum.ansible.com/u/Zeljko_Dokman)\
**Post date:** [April 12, 2018, 12:11pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/11 "2018-04-12T12:11:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 12, 2018, 3:08pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/12 "2018-04-12T15:08:47Z")

</div>

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

&nbsp;&nbsp;&nbsp;- debug: var=vm\_state

---

<div class="post-metadata">

**Author:** ![Zeljko\_Dokman](https://avatars.discourse-cdn.com/v4/letter/z/f04885/32.png) [@Zeljko\_Dokman](https://forum.ansible.com/u/Zeljko_Dokman)\
**Post date:** [April 13, 2018, 7:14am UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/13 "2018-04-13T07:14:36Z")

</div>

Hi, thank you for your replay

Do you know is it possible to list all undocumented parameters of a module and its states?

Regards

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 13, 2018, 3:01pm UTC](https://forum.ansible.com/t/vsphere-wait-for-vm-powered-off/23007/14 "2018-04-13T15:01:32Z")

</div>

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.
