XL.
XL.

Reputation: 1520

How to pull deeply nested data from a dictionary in ansible

I am trying to print nested data from an ansible dictionary. However, I cannot figure out how to pull the nested values. Specifically, in the data below, I am trying to print the two values returned under "ipv4". Currently, the output of item.value.ipv4 is the portion in brackets below:

"msg": "GigabitEthernet0/0/0,[{'address': '192.168.3.1', 'subnet': '28'}]"  

I would like to just use the value like so:

"msg": "GigabitEthernet0/0/0, 192.168.3.1, 28"

I cannot figure out how to pull this nested data out of to do this. To put it simply, it would be nice if something like this worked: item.value.ipv4['address']. How is this done?

  tasks:

    - name: get config for Cisco IOS
      ios_facts:
        gather_subset: all
        gather_network_resources: interfaces

   - name: create dictionary with ansible_net_interfaces
      set_fact:
        foo_value: "{{ query('dict', ansible_net_interfaces) }}"

    - name: display the results of foo_value
      debug:
        msg: "{{ foo_value }}"

    - name: display certain detalys values from foo_value
      debug:
        msg: "{{ item.key }},{{ item.value.ipv4 }}"
      with_items: "{{ foo_value }}" 

These tasks produce the following:

TASK [display the results of foo] **********************************************************************************************************************
ok: [192.168.3.1] => {
    "msg": [
        {
            "key": "GigabitEthernet0/0/0",
            "value": {
                "bandwidth": 1000000,
                "description": "This is an interface description",
                "duplex": "Full",
                "ipv4": [
                    {
                        "address": "192.168.3.1",
                        "subnet": "28"
                    }
                ],
                "lineprotocol": "up",
                "macaddress": "50f7.123c.b0c0",
                "mediatype": "RJ45",
                "mtu": 1500,
                "operstatus": "up",
                "type": "ISR4331-3x1GE"
            }
        },
        {
            "key": "GigabitEthernet0/0/1",
            "value": {
                "bandwidth": 1000000,
                "description": "This is another interface description",
                "duplex": "Full",
                "ipv4": [
                    {
                        "address": "192.168.3.33",
                        "subnet": "30"
                    }
                ],
                "lineprotocol": "up",
                "macaddress": "50f7.123c.b0c0",
                "mediatype": "RJ45",
                "mtu": 1500,
                "operstatus": "up",
                "type": "ISR4331-3x1GE"
            }
        },
    ]
}

Upvotes: 2

Views: 334

Answers (1)

franklinsijo
franklinsijo

Reputation: 18270

ipv4 is a list of dictionaries. Assuming you need only the first dictionary,

    - name: display certain detalys values from foo_value
      debug:
        msg: "{{ item.key }},{{ item.value.ipv4[0].address }},{{ item.value.ipv4[0].subnet }}"
      when: item.value.ipv4 is defined and item.value.ipv4[0].subnet is defined and item.value.ipv4[0].address is defined 
      with_items: "{{ foo_value }}"

Upvotes: 2

Related Questions