Checking Success/Failure with Vmware Ansible modules

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”
}
]
}
}

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

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
        when: taskresult1|succeeded
        delegate_to: localhost
      - shell: echo "HPQC xxx Failed Suspend the Single VM" >> results.txt
        when: taskresult1|failed
        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] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [

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

            {
                "_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,

And each item has a failed, either true or false