Parameterize an array for vars_files, can't seem to template i.e. lookup/split

I’d like to read in a directory full of var files without having to specify individual files.

The below combination of lookup and split is valid to define a variable but appears not to be supported with vars_files with “ERROR! vars file { was not found on the Ansible Controller.”

vars_files: “{{ lookup(‘fileglob’, ‘mydir/vars/*.yml’).split(‘,’)}}”

I see in manager.py that the template is applied only individually per vars_files item and not at the top level so it’s not possible to assign a list structure directly to vars_files.

https://github.com/maxamillion/ansible/blob/stable-2.6/lib/ansible/vars/manager.py

Any other alternatives to parameterize reading in multiple var files without having to specify the individual files would be greatly appreciated.

This with a loop
https://docs.ansible.com/ansible/latest/modules/include_vars_module.html

Thank you for a quick response Kai. For various reasons, we require the variables early on before the tasks stage and can’t take advantage of the wonderfully flexible include_vars.

Even before pre_tasks that is run before roles?

Hi, I’ll answer this question:
Yes, we need it for the hosts definition:

hosts: “{{ some_var_from_some_of_the_var_files }}”

Hi all

I have the very same issue, trying to include a pattern of files. I was also expecting the following to work

vars_files: "{{ query('fileglob', 'mydir/vars/want_these_*.yml')}}"

but I am getting the same error. Did you manage to find a workaround?

For me the include_vars module is not an option, since I also need to evaluate the “{{…}}” inside the included files. Unfortunately, there is no import_vars module…
I will be also grateful for any tips. My ultimate goal is load variables from a bunch of files using glob or regex pattern, and evaluate {{}} inside them.