6

I know it's possible to include tasks / handlers files in other similar files via lines like the following

- name: Configure django
  include: django.yml
- name: Configure nginx reverse proxy
  include: nginx.yml

But according to the documentation, it doesn't seem as if this functionality exists for files under vars/ or defaults/. The problem I'm facing is that I have a lot of variables which modify fundamentally different aspects of a role, but all of the variables are just divided into different sections within the same file (defaults/main.yml) using comments (e.g. # BEGIN RoR config vars ... # END RoR config vars ).

It's very ugly and unwieldy, but more to the point it's not modular, and it makes maintaining the thing that much more difficult.

Edit: Since someone voted to close because they felt this was unclear...

This is akin to what I have (all in one file):

## Database variables

database:
  user: bob
  pass: bobs_pass
  host: dbhost

## Server variable

server:
  su_user: nobody
  max_connections: 50

Giving ls -R output of:

./defaults/main.yml

And this is what I'd prefer to have:

- name: Include database vars
  include: database.yml
- name: Include app server vars
  include: server.yml

With ls -R output:

./defaults/main.yml
./defaults/database.yml
./defaults/server.yml

1 Answer 1

8

Why not? I do this all the time in roles/whatever/tasks/main.yml with include_vars:

- include_vars: whatever_os_{{ ansible_distribution }}_{{ ansible_distribution_major_version }}.yml

The variables are loaded from the vars directory of the role. In a playbook the path is relative to the playbook, or you can use an absolute path.

In the case of the above, I then have OS-specific variables in these files:

$ ls -l roles/whatever/vars
total 48
lrwxrwxrwx. 1 error error  24 Feb  2 21:56 whatever_os_CentOS_5.yml -> whatever_os_RedHat_5.yml
lrwxrwxrwx. 1 error error  24 Feb  2 21:56 whatever_os_CentOS_6.yml -> whatever_os_RedHat_6.yml
lrwxrwxrwx. 1 error error  24 Feb  2 21:56 whatever_os_CentOS_7.yml -> whatever_os_RedHat_7.yml
-rw-rw-r--. 1 error error  81 May  9 23:17 whatever_os_Debian_6.yml
-rw-rw-r--. 1 error error  81 May  9 23:17 whatever_os_Debian_7.yml
-rw-rw-r--. 1 error error  81 May  9 23:17 whatever_os_Debian_8.yml
-rw-r--r--. 1 error error 173 Apr 28 14:59 whatever_os_Fedora_20.yml
-rw-r--r--. 1 error error 173 Mar 22 01:51 whatever_os_Fedora_21.yml
-rw-r--r--. 1 error error 173 May  9 23:18 whatever_os_Fedora_22.yml
-rw-r--r--. 1 error error 188 Mar 22 01:51 whatever_os_RedHat_5.yml
-rw-r--r--. 1 error error 191 Mar 22 01:51 whatever_os_RedHat_6.yml
-rw-r--r--. 1 error error 189 Mar 22 01:51 whatever_os_RedHat_7.yml
-rw-rw-r--. 1 error error  81 May  9 23:17 whatever_os_Ubuntu_10.yml
-rw-rw-r--. 1 error error  81 May  9 23:17 whatever_os_Ubuntu_12.yml
-rw-rw-r--. 1 error error  81 May  9 23:17 whatever_os_Ubuntu_14.yml
2
  • So the easiest way to do it is from a tasks file? I'm upvoting, because this is pertinent and helpful. I just wondered if there were another way. Partly because it doesn't really work for defaults- if you include a defaults file for vars within a task, won't that override other, more specific vars? Commented Jun 8, 2015 at 21:31
  • 1
    Defaults have the lowest priority, so I find them best for things that are only rarely overridden. Things like "the MySQL port is 3306". I don't often use defaults for anything, in fact, unless I find myself repeating a variable; then it's useful to DRY it up. Commented Jun 8, 2015 at 21:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .