使用 Ansible 遍历复杂字典

Looping through complex dictionary with Ansible

我正在使用 Cisco ACI_REST 模块。我正在构建一个剧本来关闭我所有的 ACI EPG。 ACI_EPG 模块用于创建字典。

- name: Get List of EPGs
       cisco.aci.aci_epg:
         host: "{{ inventory_hostname }}"
         username: "{{ ansible_user }}"
         password: "{{ ansible_password }}"
         validate_certs: no
         state: query
       delegate_to: localhost
       register: epg_list

它创建了一个如下所示的字典:

  "item": {
        "key": "current",
        "value": [
            {
                "fvAEPg": {
                    "attributes": {
                        "dn": "uni/tn-PC/ap-PROD_AP/epg-VLAN80_EPG",
                        "shutdown": "no",
                     },
                    "children": [
                        {
                            "fvRsBd": {
                                "attributes": {
                                    "annotation": "",
                                    "tDn": "uni/tn-PC/BD-VLAN80_BD",
                                    "tRn": "BD-VLAN80_BD",
                                    "tType": "name",
                                    "tnFvBDName": "VLAN80_BD",
                                    "uid": "0"
                                },
                                "children": [
                                    {
                                        "fvSubnetBDDefCont": {
                                            "attributes": {
                                                "name": "",
                                                "nameAlias": "",
                                                "status": ""
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }

我需要遍历 DN 值。

"dn": "uni/tn-PC/ap-PROD_AP/epg-VLAN80_EPG"

我正在玩 dict2items 但卡住了。如何引用字典中的嵌套项?这是我正在使用的一些测试代码。

- name: Display EPG List
       ansible.builtin.debug:
         msg: "{{ item.key }} - {{ item.value }}"
       loop: "{{ epg_list | dict2items }}"  

谢谢,

问:"遍历 DN 值。"

答:试试json_query,例如

    - debug:
        msg: "{{ item }}"
      loop: "{{ epg_list.item.value|
                json_query('[].*.attributes.dn')|
                flatten }}"

给予

  msg: uni/tn-PC/ap-PROD_AP/epg-VLAN80_EPG

给定数据

  epg_list:
    item:
      key: current
      value:
      - fvAEPg:
          attributes:
            dn: uni/tn-PC/ap-PROD_AP/epg-VLAN80_EPG
            shutdown: 'no'
          children:
          - fvRsBd:
              attributes:
                annotation: ''
                tDn: uni/tn-PC/BD-VLAN80_BD
                tRn: BD-VLAN80_BD
                tType: name
                tnFvBDName: VLAN80_BD
                uid: '0'
              children:
              - fvSubnetBDDefCont:
                  attributes:
                    name: ''
                    nameAlias: ''
                    status: ''

如果数据是

  epg_list:
    current:
    - fvAEPg:
        attributes:
          dn: uni/tn-PC/ap-PROD_AP/epg-VLAN80_EPG
          shutdown: 'no'
        children:
        - fvRsBd:
            attributes:
              annotation: ''
              tDn: uni/tn-PC/BD-VLAN80_BD
              tRn: BD-VLAN80_BD
              tType: name
              tnFvBDName: VLAN80_BD
              uid: '0'
            children:
            - fvSubnetBDDefCont:
                attributes:
                  name: ''
                  nameAlias: ''
                  status: ''

下面的任务给出了相同的结果

    - debug:
        msg: "{{ item }}"
      loop: "{{ epg_list.current|
                json_query('[].*.attributes.dn')|
                flatten }}"