# Checking Success/Failure with Vmware Ansible modules

**URL:** <https://forum.ansible.com/t/checking-success-failure-with-vmware-ansible-modules/25716>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [October 10, 2017, 10:37pm UTC](https://forum.ansible.com/t/checking-success-failure-with-vmware-ansible-modules/25716 "2017-10-10T22:37:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Victor1](https://forum.ansible.com/u/Victor1)\
**Post date:** [October 10, 2017, 10:37pm UTC](https://forum.ansible.com/t/checking-success-failure-with-vmware-ansible-modules/25716/1 "2017-10-10T22:37:51Z")

</div>

Hi ,

I have written few tests using ansible . basicall suspending a VM and then resuming again , taking snapshots etc… I need to check success or failure of these operations. I am registering a vraibklle to capture the output as below and writing the results in a local file.

I am not sure about the correct way of capturing the success or failure through the registered variable.

Thanks for your help!

- name: Suspend the Single Machine  
local\_action:  
module: vmware\_guest  
hostname: ‘{{ hostname }}’  
username: ‘{{ username }}’  
password: ‘{{ password }}’  
name: ‘{{ item }}’  
esxi\_hostname: ‘{{ esxi\_hostname2 }}’  
template: ‘’  
is\_template: no  
datacenter: ‘{{ datacenter\_name }}’  
state: ‘suspended’  
force: yes  
validate\_certs: False  
with\_items:

- ‘{{ guest\_list[0] }}’  
register: **taskresult1**  
ignore\_errors: True

- shell: echo “HPQC xxx Passed Suspend the Single VM” \>\> results.txt  
when: taskresult1|succeeded  
delegate\_to: localhost

- shell: echo “HPQC xxx Failed Suspend the Single VM” \>\> results.txt  
when: taskresult1|failed  
delegate\_to: localhost

taskresult1 has following content.

TASK [VCENTER\_VM\_SuspendRes : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/roles/VCENTER\_VM\_SuspendRes/tasks/main.yml:54  
ok: [xxxxxx] =\> {  
“msg”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_delegated\_vars”: {  
“ansible\_delegated\_host”: “localhost”,  
“ansible\_host”: “localhost”  
},  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“annotation”: null,  
“cluster”: null,  
“customization”: {},  
“customvalues”: ,  
“datacenter”: “Datacenter”,  
“disk”: ,  
“esxi\_hostname”: “xxxxx”,  
“folder”: “/vm”,  
“force”: true,  
“guest\_id”: null,  
“hardware”: {},  
“hostname”: “xxxxx”,  
“is\_template”: false,  
“name”: “test1”,  
“name\_match”: “first”,  
“networks”: ,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“resource\_pool”: null,  
“state”: “suspended”,  
“template”: “”,  
“template\_src”: “”,  
“username”: “administrator@vsphere.local”,  
“uuid”: null,  
“validate\_certs”: false,  
“wait\_for\_ip\_address”: false  
}  
},  
“item”: “test1”  
}  
]  
}  
}

---

<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:** [October 11, 2017, 8:14am UTC](https://forum.ansible.com/t/checking-success-failure-with-vmware-ansible-modules/25716/2 "2017-10-11T08:14:43Z")

</div>

> Hi ,
> 
> I have written few tests using ansible . basicall suspending a VM and then  
> resuming again , taking snapshots etc.. I need to check success or failure  
> of these operations. I am registering a vraibklle to capture the output as  
> below and writing the results in a local file.
> 
> I am not sure about the correct way of capturing the success or failure  
> through the registered variable.
> 
> Thanks for your help!
> 
> &nbsp;&nbsp;- name: Suspend the Single Machine  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module: vmware\_guest  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: '{{ hostname }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: '{{ username }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: '{{ password }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ item }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi\_hostname: '{{ esxi\_hostname2 }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template: ''  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is\_template: no  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datacenter: '{{ datacenter\_name }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: 'suspended'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;force: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: False  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- '{{ guest\_list[0] }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: \*taskresult1\*  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: True

Since you are using with\_items, the actual result is in taskresult1['results'], and this is a list of arrays, one for each item in with\_items.

> - shell: echo "HPQC xxx Passed Suspend the Single VM" \>\> results.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: taskresult1|succeeded  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: echo "HPQC xxx Failed Suspend the Single VM" \>\> results.txt  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: taskresult1|failed  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

So to test the first task item number one you'll need to use taskresult1.result.0 and for the second item taskresult1.result.

> TASK [VCENTER\_VM\_SuspendRes : debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> task path: /etc/ansible/roles/VCENTER\_VM\_SuspendRes/tasks/main.yml:54  
> ok: [xxxxxx] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "All items completed",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"results": [

Here you see the results and the [ that indicate a list.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_delegated\_vars": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_delegated\_host": "localhost",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_host": "localhost"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_item\_result": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_no\_log": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_parsed": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"failed": false,

And each item has a failed, either true or false
