Use multiple var files in ansible role

Viewed 17657

One of my roles has two different variable types. One is public (things like package versions and other benign information). These can be committed to SCM without a worry. It also requires some private information (such as API keys and other secret information). I'm using ansible-vault to encrypt secret information. My solution was to have vars/main.yaml for pulic, and vars/vault.yml for the encrypted private information.

I came across a problem and am uncertain what's the best practice or actual solution here. It seems that ansible only loads the vars/main.yml file. Naturally I do not want to encrypt the public information so I looked for solution. So far the only solution I came up with (suggested on IRC) is to create group_vars/all/vault.yml and prefix all variables with the role name. This works because ansible seems to recursively load everything under group_vars. This does work but seems organizationally incorrect because the variables are for a specific role and not "globally universally true". I also tried to put include: vars/vault.yml into vars/main.yml but that did not work.

Is there a proper way to do this?

3 Answers

In case anyone is still trying to do that, instead of having the following structure:

vars/main.yml
vars/vault.yml

which won't work like you saw, you can instead organise your role like this:

vars/main/vars.yml
vars/main/vault.yml

Every vars file in the 'main' dir will be loaded by your role and you can encrypt your 'vault.yml' file only.

Related