vsphere wait for vm powered off

1,082 views
Skip to first unread message

Mathias Waack

unread,
Nov 11, 2016, 11:14:24 AM11/11/16
to Ansible Project
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

Kai Stian Olstad

unread,
Nov 11, 2016, 11:40:55 AM11/11/16
to ansible...@googlegroups.com
On 11. nov. 2016 11:36, Mathias Waack wrote:
> 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:
>
> 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?

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

--
Kai Stian Olstad

Dylan Silva

unread,
Nov 14, 2016, 9:59:25 AM11/14/16
to Ansible Project
@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.  

Mathias Waack

unread,
Nov 15, 2016, 3:35:13 AM11/15/16
to Ansible Project, ansible-pr...@olstad.com
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

Mathias Waack

unread,
Nov 15, 2016, 3:53:39 AM11/15/16
to Ansible Project
Hi Dylan, 

I am using 2.2. Thanks for pointing me at vmware_guest, but vsphere_guest works for me (at moment). 

Dylan Silva

unread,
Nov 15, 2016, 8:52:49 AM11/15/16
to Ansible Project
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

Zeljko Dokman

unread,
Apr 4, 2018, 9:34:32 AM4/4/18
to Ansible Project
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

robert sanders

unread,
Apr 10, 2018, 3:09:42 PM4/10/18
to Ansible Project
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

Zeljko Dokman

unread,
Apr 11, 2018, 5:05:43 AM4/11/18
to Ansible Project
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=ans...@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

Kai Stian Olstad

unread,
Apr 11, 2018, 1:09:07 PM4/11/18
to ansible...@googlegroups.com
On Wednesday, 11 April 2018 11.05.42 CEST Zeljko Dokman wrote:
> 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.

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


--
Kai Stian Olstad

Zeljko Dokman

unread,
Apr 12, 2018, 8:11:21 AM4/12/18
to Ansible Project
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

Kai Stian Olstad

unread,
Apr 12, 2018, 11:09:05 AM4/12/18
to ansible...@googlegroups.com
On 12.04.2018 14:11, Zeljko Dokman wrote:
> 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?

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


--
Kai Stian Olstad

Zeljko Dokman

unread,
Apr 13, 2018, 3:14:36 AM4/13/18
to Ansible Project
Hi, thank you for your replay

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

Regards

Kai Stian Olstad

unread,
Apr 13, 2018, 11:01:43 AM4/13/18
to ansible...@googlegroups.com
On Friday, 13 April 2018 09.14.36 CEST Zeljko Dokman wrote:
> Do you know is it possible to list all undocumented parameters of a module
> and its states?

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.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages