multi-platform role support: setting vars according to OS

72 views
Skip to first unread message

Nusenu

unread,
Feb 20, 2015, 2:01:39 PM2/20/15
to ansible...@googlegroups.com
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
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



Dan Vaida

unread,
Feb 21, 2015, 1:21:30 PM2/21/15
to ansible...@googlegroups.com, nus...@openmailbox.org
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 and use the ansible_distribution fact as a key.
Hope that helps.

Nusenu

unread,
Feb 21, 2015, 6:58:12 PM2/21/15
to ansible...@googlegroups.com
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 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

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

recap:
NOT working
-------------
- hosts: all
- name: create groups based on package manager
group_by: key="{{ansible_pkg_mgr}}"

roles:
- foorole
#foorole is not able to access vars defined in group_vars/apt /yum
-----------

working
-------------
- hosts: all
- name: create groups based on package manager
group_by: key="{{ansible_pkg_mgr}}"

- hosts: all
gather_facts: False
roles:
- foorole
# this time foorole can access stuff defined in group_vars/*
-----------

thank you,
Nusenu





Reply all
Reply to author
Forward
0 new messages