Is it possible to / how would one include vars files from other vars files?

Parthian Shot asked:

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:


My answer:

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

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.