# include a conditioanl role at playbook

**URL:** <https://forum.ansible.com/t/include-a-conditioanl-role-at-playbook/13558>\
**Category:** Ansible Project\
**Created:** [September 26, 2013, 12:49am UTC](https://forum.ansible.com/t/include-a-conditioanl-role-at-playbook/13558 "2013-09-26T00:49:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack1](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@jack1](https://forum.ansible.com/u/jack1)\
**Post date:** [September 26, 2013, 12:49am UTC](https://forum.ansible.com/t/include-a-conditioanl-role-at-playbook/13558/1 "2013-09-26T00:49:42Z")

</div>

Hi,

I am trying to include a conditional role in my playbook, this role will only apply to first node of my server group, and this server group is automatically created, the ip/hostnames are not fixed. So we could not use following format

roles:

- common
- { role: cassandra\_opscenter, when: (“10.1.1.1” == inventory\_hostname )}

I tried to use something like:

- { role: cluster\_config, when: “{% for host in groups[‘myservers’] %}{% if loop.index0 == 0 %}{{ host }}{% endif %}{% endfor %}” == inventory\_hostname }  
or
- { role: cassandra\_opscenter, when: (“groups[‘myservers’][0] == inventory\_hostname” )}

But got syntax error for both cases. Does anyone know the correct way to do that?

Thanks

---

<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:** [September 26, 2013, 1:23am UTC](https://forum.ansible.com/t/include-a-conditioanl-role-at-playbook/13558/2 "2013-09-26T01:23:59Z")

</div>

Hi Jack!

It’s not intended that you would need to use template expressions in the when statement (I wanted to make that not required to access variables), but it’s also true that conditional expressions in Jinja2 are not allowed the full python gamut. So that first version is probably going to throw it for a loop – and I think that’s fine, as that’s a bit too complex for the design aesthetic of ansible anyway – that is, we want to keep things simple when we can.

So you had this and this was close though:

- { role: cassandra\_opscenter, when: (“groups[‘myservers’][0] == inventory\_hostname” )}

I think is thrown only by the parens:

- { role: cassandra\_opscenter, when: groups.myservers[0] == inventory\_hostname }

should be fine.

The bracket around myservers like you had above is still valid, I just simplified it a bit

Let me know if this works for you!

An alternative would be to just have a micro-play in there:

hosts: myservers[0]  
tasks:  
…

Which just targets the head node ahead of the others.

I think that would generally be more intuitive, but may not make sense depending on what you want in your playbooks.

---

<div class="post-metadata">

**Author:** ![jack1](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@jack1](https://forum.ansible.com/u/jack1)\
**Post date:** [September 26, 2013, 3:26pm UTC](https://forum.ansible.com/t/include-a-conditioanl-role-at-playbook/13558/3 "2013-09-26T15:26:38Z")

</div>

Hi Mike:

Thank you so much for the response. I made the change like you suggested:

- { role: cassandra\_opscenter, when: groups.myservers[0] == inventory\_hostname }

But I still get following errors:  
fatal: [10.1.1.1] =\> Conditional expression must evaluate to True or False: (True) and ({% if groups.myservers[0] == inventory\_hostname %} True {% else %} False {% endif %})  
fatal: [10.1.1.2] =\> Conditional expression must evaluate to True or False: (True) and ({% if groups.myservers[0] == inventory\_hostname %} True {% else %} False {% endif %})  
fatal: [10.1.1.2] =\> Conditional expression must evaluate to True or False: (True) and ({% if groups.myservers[0] == inventory\_hostname %} True {% else %} False {% endif %})

By the way, I am using ansible server version 1.2.2.

Thanks  
Jack

---

<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:** [September 26, 2013, 10:29pm UTC](https://forum.ansible.com/t/include-a-conditioanl-role-at-playbook/13558/4 "2013-09-26T22:29:29Z")

</div>

Can you please try this in 1.3.X?

This is the currently released version.

Thanks!
