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

  user: bob
  pass: bobs_pass
  host: dbhost

## Server variable

  su_user: nobody
  max_connections: 50

Giving ls -R output of:


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:


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
  • 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? – Parthian Shot Jun 8 '15 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. – Michael Hampton Jun 8 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.