# Ansible 2.0  with\_items failing on variable that doesn't exist even though it is in where clause.

**URL:** <https://forum.ansible.com/t/ansible-2-0-with-items-failing-on-variable-that-doesnt-exist-even-though-it-is-in-where-clause/19836>\
**Category:** Ansible Project\
**Created:** [November 23, 2015, 10:28pm UTC](https://forum.ansible.com/t/ansible-2-0-with-items-failing-on-variable-that-doesnt-exist-even-though-it-is-in-where-clause/19836 "2015-11-23T22:28:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_LaCombe](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Alan\_LaCombe](https://forum.ansible.com/u/Alan_LaCombe)\
**Post date:** [November 23, 2015, 10:28pm UTC](https://forum.ansible.com/t/ansible-2-0-with-items-failing-on-variable-that-doesnt-exist-even-though-it-is-in-where-clause/19836/1 "2015-11-23T22:28:34Z")

</div>

This test playbook used to work in ansible 1.9.x and is no longer working. I have no variables (foo or php\_packages)defined for this example.

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 23, 2015, 11:39pm UTC](https://forum.ansible.com/t/ansible-2-0-with-items-failing-on-variable-that-doesnt-exist-even-though-it-is-in-where-clause/19836/2 "2015-11-23T23:39:16Z")

</div>

This has not changed, when: always executes after with\_ : , this is so  
you can use 'item' in your when clause to conditionally execute each  
loop iteration. This means you CANNOT use it to prevent an undefined  
error in with\_, you need to use default() to provide an empty list,  
which will skip the task:

- name: Install php and packages from php\_packages  
&nbsp;&nbsp;yum: name={{ item }} enablerepo=epel state=present  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- '{{ php\_packages|default() }}'

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 23, 2015, 11:39pm UTC](https://forum.ansible.com/t/ansible-2-0-with-items-failing-on-variable-that-doesnt-exist-even-though-it-is-in-where-clause/19836/3 "2015-11-23T23:39:28Z")

</div>

When statements are evaluated for every loop of with\_items. So your when statement will not prevent ansible from attempting to loop over the value.

You will need to ensure that the value is at least an empty list. Something like:

- name: Install php and packages from php\_packages  
yum: name={{ item }} enablerepo=epel state=present  
with\_items: php\_packages|default()

The side effect, is that you also don’t need to check that it is defined, as the default filter will set it to an empty list which has no iterations, if the var was not defined to begin with.

---

<div class="post-metadata">

**Author:** ![Alan\_LaCombe](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Alan\_LaCombe](https://forum.ansible.com/u/Alan_LaCombe)\
**Post date:** [November 24, 2015, 12:02am UTC](https://forum.ansible.com/t/ansible-2-0-with-items-failing-on-variable-that-doesnt-exist-even-though-it-is-in-where-clause/19836/4 "2015-11-24T00:02:17Z")

</div>

Thanks Brian (and Matt) the |default() did fix it. I did test it on 1.9.4 as presented and it did work. Here is my output

`  
[vagrant@ansibleserver provisioning]$ ansible --version  
ansible 1.9.4  
configured module search path = /usr/share/ansible

[vagrant@ansibleserver provisioning]$ ansible-playbook -i host test.yml

PLAY [configure and deploy the webservers and application code] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK: [test | debug msg "Foo is " {{ foo }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

TASK: [test | debug msg "Foo is " {{ foo }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

TASK: [test | Install generic php] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> (item=php)

TASK: [test | Install php and packages from php\_packages] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=1 unreachable=0 failed=0  
`

You can see the step does get skipped when running on 1.9.4
