Ansible: How to know existence of a error string from an output

Hi ,

I am trying to pass or fail a command execution if i get an exact error message as a result of command execution.
but i dont’t know how to extract a output string from a registered variable.

I am sure somebody can help.

Following is my playbook role’s code:

  • name: Create snapshot of a chekpoint disabled vGPU VM
    vmware_guest_snapshot:
    hostname: ‘{{ hostname }}’
    username: ‘{{ username }}’
    password: ‘{{ password }}’
    name: ‘{{ guest_name_checkpoint}}’
    datacenter: ‘{{ datacenter_name }}’
    validate_certs: False
    state: present
    snapshot_name: ‘{{ item }}’
    description: snap1 is the snapshot of ‘{{guest_name}}’
    delegate_to: localhost
    with_items:

  • ‘{{snapshot_list}}’
    register: taskresult
    ignore_errors: true

  • debug: msg=“{{taskresult}}”

and below is content of taskresult variable. I need to know whther taskresult contains error string "Checkpoint operation not supported for PCI Plugin.
OR not.

ASK [VCENTER_VM_CheckPointOPs : debug] *********************************************************************************************************************************
ok: [10.160.89.54] => {
“msg”: [
{
“_ansible_delegated_vars”: {
“ansible_delegated_host”: “localhost”,
“ansible_host”: “localhost”
},
“_ansible_item_result”: true,
“_ansible_no_log”: false,
“_ansible_parsed”: true,
“changed”: false,
“failed”: true,
“invocation”: {
“module_args”: {
“datacenter”: “Datacenter”,
“description”: “snap1 is the snapshot of ‘DNT_T’”,
“folder”: “/vm”,
“hostname”: “xxxx”,
“name”: “test1_checkpoint”,
“name_match”: “first”,
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“snapshot_name”: “snap1”,
“state”: “present”,
“username”: “adminxxxxr@vsphere.local”,
“uuid”: null,
“validate_certs”: false
}
},
“item”: “snap1”,
“msg”: “Checkpoint operation not supported for PCI Plugin.”
},
{
“_ansible_delegated_vars”: {
“ansible_delegated_host”: “localhost”,
“ansible_host”: “localhost”
},
“_ansible_item_result”: true,
“_ansible_no_log”: false,
“_ansible_parsed”: true,
“changed”: false,
“failed”: true,
“invocation”: {
“module_args”: {
“datacenter”: “Datacenter”,
“description”: “snap1 is the snapshot of ‘DNT_T’”,
“folder”: “/vm”,
“hostname”: “xxxx”,
“name”: “test1_checkpoint”,
“name_match”: “first”,
“password”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“snapshot_name”: “snap2”,
“state”: “present”,
“username”: “adminxxx@vsphere.local”,
“uuid”: null,
“validate_certs”: false
}
},
“item”: “snap2”,
“msg”: “Checkpoint operation not supported for PCI Plugin.”
}
]
}

           I am trying to pass or fail a command execution if i get an exact
error message as a result of command execution.
but i dont't know how to extract a output string from a registered variable.

I am sure somebody can help.

Following is my playbook role's code:

  - name: Create snapshot of a chekpoint disabled vGPU VM
         vmware_guest_snapshot:
            hostname: '{{ hostname }}'
            username: '{{ username }}'
            password: '{{ password }}'
            name: '{{ guest_name_checkpoint}}'
            datacenter: '{{ datacenter_name }}'
            validate_certs: False
            state: present
            snapshot_name: '{{ item }}'
            description: snap1 is the snapshot of '{{guest_name}}'
         delegate_to: localhost
         with_items:
            - '{{snapshot_list}}'
         register: taskresult
         ignore_errors: true

Since you are using with_items, the register variable will contain a list.

       - debug: msg="{{taskresult}}"

and below is content of taskresult variable. I need to know whther
taskresult contains error string "Checkpoint operation not supported for
PCI Plugin.
OR not.

ASK [VCENTER_VM_CheckPointOPs : debug]
*********************************************************************************************************************************
ok: [10.160.89.54] => {
     "msg": [
         {
             "_ansible_delegated_vars": {
                 "ansible_delegated_host": "localhost",
                 "ansible_host": "localhost"
             },
             "_ansible_item_result": true,
             "_ansible_no_log": false,
             "_ansible_parsed": true,
             "changed": false,
             "failed": true,
             "invocation": {
                 "module_args": {
                     "datacenter": "Datacenter",
                     "description": "snap1 is the snapshot of 'DNT_T'",
                     "folder": "/vm",
                     "hostname": "xxxx",
                     "name": "test1_checkpoint",
                     "name_match": "first",
                     "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                     "snapshot_name": "snap1",
                     "state": "present",
                     "username": "adminxxxxr@vsphere.local",
                     "uuid": null,
                     "validate_certs": false
                 }
             },
             "item": "snap1",
             "msg": "Checkpoint operation not supported for PCI Plugin."
         },
         {
             "_ansible_delegated_vars": {
                 "ansible_delegated_host": "localhost",
                 "ansible_host": "localhost"
             },
             "_ansible_item_result": true,
             "_ansible_no_log": false,
             "_ansible_parsed": true,
             "changed": false,
             "failed": true,
             "invocation": {
                 "module_args": {
                     "datacenter": "Datacenter",
                     "description": "snap1 is the snapshot of 'DNT_T'",
                     "folder": "/vm",
                     "hostname": "xxxx",
                     "name": "test1_checkpoint",
                     "name_match": "first",
                     "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                     "snapshot_name": "snap2",
                     "state": "present",
                     "username": "adminxxx@vsphere.local",
                     "uuid": null,
                     "validate_certs": false
                 }
             },
             "item": "snap2",
             "msg": "Checkpoint operation not supported for PCI Plugin."
         }
     ]
}

You taskresult contains 2 list items.
The fist one is taskresult.0.msg and the second one is taskresult.1.msg

So what you do next depend on what need to do. To check it with a loop you can do something like this.

- debug: msg="Item {{ item.item }} does contain the sting"
   when: item.msg == "Checkpoint operation not supported for PCI Plugin."
   with_items: '{{ taskresult }}'