当使用Ansible的'when'条件时,如果条件是从数组中返回的结果,可以使用'with_items'来迭代数组并使用'when'来判断条件。以下是一个示例解决方法:
- name: Example playbook
hosts: all
vars:
my_array:
- 1
- 2
- 3
tasks:
- name: Execute task when condition is true
debug:
msg: "Condition is true"
when: item > 2
with_items: "{{ my_array }}"
在上面的示例中,我们使用'with_items'迭代'my_array'数组,并使用'when'条件来判断是否满足条件(item > 2)。如果满足条件,则执行debug任务,并输出"Condition is true"。