# Conditional Roles

**URL:** <https://forum.ansible.com/t/conditional-roles/18464>\
**Category:** Ansible Project\
**Created:** [June 11, 2015, 1:15am UTC](https://forum.ansible.com/t/conditional-roles/18464 "2015-06-11T01:15:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javeria\_Khan](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Javeria\_Khan](https://forum.ansible.com/u/Javeria_Khan)\
**Post date:** [June 11, 2015, 1:15am UTC](https://forum.ansible.com/t/conditional-roles/18464/1 "2015-06-11T01:15:59Z")

</div>

Not sure if this has been asked before, but I needed to include some playbooks conditionally, each of these was a top level that ran on different hosts:

- include: play1.yml  
when: “'var1 in some\_list”

- include: play2.yml  
when: “‘var2’ in some\_list”

But apparently this doesn’t work, no errors are generated, but the playbooks are always inlcuded and run even when the condition is false. So I took my conditions to another level inside and applied them to roles:

(play1.yml)

- hosts: hosts  
user: root  
roles:
- { role: role1, when: “'var1 in some\_list” }
- { role: role2, when: “'var1 in some\_list” }

But this results in a whole lot of skipped output in the run. There must a cleaner way to do this?

Thanks

---

<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:** [June 11, 2015, 1:21am UTC](https://forum.ansible.com/t/conditional-roles/18464/2 "2015-06-11T01:21:11Z")

</div>

The only conditional applicable to a play, is the hosts: selection.

Iincludes, roles and everything else won't get conditions applied, but  
they can pass them on to the only object that is subject to  
conditions, the tasks.

this changes a bit in v2 but that is still in development.

---

<div class="post-metadata">

**Author:** ![Javeria\_Khan](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Javeria\_Khan](https://forum.ansible.com/u/Javeria_Khan)\
**Post date:** [June 11, 2015, 2:36am UTC](https://forum.ansible.com/t/conditional-roles/18464/3 "2015-06-11T02:36:46Z")

</div>

‘Hosts’ has to have a group name infront of it right? To be able to apply the condition, could I conditionally include the relevant hosts to that group?

---

<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:** [June 11, 2015, 2:47am UTC](https://forum.ansible.com/t/conditional-roles/18464/4 "2015-06-11T02:47:40Z")

</div>

possibly, but the variables you have available in that scope is small.
