Including vars file based on another variable value in roles

Hi,

In the pre-role world, we were using syntax like:

var_files: var/{{ ansible_os_distribution }}.yml
tasks:

  • action: {{ ansible_pkg_mgr }} name={{ item }}
    with_items: {{ software }}

this kind of thing would be useful as a role. Is there anyway to include a varfile either directly based on OS, or via a variable name as in the above example?

Thanks,
Matt

That is quite elegant way how to install same packages with different names on different distributions.

I`m trying to mimic it in roles, but so far no luck. I cannot find a way to include different vars for different distributions in the same role :.

I actually don’t find that elegant.

First – One really should not use “ansible_pkg_manager” because it bypasses with the with_items optimizations that allow yum and apt packages to be installed in the same transaction set.

Second --No, you can’t load roles based on facts, and should not do this.

Rather, two options:

(A)

use the group_by module to select your webservers that are Ubuntu and your webservers that are Fedora (for example) and then apply the appropriate
roles to each,

(B) use the “when” parameter on the role like so:

roles:

  • { role: ubuntu_stuff, when: ansible_os_distribution == “Ubuntu” }
  • { role: common_stuff }

The group_by method will produce cleaner output when the playbook is run.

In many cases though, you should know what OS each of your servers are, so many people will not have to worry about dynamic application.

If you have a web farm where some servers are randomly differnet distributions, I recommend straightening that out :slight_smile:

in pre role i used group by and just tasks that would execute for yum or apt, each with their own vars list {{ansible_pkg_manager}}_webapp_deps.yml

Michael,

I actually don't find that elegant.

First -- One really should not use "ansible_pkg_manager" because it
bypasses with the with_items optimizations that allow yum and apt packages
to be installed in the same transaction set.

Was about to ask about odd behaviour if ansible_pkg_manage used, thanks for
explaining.

Second --No, you can't load roles based on facts, and should not do this.

Rather, two options:

(A)

use the group_by module to select your webservers that are Ubuntu and your
webservers that are Fedora (for example) and then apply the appropriate
roles to each,

(B) use the "when" parameter on the role like so:

roles:
    - { role: ubuntu_stuff, when: ansible_os_distribution == "Ubuntu" }
    - ...
    - { role: common_stuff }

The group_by method will produce cleaner output when the playbook is run.

In many cases though, you should know what OS each of your servers are, so
many people will not have to worry about dynamic application.

If you have a web farm where some servers are randomly differnet
distributions, I recommend straightening that out :slight_smile:

Working for several customers with different distributions, maybe not so
common use case :). So basically, if I want to have clean output, I should
have LAMP-Debian and LAMP-CentOS roles?

I can surely take duplicated code into separate .yml files and share it
between roles, is that 'best practise'?

David

Yes, definitely sharing common tasks from two different roles is doable and a good way to do it.

In 1.3, James also added role dependencies, which provide a cleaner way to do this, so you could have a fooapp_centos role that requires all the common
parts, but also the right OS specific parts as well.

So you can just have

  • hosts: XyzCustomerWebservers
    roles:
  • fooapp_centos