# conditional include of task file

**URL:** <https://forum.ansible.com/t/conditional-include-of-task-file/12748>\
**Category:** Ansible Project\
**Created:** [May 1, 2013, 9:17pm UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748 "2013-05-01T21:17:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [May 1, 2013, 9:17pm UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748/1 "2013-05-01T21:17:08Z")

</div>

Is there a way to include a task file base on a condition?

Something like:

- include: some\_task\_file  
&nbsp;&nbsp;&nbsp;when: do\_that\_work == True

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 2, 2013, 12:00pm UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748/2 "2013-05-02T12:00:08Z")

</div>

What you have above applies that condition to everything in the file. It does not conditionally include a file, and we will _NOT_ do that for architectural reasons.

The better way to do it is use the ‘group\_by’ module and start a new play against that dynamically created subgroup, as that will produce more elegant output.

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [May 2, 2013, 11:34pm UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748/3 "2013-05-02T23:34:42Z")

</div>

That's a cool idea. So you're suggesting something like:

- name: "define a subgroup based on a variable"  
&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- group\_by: key=has\_foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: foo

- name: "run subgroup tasks"  
&nbsp;&nbsp;hosts: has\_foo  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- shell: echo "I have foo!"

K

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 3, 2013, 12:40am UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748/4 "2013-05-03T00:40:04Z")

</div>

Well, the original example was about grouping on a variables value:

tasks:

- group\_by: key=asdf\_{{asdf}}

… later in a following play:

hosts: asdf\_True

I haven’t tried it with the when but suspect that would work too.

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@David\_Yu](https://forum.ansible.com/u/David_Yu)\
**Post date:** [November 13, 2013, 1:56pm UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748/5 "2013-11-13T13:56:48Z")

</div>

> Well, the original example was about grouping on a variables value:
> 
> tasks:
> 
> - group\_by: key=asdf\_{{asdf}}
> 
> … later in a following play:
> 
> hosts: asdf\_True
> 
> I haven’t tried it with the when but suspect that would work too.

It doesn’t work with when (error while evaluating conditional: {% if foo %} True {% else %} False {% endif %})  
Ran kahlil’s example on 1.3.4

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@David\_Yu](https://forum.ansible.com/u/David_Yu)\
**Post date:** [November 14, 2013, 3:12am UTC](https://forum.ansible.com/t/conditional-include-of-task-file/12748/6 "2013-11-14T03:12:25Z")

</div>

This works:  
when: foo is defined

The above error happens with:  
when: foo
