# problem with with\_items + when in ansible 2.2

**URL:** <https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330>\
**Category:** Ansible Project\
**Created:** [December 23, 2016, 7:27pm UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330 "2016-12-23T19:27:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spike1](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@Spike1](https://forum.ansible.com/u/Spike1)\
**Post date:** [December 23, 2016, 7:27pm UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330/1 "2016-12-23T19:27:34Z")

</div>

Hi,

I found this bug, which is closed, but I believe I’m still seeing the same problem:

Ansible 2.x errors on the task with the `with_items` loop with `when: some_var is defined` condition when it is used in a role, but not in a play

[https://github.com/ansible/ansible/issues/14383](https://github.com/ansible/ansible/issues/14383)

In my case I have a very simple task in a role:

- name: install sw requested for this box

apt:  
name: “{{item}}”  
state: present  
install\_recommends: yes  
with\_items: “{{additional\_sw}}”  
when: additional\_sw is defined

this fails with an error:

fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “‘additional\_sw’ is undefined”}

which is true, but the point of the _when:_ is exactly to avoid that. To make it even weirder, I tried to move the _when:_ to a _block_ wrapping that task, and still get the same error.

Eventually I defined a default value to an empty list in role/defaults/main.yml:

additional\_sw:

this worked but I’d still prefer to resolve the above problem.

any idea what’s going on?

thanks,

Spike

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 23, 2016, 8:16pm UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330/2 "2016-12-23T20:16:53Z")

</div>

> I found this bug, which is closed, but I believe I'm still seeing the same  
> problem:
> 
> Ansible 2.x errors on the task with the `with_items` loop with `when:  
> some\_var is defined` condition when it is used in a role, but not in a play  
> [https://github.com/ansible/ansible/issues/14383](https://github.com/ansible/ansible/issues/14383)
> 
> In my case I have a very simple task in a role:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: install sw requested for this box  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apt:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{item}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install\_recommends: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{additional\_sw}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: additional\_sw is defined
> 
> this fails with an error:
> 
> fatal: [localhost]: FAILED! =\> {"failed": true, "msg": "'additional\_sw' is  
> undefined"}
> 
> which is true, but the point of the \*when:\* is exactly to avoid that. To  
> make it even weirder, I tried to move the \*when:\* to a \*block\* wrapping  
> that task, and still get the same error.

You have the point of when wrong, this is by design.  
When is applied to every iteration of elements in with\_items.

You can read more about it here  
[https://docs.ansible.com/ansible/playbooks\_conditionals.html#loops-and-conditionals](https://docs.ansible.com/ansible/playbooks_conditionals.html#loops-and-conditionals)

> Eventually I defined a default value to an empty list in  
> role/defaults/main.yml:
> 
> additional\_sw:
> 
> this worked but I'd still prefer to resolve the above problem.
> 
> any idea what's going on?

Just remove the when, an change the with\_items to  
with\_items: "{{ additional\_sw | default() }}"

This translate to, if additional\_sw variable is not defined use a empty list, the result is that the task will be skipped.

---

<div class="post-metadata">

**Author:** ![Spike1](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@Spike1](https://forum.ansible.com/u/Spike1)\
**Post date:** [December 23, 2016, 9:13pm UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330/3 "2016-12-23T21:13:19Z")

</div>

thanks for the explanation Kai, makes sense, it effectively does the same thing I’ve done by setting a default value, but indeed I didn’t understand how when worked in conjunction with the with\_items.

Here’s however something else related that’s happening and I’m not understanding: why does this happens also if I move the task in a block and apply the when to that?

_- block:_

_- name: install sw requested for this box_  
_apt:_  
_name: “{{item}}”_  
_state: present_  
_install\_recommends: yes_  
_with\_items: “{{additional\_sw}}”_  
_when: additional\_sw is defined_

shouldn’t the task only be considered if the block is executed? why in this case i’m still getting the same error? and in fact, I also see the problem elsewhere with an include, which is even stranger:

_#tasks/main.yml_

_- include: interfaces.yml_  
_when: server\_interfaces is defined_  
_tags:_  
_- netwrk_

_#tasks/interfaces.yml_

_- name: generate new interfaces file_  
_template:_  
_src: interfaces.j2_  
_dest: /etc/network/interfaces_  
_owner: root_  
_group: adm_  
_mode: 0640_  
_notify:_  
_- restart network_

_- name: setup rt\_tables_  
_lineinfile:_  
_dest: /etc/iproute2/rt\_tables_  
_regexp: "^{{[item.id](http://item.id)}} "_  
_line: “{{[item.id](http://item.id)}} {{item.tname}}”_  
_with\_items: “{{server\_rt\_tables}}”_  
_when: server\_rt\_tables is defined_

If I don’t define server\_interfaces I would expect the interfaces.yml to be skipped. Is that not correct? Instead what I’m seeing is the “generate new interfaces” task being skipped while the setup rt\_tables throws the same error. It seems like the tasks are “interpreted” even tho they are supposed to be skipped, which doesn’t make sense to me.

any thoughts?

thanks,

Spike

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 23, 2016, 10:07pm UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330/4 "2016-12-23T22:07:23Z")

</div>

> thanks for the explanation Kai, makes sense, it effectively does the same  
> thing I've done by setting a default value, but indeed I didn't understand  
> how when worked in conjunction with the with\_items.
> 
> Here's however something else related that's happening and I'm not  
> understanding: why does this happens also if I move the task in a block and  
> apply the when to that?

You should really read the documentation, it's documented.  
[https://docs.ansible.com/ansible/playbooks\_blocks.html](https://docs.ansible.com/ansible/playbooks_blocks.html)

> \* - block:\*  
> \* - name: install sw requested for this box\*  
> \* apt:\*  
> \* name: "{{item}}"\*  
> \* state: present\*  
> \* install\_recommends: yes\*  
> \* with\_items: "{{additional\_sw}}"\*  
> \* when: additional\_sw is defined\*
> 
> shouldn't the task only be considered if the block is executed? why in this  
> case i'm still getting the same error? and in fact, I also see the problem  
> elsewhere with an include, which is even stranger:

All the tasks in a block inherit the when, so it's the same as adding a when on all the task.

> \*#tasks/main.yml\*  
> \* - include: interfaces.yml\*  
> \* when: server\_interfaces is defined\*  
> \* tags:\*  
> \* - netwrk\*
> 
> \*#tasks/interfaces.yml\*
> 
> \* - name: generate new interfaces file\*  
> \* template:\*  
> \* src: interfaces.j2\*  
> \* dest: /etc/network/interfaces\*  
> \* owner: root\*  
> \* group: adm\*  
> \* mode: 0640\*  
> \* notify:\*  
> \* - restart network\*
> 
> \* - name: setup rt\_tables\*  
> \* lineinfile:\*  
> \* dest: /etc/iproute2/rt\_tables\*  
> \* regexp: "^{{item.id \<[http://item.id](http://item.id)\>}} "\*  
> \* line: "{{item.id \<[http://item.id](http://item.id)\>}} {{item.tname}}"\*  
> \* with\_items: "{{server\_rt\_tables}}"\*  
> \* when: server\_rt\_tables is defined\*
> 
> If I don't define server\_interfaces I would expect the interfaces.yml to be  
> skipped. Is that not correct? Instead what I'm seeing is the "generate new  
> interfaces" task being skipped while the setup rt\_tables throws the same  
> error. It seems like the tasks are "interpreted" even tho they are supposed  
> to be skipped, which doesn't make sense to me.

You should read the entire page that I linked to in my previous mail  
[https://docs.ansible.com/ansible/playbooks\_conditionals.html](https://docs.ansible.com/ansible/playbooks_conditionals.html)

All the task in the include inherits the when.  
So all task is executed with the when applied, it's same as manually adding when to every single task.

> any thoughts?

When alway get applied to the tasks, not the include or the block.  
And the Ansible documentation is pretty good, I do recommend reading it, you will pick up many useful things 🙂

---

<div class="post-metadata">

**Author:** ![Spike1](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@Spike1](https://forum.ansible.com/u/Spike1)\
**Post date:** [December 24, 2016, 1:54am UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330/5 "2016-12-24T01:54:15Z")

</div>

Kai,

wow, school’ed :). I read the docs way back when I picked up _when_ and _blocks_ for the first time and used them for months without a problem thinking I understood what was going on. So when these issues arose I didn’t think of checking again. Obviously I had misunderstood it the first time around. Thank you again for the clarifications and reminders.

Spike

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 24, 2016, 9:37am UTC](https://forum.ansible.com/t/problem-with-with-items-when-in-ansible-2-2/23330/6 "2016-12-24T09:37:09Z")

</div>

> wow, school'ed :).

Sorry about that 🙂

> I read the docs way back when I picked up \*when\* and  
> \*blocks\* for the first time and used them for months without a problem  
> thinking I understood what was going on. So when these issues arose I  
> didn't think of checking again. Obviously I had misunderstood it the first  
> time around. Thank you again for the clarifications and reminders.

I forgot that in 2.0 and 2.1 this gave a deprecation warning with a message that this feature will be removed in future releases and that it will be a fatal error.  
So you're right, it did work until 2.2.
