ansible$ cat playbooks/test_role_with_undefined_list.yml
---
- hosts: 127.0.0.1
connection: local
roles:
- { role: ansible_tests/debug_loop_on_list, list_of_items: '{{ possibly_undefined_list }}' }
ansible$ cat roles/ansible_tests/debug_loop_on_list/tasks/main.yml
- name: do something in a loop
debug: msg="loop var is '{{ item }}'"
with_items: list_of_items
ansible$ ansible-playbook -e '{ "possibly_undefined_list":[ "apple", "orange" ] }' playbooks/test_role_with_undefined_list.yml
PLAY [127.0.0.1] **************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [ansible_tests/debug_loop_on_list | do something in a loop] *************
ok: [127.0.0.1] => (item=apple) => {
"item": "apple",
"msg": "loop var is 'apple'"
}
ok: [127.0.0.1] => (item=orange) => {
"item": "orange",
"msg": "loop var is 'orange'"
}
PLAY RECAP ********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0
ansible$ ansible-playbook playbooks/test_role_with_undefined_list.yml
PLAY [127.0.0.1] **************************************************************
GATHERING FACTS ***************************************************************
ok: [127.0.0.1]
TASK: [ansible_tests/debug_loop_on_list | do something in a loop] *************
ok: [127.0.0.1] => (item=list_of_items) => {
"item": "list_of_items",
"msg": "loop var is 'list_of_items'"
}
PLAY RECAP ********************************************************************
127.0.0.1 : ok=2 changed=0 unreachable=0 failed=0
ansible$ cat - >/dev/null
EXPECTED BEHAVIOR ABOVE WAS:
Error: list_of_items is undefined.
ansible$ ansible --version
ansible 1.8.4
configured module search path = None
ansible$ grep error ansible.cfg
# by default (as of 1.3), Ansible will raise errors when attempting to dereference
#error_on_undefined_vars = False
ansible$ find roles/ansible_tests/debug_loop_on_list/ -type f
roles/ansible_tests/debug_loop_on_list/tasks/main.yml
ansible$
possibly_undefined_list should have thrown an undefined error before you ever try to access list_of_items on the second run.