gpt4 book ai didi

列表中的 Ansible 字典 - 获取字典的键和值

转载 作者:行者123 更新时间:2023-12-03 08:57:14 24 4
gpt4 key购买 nike

我有一个 ansible 剧本,其变量值如下 -

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

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

我想要 -

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

我尝试了 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
}}

关于列表中的 Ansible 字典 - 获取字典的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com