代码之家  ›  专栏  ›  技术社区  ›  vivekyad4v

列表中的可翻译字典-获取字典的键和值

  •  0
  • vivekyad4v  · 技术社区  · 5 年前

    我有一本易懂的剧本,其价值如下-

    "instances": [
        {
            "architecture": "x86_64",
            "tags": {
                "A": "B",
                "C": "D"
            }
        },
        {
            "architecture": "x86",
            "tags": {
                "A": "X",
                "G": "D"
            }
        }
    ]
    

    实例列表是动态的,每次运行的值可能会有所不同。

    我想

    1. 如果整个列表中存在标记键“A”,则获取键“architecture”的值。
    2. 如果整个列表中存在标记值“d”,则获取键“architecture”的值。

    我试过 with_subelements 但没有运气,因为它期待一个清单。

    1 回复  |  直到 5 年前
        1
  •  1
  •   Konstantin Suvorov    5 年前

    第一个任务可以用纯金贾完成,第二个任务需要一些jmespath。

    - name: List archs with tag A present
      debug:
        msg: >-
          {{ instances
             | selectattr('tags.A','defined')
             | map(attribute='architecture')
             | list
             | unique
          }}
    
    - name: List archs with any tag set to D
      debug:
        msg: >-
          {{ instances
             | json_query('[?contains(values(tags),`D`)]')
             | map(attribute='architecture')
             | list
             | unique
          }}