我想要像这样在 ansible 中定义字典
vhosts:
git_branch_1:
- { a: example.com, customer: a }
- { a: example.com, customer: b }
- { a: example.org, customer: a }
git_branch_2:
- { a: example.com, customer: x }
- { a: example.org, customer: y }
有些任务我只需要循环字典键,这很好用
- name: "just debug"
debug: msg={{ item }}
with_items: "{{ vhosts.keys() }}"
但有些任务我想从每个键迭代列表,并将键附加为字典的另一个属性,所以我想从这个原始字典组合/创建新字典,如下所示:
combined_vhosts:
- { a: example.com, customer: a, branch: git_branch_1 }
- { a: example.com, customer: b, branch: git_branch_1 }
...
- { a: example.com, customer: x, branch: git_branch_2 }
在某些任务中,我只需要获取顶级域:
domains:
- example.com
- example.org
有没有办法,如何用 ansible setfacts/jinja2 表示法实现这一点,或者我是否必须在 python 中为 ansible 编写一个自定义插件?