如何解决如何写来自两个列表的行?
如何使用 Ansible 编写来自两个列表的行?
Bottle:
- wine:
- 1951
- 1960
- country_to_export:
- belgium-1
- belgium-2
main.yml 文件:
debug:
msg: "I send the bottle {{ item.0 }} to country {{ item.1 }}"
with_items:
- "{{ Bottle.wine }}"
- "{{ Bottle.country_to_export}}"
结果:
"I send the bottle [ u1951,u1960 ] to country [ ubelgium-1,ubelgium-2 ]"
想要的结果:
I send the bottle 1951 to country Belgium-1
I send the bottle 1960 to country Belgium-2
解决方法
您只是使用了错误类型的循环,您应该使用循环 with_together
而不是循环 with_items
。
给定剧本:
- hosts: all
gather_facts: yes
tasks:
- debug:
msg: "I send the bottle {{ item.0 }} to country {{ item.1 }}"
with_together:
- "{{ Bottle.wine }}"
- "{{ Bottle.country_to_export}}"
vars:
Bottle:
wine:
- 1951
- 1960
country_to_export:
- belgium-1
- belgium-2
总结如下:
PLAY [all] **********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]
TASK [debug] ********************************************************************************************************
ok: [localhost] => (item=[1951,'belgium-1']) => {
"msg": "I send the bottle 1951 to country belgium-1"
}
ok: [localhost] => (item=[1960,'belgium-2']) => {
"msg": "I send the bottle 1960 to country belgium-2"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
还要注意,在您给定的 bottle.yml 中,与您所说的相比,您的字典是错误的。
应该是:
Bottle:
wine:
- 1951
- 1960
country_to_export:
- belgium-1
- belgium-2
如果您想与即将推出的 Ansible 版本兼容,更好的语法是删除 with_*
结构并开始使用它们的 loop
replacement。
所以上面的剧本最终会是:
- hosts: all
gather_facts: yes
tasks:
- debug:
msg: "I send the bottle {{ item.0 }} to country {{ item.1 }}"
loop: "{{ Bottle.wine | zip(Bottle.country_to_export) | list }}"
vars:
Bottle:
wine:
- 1951
- 1960
country_to_export:
- belgium-1
- belgium-2
产生相同的回顾。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。