Ansible parsing a JSON output

ansibleansible-playbookjsonparsingvmware-vcenter

I'm using URI in order to get a JSOn output to a register called "vchosts"

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "vchosts": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "connection": "close",
                "content_type": "application/json",
                "cookies": {},
                "date": "Mon, 11 Jun 2018 09:50:45 GMT",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "attributes": null,
                        "backup": null,
                        "body": null,
                        "body_format": "raw",
                        "client_cert": null,
                        "client_key": null,
                        "content": null,
                        "creates": null,
                        "delimiter": null,
                        "dest": null,
                        "directory_mode": null,
                        "follow": false,
                        "follow_redirects": "safe",
                        "force": false,
                        "force_basic_auth": true,
                        "group": null,
                        "headers": {
                            "Cookie": "vmware-api-session-id=56fa6d3015150212b086917d15165bee;Path=/rest;Secure;HttpOnly"
                        },
                        "http_agent": "ansible-httpget",
                        "method": "GET",
                        "mode": null,
                        "owner": null,
                        "regexp": null,
                        "remote_src": null,
                        "removes": null,
                        "return_content": false,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": null,
                        "status_code": [
                            200
                        ],
                        "timeout": 30,
                        "unsafe_writes": null,
                        "url": "https://vcenter01.lab.test/rest/vcenter/host?filter.clusters=domain-c310",
                        "url_password": null,
                        "url_username": null,
                        "use_proxy": true,
                        "validate_certs": false
                    }
                },
                "item": {
                    "cluster": "domain-c310",
                    "drs_enabled": true,
                    "ha_enabled": false,
                    "name": "DB-CLUSTER"
                },
                "json": {
                    "value": [
                        {
                            "connection_state": "CONNECTED",
                            "host": "host-312",
                            "name": "vmh19.lab.test",
                            "power_state": "POWERED_ON"
                        },
                        {
                            "connection_state": "CONNECTED",
                            "host": "host-313",
                            "name": "vmh20.lab.test",
                            "power_state": "POWERED_ON"
                        }
                    ]
                },
                "msg": "OK (unknown bytes)",
                "redirected": false,
                "status": 200,
                "url": "https://vcenter01.lab.test/rest/vcenter/host?filter.clusters=domain-c310"
            }
        ]
    }
}

From the full JSON I just need the values:

"name": "vmh20.lab.test"
"name": "vmh19.lab.test"

This output can give any number of hosts in the cluster depending in the cluster size.

I want to use those values as entry for hostname at the task:

 - name: Modify root local user to ESXi
    vmware_local_user_manager:
      hostname: '{{ item.results.json.value.name }}'
      username: root
      password: '{{ esxi_pass }}'
      local_user_name: root
      local_user_password: '{{ esxi_new_pass }}'
      validate_certs: False
    with_items:
    -  "{{ vchosts }}"

but it doesn't work as with the error:

TASK [Modify root local user to ESXi] **********************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'json'\n\nThe error appears to have been in '/Users/jv/Workspace/vmware-powershell/ansible/site.yml': line 90, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Modify root local user to ESXi\n    ^ here\n"}

Investigating a bit more using debug I tried

  - debug:
    var: item.json.value.name
    with_items:
    -  "{{ vchosts.results }}"

but the output is a

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
    "msg": "Hello world!"
}

I have also tried:

 - debug:
    var: item.vchosts.results.json.value[*].name
    with_items:
    -  "{{ vchosts }}"

  - debug:
    var: item
    with_items:
    -  "{{ vchosts | json_query('[*].value[*].{name: name}') }}"

and I got the same Hello world as before. the last I tried was

  - debug:
    var: item.name
    with_items:
    -  "{{ vchosts | json_query('*.value') }}"

and instead of a Hello world I get empty output in the task

TASK [debug] ***********************************************************************************************************************************************************************************************

I'm using ansible 2.5.4

Best Answer

If you just need the values "vmh20.lab.test" and "vmh19.lab.test" isn't this the code you're looking for?

- name: Modify root local user to ESXi
  vmware_local_user_manager:
    hostname: '{{ item }}'
    username: root
    password: '{{ esxi_pass }}'
    local_user_name: root
    local_user_password: '{{ esxi_new_pass }}'
    validate_certs: False
  with_items:
    - vmh20.lab.test
    - vmh19.lab.test