我正在尝试遍历清单文件中的匹配主机,其中每个主机都有某些变量,但是每个主机可能具有与其关联的多个嵌套变量,如下所示:
[support]
myhost1 application_role="['role1', 'role2']" team="['ops', 'dev']"
myhost2 application_role="['role1']" team="['ops', 'sales']"
我的目标是尝试仅将文件传递给与客户变量键“ team”等于值“ sales”匹配的主机。
我正在测试此测试任务,只是为了获得一些响应,但是正如您从输出中看到的那样,它正在跳过所有这些,因为它没有捕获嵌套变量,它似乎是将变量作为一个完整的字符串读取而不是分裂?
- name: Loop through example servers and show the hostname, team attribute
debug:
msg: "team attribute of {{ item }} is {{ hostvars[item]['team'] }}"
when: hostvars[item]['team'] == "sales"
loop: "{{ groups['support'] }}"
PLAY [support] ************************************************************************
TASK [ssh_key_push : Loop through example servers and show the hostname, team attribute msg=team attribute of {{ item }} is {{ hostvars[item]['team'] }}] ***
skipping: [myhost1] => (item=myhost1)
skipping: [myhost1] => (item=myhost2)
skipping: [myhost1]
skipping: [myhost2] => (item=myhost1)
skipping: [myhost2] => (item=myhost2)
skipping: [myhost2]
我不确定如何从主机清单中读取单个嵌套变量。
谢谢!
答案 0 :(得分:1)
<div id="app">
<div id="box-container">
<div class="box" id="box-four">4</div>
</div>
<button id="button-insert-div">show or hide box</button>
<button id="button-delete-element">delete element</button>
</div>
此表达式正在比较一个列表,例如
when: hostvars[item]['team'] == "sales"
转换为单个字符串值team:
- ops
- sales
。这将始终返回false。
您想做的是check if the list contains that value。如该链接中所述,Jinja2提供了sales
测试,而ansbile提供了in
,在某些情况下可以简化编写过程。这两个版本是等效的:
contains