Multiple role default variable files: dynamic includes and variable precedence

Hi all,

I have a question regarding role default variables. First, this is my use case: I want to develop reusable, redistributable and configurable roles supporting different operating systems. Some variables differ on the different operating systems. In addition to the defaults/main.yml file that is always automatically loaded, I would like to have a variable file for each operating system, something like rhel.yml, debian.yml etc. As the roles should be redistributable, it should be easy to override these variables, e.g. in the inventory or the playbook.

I really like the approach mentioned in the Ansible Best Practices here: http://docs.ansible.com/ansible/playbooks_best_practices.html#operating-system-and-distribution-variance, but it’s not on role level. If I would use include_vars, it would be very hard to overwrite those role default variables. So the question is

Is there any built-in mechanism to load more files than the main.yml from a role defaults/ folder which keeps the variable precedence? And if yes, is it possible to load them dynamically based on the operating system?

Of course, I’m open to other ideas how to satisfy the use case and keep roles portable, reusable and configurable without always having to modify them.

Thanks in advance,
Patrick

Meanwhile, I found this proposal dealing with this topic: https://github.com/ansible/proposals/pull/21/commits

It contains an interesting workaround using set_fact. More ideas are welcomed.

Have you thought of using tags to choose an operating system? Tag the roles with something like rhel and then execute a playbook using --tags=rhel

You can use tags on includes as well which comes in handy.

I’ve used tags to skip items that I didn’t want to run as well with --skip-tags=tagname

The only way to do this currently—while still preserving the ability for playbooks to override OS/environment-specific variables—is to use a little bit of a hack as is shown here: https://github.com/geerlingguy/ansible-role-apache/blob/master/tasks/main.yml#L1-L9

The goal of the proposal (https://github.com/ansible/proposals/pull/21/commits) would be to make it so you don’t have to add the extra set_fact task, nor use the ‘__varname’ convention to indicate a variable that can be overridden.

I’m hoping this kind of thing could be supported in core Ansible someday, but for now, I’ve been using the hack/workaround for some time, and it’s a passable solution.

-Jeff