2

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

1

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
  • no, as I was looking to get dynamically the list of nodes inside the cluster – Vidanez Jun 13 '18 at 8:53
0

I dont know why this

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

is not the same as

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

and actually that was driving me crazy, this is for sure a bug in this version of ansible

the problem is that it was tooking the elements in my first request as list inside a Json and you need to enumerate them.

In order to dont get a dirty "vchosts" JSON you need to use just one element from "vcclust", so the code end like:

 - name: Get vCenter cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/cluster?filter.names={{ cluster_touched }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vcclust

  - name: Get ESXi nodes from cluster ID for {{ cluster_touched }}
    uri:
      url: "https://{{ vcenter_hostname }}/rest/vcenter/host?filter.clusters={{ vcclust.json.value[0].cluster }}"
      force_basic_auth: yes
      validate_certs: no
      headers:
        Cookie: '{{ login.set_cookie }}'
    register: vchosts

  - name: Modify root local user to ESXi
    vmware_local_user_manager:
      hostname: '{{ item.name }}'
      username: root
      password: '{{ esxi_pass }}'
      local_user_name: root
      local_user_password: '{{ esxi_new_pass }}'
      validate_certs: False
    with_items:
    -  "{{ vchosts.json.value }}"
  • There is no bug in Ansible. Your indentation is wrong. Both, in the question and in the answer (though in different ways). – techraf Jun 13 '18 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.