我在剧本中有以下任务:
- name: task xyz
copy:
src="{{ item }}"
dest="/tmp/{{ item }}"
with_items: "{{ y.z }}"
when: y.z is defined
y.z
未定义,因此我希望跳过该任务。相反,我收到:
FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'z'"
我发现: How to run a task when variable is undefined in ansible? 但似乎我实现了这一点。我在这里做错了什么?
答案 0 :(得分:7)
这里的问题是在with_items
之前评估when
。实际上,在实际场景中,您将item
置于when
条件中。请参阅:Loops and Conditionals。
此任务适合您:
- name: task xyz
copy:
src: "{{ item }}"
dest: "/tmp/{{ item }}"
with_items: "{{ (y|default([])).z | default([]) }}"