# with\_items task and when: statement

**URL:** <https://forum.ansible.com/t/with-items-task-and-when-statement/21493>\
**Category:** Ansible Project\
**Created:** [May 19, 2016, 4:09pm UTC](https://forum.ansible.com/t/with-items-task-and-when-statement/21493 "2016-05-19T16:09:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_Tognazzi](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Julien\_Tognazzi](https://forum.ansible.com/u/Julien_Tognazzi)\
**Post date:** [May 19, 2016, 4:09pm UTC](https://forum.ansible.com/t/with-items-task-and-when-statement/21493/1 "2016-05-19T16:09:23Z")

</div>

What is the proper way to skip a with\_items tasks if the with\_items variable is undefined ?

I’ll try to explain my use case:  
I have some variable defined via the group\_vars

And I want to do loop over this variable only if it is defined.

Before ansible 2 it worked without problem  
with a playbook like

`

---

<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:** [May 19, 2016, 4:17pm UTC](https://forum.ansible.com/t/with-items-task-and-when-statement/21493/2 "2016-05-19T16:17:25Z")

</div>

The correct way is to just drop the `when` statement and rely on the `|default` filter:

tasks:

- name: “some loop”  
debug: var=item  
with\_items:
- “{{ my\_var|default() }}”

`when` statements are evaluated for every iteration of the loop, not before the loop. So they are evaluated after ansible attempts to start looping.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [May 19, 2016, 4:21pm UTC](https://forum.ansible.com/t/with-items-task-and-when-statement/21493/3 "2016-05-19T16:21:14Z")

</div>

The other thing you could do is define an empty value in a role default,  
if you're in a role. (There doesn't seem to be a way to do that if you're  
not in a role, i.e. to define a playbook default that can be overridden by  
group or host variables, at least not that I see in the precedence list at  
[https://docs.ansible.com/ansible/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable) .)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
