I’d like to map a role to a group of hosts with a specific operating system.
So far I read Applying ‘when’ to roles and includes and the module description of group_by. But I could not figure out how to use group_by to use a specific role only in cases where a variable matches an operating system. Could someone give an example, please?
Do I have to set some group_var first to use with group_by?
I'd like to map a role to a group of hosts with a specific operating system.
This should be possible with a playbook somewhat along these lines:
First play working on the relevant hosts (not filtered by OS) with one
pre_task:
- name: group by OS
group_by: key=bla_bla_{{ [ansible_fact_for_OS_name] }} # (you may add
a default, if necessary)
changed_when: False
The you'll want to have a second play in the same playbook working on
one (or more) of the just defined group(s), like:
- hosts: bla_bla_Debian # (or bla_bla_Windows, or whatever)
roles:
- [add-your-role-here]
You'll get an output for all hosts of the bigger group of play one, but
play two will run for just that hosts you want it to run for. At least
this way it works for me - hope it helps.