On 11.10.2017 00:37, Vijay Misra wrote:
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


--
Kai Stian Olstad

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/47b9b10a1597cd13c5a23ba289fb4cd2%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to