访问 Ansible 中 JSON 格式列表的每个键的值

Accessing the value of every key of a JSON formatted list in Ansible

我有以下列表,我在 cisco 控制器的 json_query 的帮助下查询了这些列表。

"aci_access_port_list": [
            [
                {
                    "description": [
                        "NOMON KASTOM"
                    ], 
                    "port_name": "E1-17"
                }, 
                {
                    "description": [
                        "ASDF NOMON K"
                    ], 
                    "port_name": "E1-16"
                }, 
                {
                    "description": [
                        "NOMON K9876"
                    ], 
                    "port_name": "E1-15"
                }, 
                {
                    "description": [
                        "LAN5002 TC "
                    ], 
                    "port_name": "E1-46"
                }, 
                {
                    "description": [
                        "LAN5002 TC "
                    ], 
                    "port_name": "E1-47"
                }, 
                {
                    "description": [
                        "NOMON LAN5001 TC "
                    ], 
                    "port_name": "E1-45"
                }, 
                {
                    "description": [
                        "LAN5001 TC "
                    ], 
                    "port_name": "E1-48"
                }, 
                {
                    "description": [
                        "NOMON No Cabling - TEST-4-AUTOMATION"
                    ], 
                    "port_name": "E1-30"
                }, 
                {
                    "description": [
                        "NOMON No Cabling - TEST-6-AUTOMATION"
                    ], 
                    "port_name": "E1-31"
                }
            ]
        ]

我想检查每个 description 键,如果它写有“NOMON K”,则将其保存到新列表中。为此,我写了以下内容:

- set_fact:
    clean_list: [\n]
- name: getting the clean list of ports and descr.
  set_fact:
    clean_list: "{{clean_list }} \n {{item.0.port_name}} - {{item.1}}"
  loop: "{{ aci_access_port_list[0]|subelements('description') }}"
  when: item.1 is search ("NOMON K")

- debug:
    msg: "{{ clean_list }}"

和这个 returns:

ok: [acitst-apic.net.dsh.at] => {
    "msg": "[u'\\n'] \n E1-17 - NOMON KASTOM \n E1-16 - ASDF NOMON K \n E1-15 - NOMON K9876"
}

我想要的格式与 aci_access_port_lists 相同,但我无法弄清楚。它应该看起来像:

"clean_list":[
                {
                    "description": [
                        "NOMON KASTOM"
                    ], 
                    "port_name": "E1-17"
                }, 
                {
                    "description": [
                        "ASDF NOMON K"
                    ], 
                    "port_name": "E1-16"
                }, 
                {
                    "description": [
                        "NOMON K9876"
                    ], 
                    "port_name": "E1-15"
                }
             ]

我认为有更好的方法,但我找不到它,如果有人能帮助我,我将很高兴。提前谢谢你。

使用 selectattr 过滤器和 contains 测试很容易实现,目标是 description 列表的第一个元素,因为它始终只有一个元素。当然,如果该列表中有多个元素,这种方法就会失效。

    - name: Show me a clean list
      vars:                                                                                                  
        clean_list: >-
          {{
            aci_access_port_list.0 |
            selectattr('description.0', 'contains', 'NOMON K') |
            list
          }}
      debug:
        var: clean_list