ansible - 列表中的 Ansible 字典 - 获取字典的键和值
问题描述
我有一个可变值的ansible剧本,如下所示 -
"instances": [
{
"architecture": "x86_64",
"tags": {
"A": "B",
"C": "D"
}
},
{
"architecture": "x86",
"tags": {
"A": "X",
"G": "D"
}
}
]
实例列表是动态的,每次运行时#values 可能会有所不同。
我想要 -
- 如果我们在整个列表中存在标签键“A”,则获取键“架构”的值。
- 如果整个列表中存在标签值“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
}}
推荐阅读
- node.js - React 应用程序应该直接与 cognito 通信,或者更好地在两者之间设置后端服务
- javascript - 使用 Puppeteer 下载文件时 Chrome 下载错误
- php - php localtime() 函数返回 UTC 时间而不是本地时间
- java - Java 代码在 NetBeans 中不间断地运行 (setDefaultCloseOperation)
- c - 如何在 C 中为跨平台应用程序着色输出
- json - Json4s杰克逊读写案例类不相等
- javascript - 预期的间谍导航已被调用
- python - CreateView 不保存表单 Django
- kubernetes - Pod 之间的通信
- r - Bookdown - 指定 MathJax URL?