# multi-platform role support: setting vars according to OS

**URL:** <https://forum.ansible.com/t/multi-platform-role-support-setting-vars-according-to-os/17581>\
**Category:** Ansible Project\
**Created:** [February 20, 2015, 7:01pm UTC](https://forum.ansible.com/t/multi-platform-role-support-setting-vars-according-to-os/17581 "2015-02-20T19:01:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 20, 2015, 7:01pm UTC](https://forum.ansible.com/t/multi-platform-role-support-setting-vars-according-to-os/17581/1 "2015-02-20T19:01:19Z")

</div>

Hi,

I'm creating a role that supports multiple platforms and should handle  
them transparently to the end user.

something like the following works fine for tasks/main.yml but doesn't  
work for default/main.yml

- include: apt.yml  
&nbsp;&nbsp;when: ansible\_pkg\_mgr == 'apt'

What is the best way to set vars based on facts (while still  
encapsulating it in a role)?

thanks!  
Nusenu

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [February 21, 2015, 6:21pm UTC](https://forum.ansible.com/t/multi-platform-role-support-setting-vars-according-to-os/17581/2 "2015-02-21T18:21:29Z")

</div>

Using that approach, the more platforms your hosts will sport, the more “skipping” you’ll see in your playbook runs.  
I’d use the group\_by module: [http://docs.ansible.com/group\_by\_module.html](http://docs.ansible.com/group_by_module.html) and use the ansible\_distribution fact as a key.  
Hope that helps.

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 21, 2015, 11:57pm UTC](https://forum.ansible.com/t/multi-platform-role-support-setting-vars-according-to-os/17581/3 "2015-02-21T23:57:51Z")

</div>

Hello Dan Vaida,

Dan Vaida wrote:

> Using that approach, the more platforms your hosts will sport, the  
> more "skipping" you'll see in your playbook runs. I'd use the  
> group\_by module: [http://docs.ansible.com/group\_by\_module.html](http://docs.ansible.com/group_by_module.html) and  
> use the ansible\_distribution fact as a key. Hope that helps.

thank you for your answer. The group\_by - way to do this is also an  
option for me but with that I can not encapsulate (and ship)  
everything in a single role anymore (galaxy) and the OS split happens  
in the playbook (instead of inside the role).

Is it uncommon to have a single role support multiple platforms with  
such platform dependent includes as it was my intention?  
I currently have 3 target platforms, in the future it might be four -  
but certainly not more.

..but after all I still don't know how to assign platform dependent  
vars within role/default/main.yml yet (my original question).

Dan Vaida wrote:

> I think your other thread to which I replied is related to this as

well.

That is correct - so I merged them into one thread for simplicity.

> Perhaps this example would be more helpful:

[https://github.com/danvaida/Ansible-Berlin-Meetup/blob/master/ansible-intro-examples/ghost.yml](https://github.com/danvaida/Ansible-Berlin-Meetup/blob/master/ansible-intro-examples/ghost.yml)

Ok, after some additional playing I got it, the trick is to specify  
'-hosts:' again after running the group\_by module.

recap:  
NOT working
