How to loop dictionary with nested dictionaries

Viewed 376

from the json below I need to gather the interface name and the unit name value in a loop. This is partial output of my json:

{
    "configuration": {
        "@": {
            "junos:changed-localtime": "2021-04-30 12:47:05 PDT",
            "junos:changed-seconds": "1619812025",
            "xmlns": "http://xml.juniper.net/xnm/1.1/xnm"
        },
        "interfaces": {
            "interface": [
                {
                    "name": "irb",
                    "unit": [
                        {
                            "family": {
                                "inet": {
                                    "address": [
                                        {
                                            "name": "100.111.10.4/24"
                                        }
                                    ],
                                    "mtu": 9100
                                }
                            },
                            "name": 4010
                        },
                        {
                            "family": {
                                "inet": {
                                    "address": [
                                        {
                                            "name": "100.127.9.2/31"
                                        }
                                    ]
                                }
                            },
                            "name": 4093
                        }
                    ]
                },
                {
                    "name": "lo0",
                    "unit": [
                        {
                            "family": {
                                "inet": {
                                    "address": [
                                        {
                                            "name": "100.127.0.32/32"
                                        }
                                    ],
                                    
                                }
                            },
                            "name": 0
                        }
                    ]
                }
            ]
        }
    }
}

The playbook I'm using is below:

- name: Print response
  debug:
    msg: "{{item.name}}.{{item|json_query('unit[*].name')}}"
  loop: "{{ test.parsed_output.configuration.interfaces.interface}}"
  ignore_errors: yes

The problem that I'm having is this:

"msg": "lo0.[0]"
"msg": "irb.[4010, 4093]"

What I want to get is:

"msg": "lo0.0"
"msg": "irb.4010"
"msg": "irb.4093"

Same outside name with each unit.name, but I don't know how to do this.

Thanks.

1 Answers

Iterate with_subelements, e.g.

    - debug:
        msg: "{{ item.0.name }} {{ item.1.name }}"
      with_subelements:
        - "{{ test.parsed_output.configuration.interfaces.interface }}"
        - unit

gives

  msg: irb 4010
  msg: irb 4093
  msg: lo0 0
Related