# How to use group\_by  to map a role only for a specific OS

**URL:** <https://forum.ansible.com/t/how-to-use-group-by-to-map-a-role-only-for-a-specific-os/22110>\
**Category:** Ansible Project\
**Created:** [July 25, 2016, 11:20am UTC](https://forum.ansible.com/t/how-to-use-group-by-to-map-a-role-only-for-a-specific-os/22110 "2016-07-25T11:20:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [July 25, 2016, 11:20am UTC](https://forum.ansible.com/t/how-to-use-group-by-to-map-a-role-only-for-a-specific-os/22110/1 "2016-07-25T11:20:07Z")

</div>

Dear all,

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](http://docs.ansible.com/ansible/playbooks_conditionals.html#applying-when-to-roles-and-includes) and the module description of [group\_by](http://docs.ansible.com/ansible/group_by_module.html). 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?

Kind regards,  
Joerg

---

<div class="post-metadata">

**Author:** ![Kai1](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kai1](https://forum.ansible.com/u/Kai1)\
**Post date:** [July 25, 2016, 2:22pm UTC](https://forum.ansible.com/t/how-to-use-group-by-to-map-a-role-only-for-a-specific-os/22110/2 "2016-07-25T14:22:14Z")

</div>

Hi Jörg!

> Dear all,
> 
> 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  
&nbsp;&nbsp;group\_by: key=bla\_bla\_{{ [ansible\_fact\_for\_OS\_name] }} # (you may add  
a default, if necessary)  
&nbsp;&nbsp;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)

&nbsp;&nbsp;roles:  
&nbsp;&nbsp;- [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.

Cheers, Kai

---

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [August 2, 2016, 12:05pm UTC](https://forum.ansible.com/t/how-to-use-group-by-to-map-a-role-only-for-a-specific-os/22110/3 "2016-08-02T12:05:48Z")

</div>

Hello Kai,

Thanks for your answer. But the method you mentioned don’t work for me. I created the following plays:

---

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [August 2, 2016, 12:21pm UTC](https://forum.ansible.com/t/how-to-use-group-by-to-map-a-role-only-for-a-specific-os/22110/4 "2016-08-02T12:21:20Z")

</div>

Ah, sorry. My mistake.

Here is my current playbook, which works as designed:
