Ansible 有条件地依赖于YAML文件中的变量
我正在尝试有条件地使用playbook运行命令。该命令取决于将其类型值设置为“PBSTP”的给定客户。如果该类型设置为其他类型,则不应运行该播放。我目前正在尝试使用此配置,但它告诉我,条件失败,因为dict对象不包含“type”的值。 有没有办法做到这一点Ansible 有条件地依赖于YAML文件中的变量,ansible,jinja2,Ansible,Jinja2,我正在尝试有条件地使用playbook运行命令。该命令取决于将其类型值设置为“PBSTP”的给定客户。如果该类型设置为其他类型,则不应运行该播放。我目前正在尝试使用此配置,但它告诉我,条件失败,因为dict对象不包含“type”的值。 有没有办法做到这一点 - command: cp -r /home/pb/scripts /home/{{ item }}/scripts with_items: "{{ customers }}" when: customers['type'] == "
- command: cp -r /home/pb/scripts /home/{{ item }}/scripts
with_items: "{{ customers }}"
when: customers['type'] == "PBSTP"
在给定的示例中,我希望“SESH”客户端不会像“TEST-BANK”客户端那样获得脚本的目录副本。
谢谢大家!
TEST-BANK:
type: PBSTP
accept: 32506
connect: 33506
ipaddr:
- 192.167.203.92
subtype: INBOUND
SESH:
type: SPOT
accept: 32508
connect: 33508
ipaddr:
- 192.167.203.63
subtype: TIERED
您尝试将
when
条件应用于客户
dict(它有键测试银行
,SESH
等)
要将条件应用于循环迭代中的当前项:
- command: cp -r /home/pb/scripts /home/{{ item.key }}/scripts
with_dict: "{{ customers }}"
when: item.value['type'] == "PBSTP"
还请注意,您应该将
与_dict
一起使用,因为与_items
一起使用将只提供键,而不提供其值。谢谢您,康斯坦丁。你帮了我很多忙!