# with\_items: expects a list or a set by using it with when:

**URL:** <https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535>\
**Category:** Ansible Project\
**Created:** [October 17, 2014, 10:04am UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535 "2014-10-17T10:04:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bshbg](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@bshbg](https://forum.ansible.com/u/bshbg)\
**Post date:** [October 17, 2014, 10:04am UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535/1 "2014-10-17T10:04:14Z")

</div>

I have the following two statements which have the same when part. The first one is skipped correctly but the second creates an error. It should be skipped also.

`

- debug: var=“{{ sites[env].restricted\_areas }}”  
when: sites[env].restricted\_areas is defined

- template: src=“apache/site\_credentials” dest=“{{ apache\_vhost\_base\_path }}/{{ sites[env].server\_name }}/conf/{{ item.area.internal\_name }}”  
with\_items: sites[env].restricted\_areas | list  
when: sites[env].restricted\_areas is defined  
tags: create

`

result:

`  
TASK: [website | debug var=“{{sites[env].restricted\_areas}}”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [TSSDMZPORTAL002]

TASK: [website | template src=“apache/site\_credentials” dest=“{{apache\_vhost\_base\_path}}/{{sites[env].server\_name}}/conf/{{item.area.internal\_name}}”] \*\*\*  
fatal: [TSSDMZPORTAL002] =\> with\_items expects a list or a set  
`

can someone help me?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 20, 2014, 9:02pm UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535/2 "2014-10-20T21:02:21Z")

</div>

The when is evaluated for each step in the loop, not before looping, which is the source of confusion here.

---

<div class="post-metadata">

**Author:** ![CyberCyber](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@CyberCyber](https://forum.ansible.com/u/CyberCyber)\
**Post date:** [November 5, 2015, 10:42pm UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535/3 "2015-11-05T22:42:03Z")

</div>

I’m having the same issue. How can we make this task check if the variable exists before looping ? If we can’t use when here, what can we do ?

---

<div class="post-metadata">

**Author:** ![Markus\_Ellers](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Markus\_Ellers](https://forum.ansible.com/u/Markus_Ellers)\
**Post date:** [November 7, 2015, 10:11am UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535/4 "2015-11-07T10:11:54Z")

</div>

you could put the tasks in a different yml file and do a conditional include based on your “when”.

---

<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 7, 2015, 11:24pm UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535/5 "2015-11-07T23:24:41Z")

</div>

first no moustaches in var, it just wants a variable name

- debug: var=sites[env].restricted\_areas

you are putting several variables that can be undefined in your when  
statement so to be safe you should check each step:

when: env is defined and env in sites and 'restricted\_areas' in sites[env]

Now you can split them up and check each step to make sure what you  
expect to be defined is there

---

<div class="post-metadata">

**Author:** ![CyberCyber](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@CyberCyber](https://forum.ansible.com/u/CyberCyber)\
**Post date:** [November 8, 2015, 3:19pm UTC](https://forum.ansible.com/t/with-items-expects-a-list-or-a-set-by-using-it-with-when/16535/6 "2015-11-08T15:19:16Z")

</div>

Markus,

That’s exactly what I ended up doing. Thanks for your help.
