簡體   English   中英

列表中的Ansible字典-獲取字典的鍵和值

[英]Ansible dictionary inside list - get keys and values of the dictionary

我有一個Ansible劇本,其可變值如下所示-

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

實例列表是動態的,並且#values每次運行可能會有所不同。

我想要 -

  1. 如果我們在整個列表中都存在標記鍵“ A”,則獲取鍵“體系結構”的值。
  2. 如果我們在整個列表中都存在標記值“ D”,則獲取鍵“體系結構”的值。

我嘗試了with_subelements但沒有運氣,因為它需要一個列表。

第一個任務可以使用純Jinja來實現,第二個任務則需要一些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
      }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM