# include when foo\['bar'\] is defined

**URL:** <https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817>\
**Category:** Ansible Project\
**Created:** [March 23, 2015, 7:45pm UTC](https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817 "2015-03-23T19:45:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bret\_Martin](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Bret\_Martin](https://forum.ansible.com/u/Bret_Martin)\
**Post date:** [March 23, 2015, 7:45pm UTC](https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817/1 "2015-03-23T19:45:03Z")

</div>

I have a number of plays like

- include: data-importer.yml  
when: instances[‘data-importer’] is defined

that no longer work after a recent Ansible upgrade. The include occurs even when instances[‘data-importer’] is not set. I tracked the behavior change down to between 1.8.1 and 1.8.2.

Is this supposed to still work?

---

<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:** [March 23, 2015, 7:46pm UTC](https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817/2 "2015-03-23T19:46:24Z")

</div>

The include should always happen, the when: should be applied to the  
included tasks and these should be skipped.

---

<div class="post-metadata">

**Author:** ![Bret\_Martin](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Bret\_Martin](https://forum.ansible.com/u/Bret_Martin)\
**Post date:** [March 23, 2015, 7:58pm UTC](https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817/3 "2015-03-23T19:58:31Z")

</div>

Thanks for the quick reply. That is definitely the behavior I've seen previously. Looking closer after your description of what happens, I note that the included tasks are being skipped. The failure is happening on a task that uses with\_items, where the with\_items line refers to the variable tested in the include/when.

Playbook/parent file:

- include: data-importer.yml  
&nbsp;&nbsp;when: instances['data-importer'] is defined

Included file 1:

- include: instance-common.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description="Data Importer"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance=data-importer

Included file 2:

- name: create {{ description }} instance  
&nbsp;&nbsp;ec2:  
&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;with\_items: instances[instance]["hostname"]

With this arrangement, I get

TASK: [aws-ec2 | create Data Importer instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [aws-vpc-hbi16058] =\> with\_items expects a list or a set

So my problem seems to be specific to with\_items and skipped tasks.

--Bret

---

<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:** [March 23, 2015, 8:10pm UTC](https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817/4 "2015-03-23T20:10:22Z")

</div>

when won't prevent with\_items from evaluating, since the when: can  
include the 'item' variable to be conditional per iteration. Add a

---

<div class="post-metadata">

**Author:** ![Bret\_Martin](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Bret\_Martin](https://forum.ansible.com/u/Bret_Martin)\
**Post date:** [March 23, 2015, 8:15pm UTC](https://forum.ansible.com/t/include-when-foo-bar-is-defined/17817/5 "2015-03-23T20:15:47Z")

</div>

Thanks again. I tried changing the play in the innermost file (actually, before I even started this thread) to

- name: create {{ description }} instance  
&nbsp;&nbsp;ec2:  
&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;with\_items: instances[instance]["hostname"] | default()

but the result is the same:

TASK: [aws-ec2 | create Data Importer instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [aws-vpc-hbi16058] =\> with\_items expects a list or a set

--Bret
