roles/vars/main.yml - but what about other files in this directory

I’m using roles for template building, and would like to understand the use the of the vars directory a bit more. From the docs, I see that ansible will automatically pick up roles/{role}/vars/main.yml. But let’s say that I want to put multiple var files in the vars directory. Is there a way to tell ansible to pickup not only main.yml, but every other *.yml file in that directory as well?

Thank you!

Could you try this for now

in main.yml
var_files:

  • “…/var1.yml”

Also i guess a feature request has been made for automatic inclusion of all vars file, so down the line this should work without manual inclusion.

  • Benno

Yep, this was already requested yesterday:

https://github.com/ansible/ansible/issues/2958

Thank you, but this does not seem to be working. Are you sure it’s “var_files” and not “vars_files”? I’ve tried a number of options, with and without quotes around the filename, but no luck.

I do appreciate your help, thank you again!

Actually i meant vars_files: but i guess even that doesn’t seem to work in vars/main.yml, so i guess the only option might be to include it in your main playbook. but i will wait for others to also comment.

Thanks,
Benno

Hi Benno,

Thank you for the clarification. I was thinking (hoping) that “vars_files” would work inside main.yml.

Do you know when ansible will support reading all .yml files from within the roles/…/vars directory?

Thank you!
– Jeremy

Variable files can’t do includes, no.

vars_files is the way to go.

Hi Michael,

Has this ever been implemented ?

With Vault especially, it seems like a sensible pattern to have a separate encrypted file in vars/ for credentials, next to main.yml

Having to specify include_vars or vars_files explicitly each time feels a little clumsy : shouldn’t one reasonably expect a directory named “vars” to have some magic meaning and consider all of its contents automatically (like group_vars and inventory directories do) ?

Since vars/ also doesn’t serve as a default path for include_vars in role tasks (AFAIK, you have to explicitly say “…/vars/xxx.yml”), technically it might as well be just “roles/x/vars.yml”, right ?

“Has this ever been implemented?”

Vault yes, the feature about multiple files in vars/ automatically being loaded? No.

Files in vars/ are not all automatically loaded - it would absolutely break certain classes of playbooks, so we can’t/won’t go there.

include_vars is a good solution from the task file for specific files.