我使用以下文件:变量文件:db.yml
x86_64:
alpine:
version: 3.15.0
debian:
version: 11.3.0
aarch64:
alpine:
version: 3.15.0
debian:
version: 11.3.0
剧本:剧本.yml
---
- name: "Playbook"
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Import variables
ansible.builtin.include_vars:
file: db.yml
name: db
- name: DENUG >>> db
debug:
var: db
- name: DENUG >>> db
debug:
var: db[item].alpine
loop: "{{ db.keys()|list }}"
when: db[item].alpine is defined
输出:
ok: [localhost] => (item=x86_64) => {
"ansible_loop_var": "item",
"db[item].alpine": {
"version": "3.15.0"
},
"item": "x86_64"
}
ok: [localhost] => (item=aarch64) => {
"ansible_loop_var": "item",
"db[item].alpine": {
"version": "3.15.0"
},
"item": "aarch64"
}
现在我已将 db.yml 转换为新样式,如下所示:
x86_64:
- distribution: alpine
version: 3.15.0
- distribution: debian
version: 11.3.0
aarch64:
- distribution: alpine
version: 3.15.0
- distribution: debian
version: 11.3.0
问题:
如何运行类似的命令如下:
- name: DENUG >>> db
debug:
var: db[item].alpine
loop: "{{ db.keys()|list }}"
when: db[item].alpine is defined
并获得与输出中可用的相同结果。
基本上如何遍历这个新的 db.yml 来获取 db.x86_64.alpine.version 的值
例如,
给
通常,创建一个包含所有属性的列表
给
现在,您可以选择任何您想要的,例如