Using conditional roles

56 views
Skip to first unread message

Francois

unread,
Nov 14, 2015, 5:34:11 PM11/14/15
to Ansible Project
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?






Co S

unread,
Nov 15, 2015, 7:02:28 AM11/15/15
to Ansible Project
Hi Francois,

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

Regards,
Costea

Brian Coca

unread,
Nov 15, 2015, 3:08:31 PM11/15/15
to Ansible Project
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


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages