Using conditional roles

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:”

---

- 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?

Hi Francois,

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

Regards,
Costea

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
  roles:
    - install_master

- hosts: workers
  roles:
    - install_worker