Bug Report
ansible 2.1.0
config file =
configured module search path = Default w/o overrides
Ubuntu 14.04
A task's with_items
seem to be evaluated before its when
clause.
If a variable used within an item is undefined, the task will fail even if the when clause says it should only run when that same variable is defined.
- hosts: localhost
tasks:
- name: Copy test file
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "root"
group: "root"
mode: "{{ item.mode }}"
with_items:
- { src: "{{ test_src }}", dest: "/tmp/test", mode: "0644" }
when: test_src is defined
Task is skipped since test_src
is undefined
Worked as expected in Ansible 1.9.4
ansible 1.9.4
configured module search path = None
TASK: [Copy test file] ********************************************************
skipping: [localhost] => (item={'dest': '/tmp/test', 'src': u'{# test_src #}', 'mode': '0644'})
TASK [Copy test file] **********************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! 'test_src' is undefined"}
Hi!
Thanks very much for your submission to Ansible. It sincerely means a lot to us.
We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.
with_
clauses ALWAYS evaluate before the when
clause, otherwise we cannot use item
and condition loop iterations. So this is not a bug, just how ansible works. The default
and ternary
filters can be used to avoid the undefined error.
In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.
Thank you once again for this and your interest in Ansible!
Most helpful comment
Possible Misunderstanding
Hi!
Thanks very much for your submission to Ansible. It sincerely means a lot to us.
We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.
with_
clauses ALWAYS evaluate before thewhen
clause, otherwise we cannot useitem
and condition loop iterations. So this is not a bug, just how ansible works. Thedefault
andternary
filters can be used to avoid the undefined error.In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.
Thank you once again for this and your interest in Ansible!