Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Ansible get information from esxi advanced settings nested dictionary with unique keynames

After my earlier post ansible create an array with set_fact, I wanted to grab some selected information from VMware ESXi Advanced setting. but the problem was when I use ansible module vmware_host_config_info, information generated here was in below json format. (Note:  There are tons of advanced settings in ESXi, I have just trimmed down json info so I can show what settings i want).

I was only looking for information Vpx.Vpxa.config.vpxa.hostKey and ScratchConfig.CurrentScratchLocation, which was in nested format. Also using below nested json table is challenge, everytime after hosts_info json keyname (hostname) is unique as highlighted.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
    "results": [
		{
			"hosts_info": {
				"ironman.vcloud-lab.com": {
					"Vpx.Vpxa.config.vpxa.hostIp": "192.168.34.21",
					"Vpx.Vpxa.config.vpxa.hostKey": "52e260fb-1d51-bff2-04a9-0da6e572140f",
					"ScratchConfig.CurrentScratchLocation": "/vmfs/volumes/5d5584c1-352a2b96-8b4f-000c2989e4d3"
				}
			}
		},
		{
			"hosts_info": {
				"hulk.vcloud-lab.com": {
					"Vpx.Vpxa.config.vpxa.hostIp": "192.168.34.25",
					"Vpx.Vpxa.config.vpxa.hostKey": "526931ea-b1f4-66b2-8547-00b913ebe3da",
					"ScratchConfig.CurrentScratchLocation": "/vmfs/volumes/5d5584f0-af5352e4-9c3b-000c290f51b1"
				}
			}
		}
	]
}

As per below yml playbook I can fetch the required information and show like in below 2 ways using ansible function json_query/join (line no 48 to 59) and jinja 2 loop using json_query/join (line no 61 to 73), I like the Jinja 2 way because It shows output very neat and clean plus best thing it creates a variable for gathered information which I can use later in the script. 

playbook_deepnesteddictionary.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
- hosts: localhost
  gather_facts: False
  connection: local
  tasks:
    - name: Include Secret Environment Items
      include_vars:
        file: secrets.yml
        name: secret
      
    - name: vCenter Login
      uri:
        url: "https://{{secret.vcenter}}/rest/com/vmware/cis/session"
        force_basic_auth: yes
        method: POST
        user: "{{secret.username}}"
        password: "{{secret.password}}"
        status_code: 200
        validate_certs: no
      register: login

    - name: Get hosts from vCenter
      uri:
        url: "https://{{secret.vcenter}}/rest/vcenter/host"
        force_basic_auth: yes
        validate_certs: no
        headers:
          Cookie: "{{login.set_cookie}}"
      register: vchosts

    - name: filter list of only connected esxi
      set_fact: 
        esxilist: "{{esxilist | default([]) + [item]}}"
      with_items: "{{vchosts.json.value}}"
      when: item.connection_state == "CONNECTED"
    
    - name: Get ESXi advanced settings
      vmware_host_config_info:
        hostname: "{{secret.vcenter}}"
        username: "{{secret.username}}"
        password: "{{secret.password}}"
        esxi_hostname: "{{item.name}}"
        validate_certs: no
      with_items: "{{esxilist}}"
      register: completehosts
      when: item.connection_state == "CONNECTED"

    - name: Method 1 - Resolved - Only hosts_info from complete report using json_query
      debug:
        msg: 
          "HostName": "{{ item.hosts_info.keys()[0] }}"
          "Vpx.Vpxa.config.vpxa.hostIp": "{{ item.hosts_info | json_query('*.\"Vpx.Vpxa.config.vpxa.hostIp\"') | join(',') }}"
          "Vpx.Vpxa.config.vpxa.hostKey": "{{ item.hosts_info | json_query('*.\"Vpx.Vpxa.config.vpxa.hostKey\"') | join(',') }}"
          "ScratchConfig.CurrentScratchLocation": "{{ item.hosts_info | json_query('*.\"ScratchConfig.CurrentScratchLocation\"') | join(',') }}"
      loop: "{{ completehosts.results }}"
      loop_control:
        label: "{{ item.hosts_info.keys()[0]  }}"
      failed_when: "item.hosts_info is undefined"
      ignore_errors: True
    
    - name: Method 2 - Resolved - Only hosts_info from complete report using jinja2
      debug:
        msg: |
          {% set info = [] -%}
          {% for infoarray in completehosts.results -%}
            {% set singlehost = infoarray.hosts_info -%}
            {% set hostkeyname = singlehost.keys() | join(', ') -%}
            {% set ipaddress = singlehost | json_query('*."Vpx.Vpxa.config.vpxa.hostIp"') | join(', ') -%}
            {% set hostkey = singlehost | json_query('*."Vpx.Vpxa.config.vpxa.hostKey"') | join(', ') -%}
            {% set scratch = singlehost | json_query('*."ScratchConfig.CurrentScratchLocation"') | join(', ') -%}
            {% set ignored = info.extend([{'hostname': hostkeyname, 'ip': ipaddress, 'key': hostkey, 'scratch': scratch}]) -%}
          {%- endfor %}
          {{info}}

Download this playbook here, same playbook is also available on github.com.

Results look like below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[root@centos01 002_Get_esxi_advanced_settings]# ansible-playbook playbook_deepnesteddictionary.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

/usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.24.3) or chardet (2.2.1) doesn't match a supported version!
  RequestsDependencyWarning)

PLAY [localhost] *********************************************************************************************************************************************

TASK [Include Secret Environment Items] **********************************************************************************************************************
ok: [localhost]

TASK [vCenter Login] *****************************************************************************************************************************************
ok: [localhost]

TASK [Get hosts from vCenter] ********************************************************************************************************************************
ok: [localhost]

TASK [filter list of only connected esxi] ********************************************************************************************************************
ok: [localhost] => (item={u'host': u'host-31', u'connection_state': u'CONNECTED', u'power_state': u'POWERED_ON', u'name': u'ironman.vcloud-lab.com'})
ok: [localhost] => (item={u'host': u'host-43', u'connection_state': u'CONNECTED', u'power_state': u'POWERED_ON', u'name': u'hulk.vcloud-lab.com'})

TASK [Get ESXi advanced settings] ****************************************************************************************************************************
ok: [localhost] => (item={u'host': u'host-31', u'connection_state': u'CONNECTED', u'power_state': u'POWERED_ON', u'name': u'ironman.vcloud-lab.com'})
ok: [localhost] => (item={u'host': u'host-43', u'connection_state': u'CONNECTED', u'power_state': u'POWERED_ON', u'name': u'hulk.vcloud-lab.com'})

TASK [Method 1 - Resolved - Only hosts_info from complete report using json_query] ***************************************************************************
ok: [localhost] => (item=ironman.vcloud-lab.com) => {
    "msg": {
        "HostName": "ironman.vcloud-lab.com",
        "ScratchConfig.CurrentScratchLocation": "/vmfs/volumes/5d5584c1-352a2b96-8b4f-000c2989e4d3",
        "Vpx.Vpxa.config.vpxa.hostIp": "192.168.34.21",
        "Vpx.Vpxa.config.vpxa.hostKey": "52e260fb-1d51-bff2-04a9-0da6e572140f"
    }
}
ok: [localhost] => (item=hulk.vcloud-lab.com) => {
    "msg": {
        "HostName": "hulk.vcloud-lab.com",
        "ScratchConfig.CurrentScratchLocation": "/vmfs/volumes/5d5584f0-af5352e4-9c3b-000c290f51b1",
        "Vpx.Vpxa.config.vpxa.hostIp": "192.168.34.25",
        "Vpx.Vpxa.config.vpxa.hostKey": "526931ea-b1f4-66b2-8547-00b913ebe3da"
    }
}

TASK [Method 2 - Resolved - Only hosts_info from complete report using jinja2] *******************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "hostname": "ironman.vcloud-lab.com",
            "ip": "192.168.34.21",
            "key": "52e260fb-1d51-bff2-04a9-0da6e572140f",
            "scratch": "/vmfs/volumes/5d5584c1-352a2b96-8b4f-000c2989e4d3"
        },
        {
            "hostname": "hulk.vcloud-lab.com",
            "ip": "192.168.34.25",
            "key": "526931ea-b1f4-66b2-8547-00b913ebe3da",
            "scratch": "/vmfs/volumes/5d5584f0-af5352e4-9c3b-000c290f51b1"
        }
    ]
}

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Useful Articles
Ansible selectattr The error was TemplateRuntimeError no test named 'equalto'
Getting started Ansible AWX tower for IT automation run first playbook
How to install Docker on Linux
Cannot connect to the Docker daemon at unix:var run docker.sock. Is the docker daemon running
Docker Error response from daemon i\o timeout internet proxy
How to install Ansible on Linux for vSphere configuration

Go Back

Comment

Blog Search

Page Views

4127726

Follow me on Blogarama