# Using conditional roles

**URL:** <https://forum.ansible.com/t/using-conditional-roles/19766>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [November 14, 2015, 9:59pm UTC](https://forum.ansible.com/t/using-conditional-roles/19766 "2015-11-14T21:59:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Francois](https://forum.ansible.com/u/Francois)\
**Post date:** [November 14, 2015, 9:59pm UTC](https://forum.ansible.com/t/using-conditional-roles/19766/1 "2015-11-14T21:59:04Z")

</div>

Hello,

In the Ansible Roles documentation it’s written;

“While it’s probably not something you should do often, you can also conditionally apply roles like so:”

```auto
---

- hosts: webservers
  roles:
    - { role: some_role, when: "ansible_os_family == 'RedHat'" }

```

It seems conditional roles is the only way to have a unique playbook executing differentes installations concidering targeted servers types.

for instance, for kubernetes install:

- hosts:all  
roles :
- { role: install\_master, when: "server\_type == “master” }
- { role: install\_worker, when: "server\_type == “worker” }

Then, executing this playbook, i install both the masters and the workers, instead to have one playbook for each with one hosts group for each.  
So conditionnal roles seems pretty important and going to be used very often.  
What i have missed? Is there another way to to this?

---

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [November 15, 2015, 12:02pm UTC](https://forum.ansible.com/t/using-conditional-roles/19766/2 "2015-11-15T12:02:27Z")

</div>

Hi Francois,

You can also conditionally include vars /execute tasks within same role using unique playbook too.

Regards,  
Costea

---

<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:** [November 15, 2015, 8:08pm UTC](https://forum.ansible.com/t/using-conditional-roles/19766/3 "2015-11-15T20:08:20Z")

</div>

the thing about conditionals on roles, they only apply to tasks,  
handler, vars, etc are all handled BEFORE any tasks or conditionals  
are processed.

A better way to handle host/group based conditionals are plays:

- hosts: masters  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- install\_master

- hosts: workers  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- install\_worker
