我有一个ansible变量,它是一个字典时间,我想将它与字符串变量或逗号分隔的输入进行比较。以下是我尝试使用
编译的剧本- hosts: localhost
gather_facts: false
vars:
cluster:
name:
- "raj"
- "kiran"
- "Pavan"
inputcluster:
- "raj"
tasks:
# inner.yml
- debug:
msg: "outer item={{ outer_item.cluster.name }} "
with_items:
- cluster:
name:
- "raj"
- "kiran"
- "Pavan"
loop_control:
loop_var: outer_item
when:
- inputcluster
- outer_item.cluster.name == inputcluster`
我希望echo语句通过将字符串与字典进行比较来仅打印“ raj”。我该如何实现?
答案 0 :(得分:0)
我认为您必须遍历名称列表以匹配它,并且要遍历两个需要嵌套循环的列表。这可以解决问题。
- hosts: localhost
gather_facts: false
vars:
cluster:
name:
- "raj"
- "kiran"
- "Pavan"
inputcluster:
- "raj"
tasks:
# inner.yml
- debug:
msg: "outer item={{ item[1] }} "
with_nested:
- "{{ inputcluster }}"
- "{{ cluster.name }}"
when: (item[0] == item[1])