# Conditional include.

**URL:** <https://forum.ansible.com/t/conditional-include/15672>\
**Category:** Ansible Project\
**Created:** [July 15, 2014, 7:49am UTC](https://forum.ansible.com/t/conditional-include/15672 "2014-07-15T07:49:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [July 15, 2014, 7:49am UTC](https://forum.ansible.com/t/conditional-include/15672/1 "2014-07-15T07:49:09Z")

</div>

Hi,

main.yml:

- include: somefile.yml  
when: variable is defined

somefile.yml:

- name: do some stuff  
file: path=“/x/y/x/application{{ item }}” state=directory  
with\_sequence: start=“{{variable.start}}” end=“{{variable.end}}”

Runs into an error when variable is -not- defined, as it seems to include somefile.yml anyway and then complains about it being unable to parse the sequence. (unknown error parsing with\_sequence arguments:)  
Considering that the include is conditional, why is it even processing the file? I also tried adding the condition to this specific task, that is add the ‘when’ clause to somefile.yml, however this results in the same error.

Any ideas?

Thanks,  
Nico

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [July 15, 2014, 8:18am UTC](https://forum.ansible.com/t/conditional-include/15672/2 "2014-07-15T08:18:11Z")

</div>

​It's not the include that is conditional. include+when means applying the  
when on each task in the included file.  
Also, the combination of a when and a with loop, means, apply the when to  
each iterated item.

You'll need to extend your conditional: when variable is defined and  
variable.start is defined and variable end is defined.​

---

<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:** [July 15, 2014, 3:34pm UTC](https://forum.ansible.com/t/conditional-include/15672/3 "2014-07-15T15:34:11Z")

</div>

You can use “group\_by” if you want something that creates a dynamic group and it will show more concise output.
