![将多个字典与列表组合成一个用于在 ansible 中循环的字典](https://linux22.com/image/743815/%E5%B0%86%E5%A4%9A%E4%B8%AA%E5%AD%97%E5%85%B8%E4%B8%8E%E5%88%97%E8%A1%A8%E7%BB%84%E5%90%88%E6%88%90%E4%B8%80%E4%B8%AA%E7%94%A8%E4%BA%8E%E5%9C%A8%20ansible%20%E4%B8%AD%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%AD%97%E5%85%B8.png)
我在 ansible 中每个主机有两个词典:
{ “rtt”:[38,1,97] }
{ “站点”:[“A”,“B”,“C”] }
我现在想要循环列表或项目,其中我可以引用各个项目,例如:
debug:
msg: "{{ item.site }} is {{ item.rtt }}"
with_items: "{{ X }}"
我如何构造 X?
答案1
这大概就是你在寻找的东西吧?
- hosts: localhost
vars:
rtt: [38,1,97]
site: ["A","B","C"]
tasks:
- debug:
msg: "{{ item.0 }} is {{ item.1 }}"
loop: "{{ site|zip(rtt)|list }}"
给出
msg: A is 38
msg: B is 1
msg: C is 97