# Conditional inclusion in template

**URL:** <https://forum.ansible.com/t/conditional-inclusion-in-template/12119>\
**Category:** Ansible Project\
**Created:** [December 5, 2012, 11:14am UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119 "2012-12-05T11:14:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [December 5, 2012, 11:14am UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/1 "2012-12-05T11:14:34Z")

</div>

Hi,

I’m looking for the idiomatic way of including a fragment in a j2 template only for hosts in a group called production.

In pseudo-code, I’m imagining something like this:

{% if host is “production” %}

…

{% endif %}

Is this an idiomatic approach? If so, what would the concrete syntax look like? If not, how do other people achieve this kind of thing?

Cheers,

Ben

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 6, 2012, 8:17pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/2 "2012-12-06T20:17:42Z")

</div>

have you looked into group\_by?

---

<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:** [December 7, 2012, 4:14pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/3 "2012-12-07T16:14:33Z")

</div>

No, what he's asking is how, in the middle of the template, do you  
make a decision based on whether something is in a group or not.  
Different question, it's about how you avoid having two templates if  
just a part of them changes.

I believe this is what you want...

{% if inventory\_hostname in groups["webservers"] %}

{% endif %}

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [December 7, 2012, 6:10pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/4 "2012-12-07T18:10:28Z")

</div>

Hi Brian,

Thanks for pointing this out - group\_by is definitely a nice dimension to  
know about. This has helped me solve the problem for now, although on  
reflection I think it would be conciser to be able to an equality test on  
an attribute called group within the context of the j2 template. But this  
gets me moving for now 🙂

Cheers,

Ben

---

<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:** [December 7, 2012, 6:15pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/5 "2012-12-07T18:15:13Z")

</div>

I think it would be conciser to be able to an equality test on an

> attribute called group within the context of the j2 template. But this gets  
> me moving for now 🙂

Did you see my email?

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [December 7, 2012, 6:16pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/6 "2012-12-07T18:16:50Z")

</div>

Hey Michael,

Sorry I didn't see your mail before Brian's - your suggestion is what I was  
looking for, so thank you for that.

Cheers,

Ben

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 7, 2012, 6:17pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/7 "2012-12-07T18:17:12Z")

</div>

Ben,

check the list/thread for Micheal’s post, it is the exact solution you wanted, group by forces the division up to the playbook level, it works but the ‘in-template’ solution Micheal points out is exactly what you wanted.

quoted here, JIC:

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [December 7, 2012, 6:17pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/8 "2012-12-07T18:17:49Z")

</div>

Yes, I did (see my reply from 1 minute ago) - thanks for following up on this 🙂

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [December 7, 2012, 6:18pm UTC](https://forum.ansible.com/t/conditional-inclusion-in-template/12119/9 "2012-12-07T18:18:47Z")

</div>

Hey Brian,

Thanks for the heads up 🙂

Cheers,

Ben
