extracting data from dict in a loop

Hi,
I am trying to extract certain information from a complex dictionary generated by a parser.
For each interface which in the loop is the key, I would like to get the system_name which is several layers deeper. I want to wildcard item.value.port_id.* but this does not work. How can I accomplish this? I would want the following result:

GigabitEthernet1/1/1 has system name accesspoint-3-ap1
GigabitEthernet2/2/2 has system name accesspoint-4-ap2

  • name: Test data extraction
    hosts: local
    gather_facts: no
    vars:
    lldp1: {
    interfaces: {
    “GigabitEthernet1/1/1”: {
    “if_name”: “GigabitEthernet1/1/1”,
    “port_id”: {
    “AAAA.BBBB.0000”: {
    “neighbors”: {
    “accesspoint-3-ap1”: {
    “port_description”: “bond0”,
    “port_id”: “AAAA.BBBB.0000”,
    “system_name”: “accesspoint-3-ap1”
    }
    }
    }
    }
    },
    “GigabitEthernet2/2/2”: {
    “if_name”: “GigabitEthernet2/2/2”,
    “port_id”: {
    “BBBB.CCCC.1111”: {
    “neighbors”: {
    “accesspoint-4-ap2”: {
    “port_description”: “bond0”,
    “port_id”: “BBBB.CCCC.1111”,
    “system_name”: “accesspoint-4-ap2”
    }
    }
    }
    }
    }
    }
    }
    tasks:
  • name: get data
    debug:
    msg: “{{ item.key }} has system name {{ item.value.port_id.*.neighbors.system_name }}”
    loop: “{{ lldp1.interfaces | dict2items }}”

I'm sure that json_query could be improved but this should work:

  - name: get data
    debug:
      msg: "{{ item.key }} has system name {{ item |
json_query('value.port_id.*.neighbors|[0]|*.system_name|[0]') }}"
    loop: "{{ lldp1.interfaces | dict2items }}"