vars_files: expand list of files via a single variable

So I just recently discovered how to list several possible vars_files, with precedence, and where some may not exist:

http://serverfault.com/questions/589734/ansible-can-i-use-vars-files-when-some-files-do-not-exist

Very handy. Where is the documentation for this?

Next question…

I’d like to centralise a list of possible vars_files in a single variable but can’t get it to work. E.g:

`

vars_files:

  • [ “{{ settings_files }}” ]

`

and then in say group_vars/all I have my files defined:

`

settings_files: ‘“…/settings.yml”, “…/example.settings.yml”’

`

But that doesn’t work - I get the following error:

`

vlad: not importing file: /Volumes/Data/Projects/vlad/vlad/vlad/playbooks/“…/settings.yml”, “…/example.settings.yml”
ERROR: vlad: FATAL, no files matched for vars_files import sequence: [u’/Volumes/Data/Projects/vlad/vlad/vlad/playbooks/“…/settings.yml”, “…/example.settings.yml”']

`

It looks like something is happening on expansion that I wasn’t expecting.

I’m hoping this is just a question of syntax and I’ve messed around with a few variations but am still having no luck.

Can anyone help with this? Is it even possible?

I’m running Ansible v1.7.1.

What were you expecting?

It’s saying none of the files in the list were found and hitting an error condition, but I’m unclear which parts you find unusual.

Thanks!

Michael DeHaan <michael@ansible.com> napisał:

What were you expecting?

It's saying none of the files in the list were found and hitting an
error
condition, but I'm unclear which parts you find unusual.

The path in the error looks strange :wink:

So I just recently discovered how to list several possible

vars_files,

with precedence, and where some may not exist:

http://serverfault.com/questions/589734/ansible-can-i-use-vars-files-when-some-files-do-not-exist

Very handy. Where is the documentation for this?

Next question...

I'd like to centralise a list of possible vars_files in a single

variable

but can't get it to work. E.g:

vars_files:
  - [ "{{ settings_files }}" ]

and then in say group_vars/all I have my files defined:

settings_files: '"../settings.yml", "../example.settings.yml"'

I think you want to create a list here, like this:

settings_files:
- ../settings.yml
- ../example.settings.yml

And then use it like this:

vars_files:
   - "{{ settings_files }}"

As Tomasz says below, the path in the error looks strange:

`
[u’/Volumes/Data/Projects/vlad/vlad/vlad/playbooks/“…/settings.yml”, “…/example.settings.yml”']

`

…especially when compared to the paths stored in my settings_files variable:

`
settings_files: ‘“…/settings.yml”, “…/example.settings.yml”’

`

So you know, the “/Volumes/Data/Projects/vlad/vlad/vlad/playbooks/” part that’s being prepended is the path to the playbook that’s calling vars_files.

Thanks for the suggestion but I already tried that and it results in a huge error:

`

Traceback (most recent call last):
File “/usr/local/Cellar/ansible/1.7.1/libexec/bin/ansible-playbook”, line 5, in
pkg_resources.run_script(‘ansible==1.7.1’, ‘ansible-playbook’)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py”, line 489, in run_script
self.require(requires)[0].run_script(script_name, ns)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py”, line 1207, in run_script
execfile(script_filename, namespace, namespace)
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/EGG-INFO/scripts/ansible-playbook”, line 309, in
sys.exit(main(sys.argv[1:]))
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/EGG-INFO/scripts/ansible-playbook”, line 249, in main
pb.run()
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/ansible/playbook/init.py”, line 339, in run
if not self._run_play(play):
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/ansible/playbook/init.py”, line 646, in _run_play
play.update_vars_files(all_hosts, vault_password=self.vault_password)
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/ansible/playbook/play.py”, line 698, in update_vars_files
self._update_vars_files_for_host(h, vault_password=vault_password)
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/ansible/playbook/play.py”, line 847, in _update_vars_files_for_host
filename2, filename3, filename4 = generate_filenames(host, inject, filename)
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/ansible/playbook/play.py”, line 775, in generate_filenames
filename4 = utils.path_dwim(self.basedir, filename3)
File “/usr/local/Cellar/ansible/1.7.1/lib/python2.7/site-packages/ansible-1.7.1-py2.7.egg/ansible/utils/init.py”, line 318, in path_dwim
if given.startswith(“/”):
AttributeError: ‘list’ object has no attribute ‘startswith’

`

I’m guessing that this is the most useful part of that error:

`
AttributeError: ‘list’ object has no attribute ‘startswith’

`

Out of curiosity, I wrapped each path string in the list with double quotes but it results in pretty much the same thing.

“settings_files: '”…/settings.yml", “…/example.settings.yml”’

It should be noted this is not a YAML list, and that is probably part of your problem.

The path being an array of unicode elements does not look strange to me. Is there a particular path element in the list that looks strange to you?

Sure. Once again for reference, here’s that path from the error message:

`
[u’/Volumes/Data/Projects/vlad/vlad/vlad/playbooks/“…/settings.yml”, “…/example.settings.yml”']

`

Disregarding the wrapping square brackets, the wrapping single quotes and the preceding “u” gives us:

`
/Volumes/Data/Projects/vlad/vlad/vlad/playbooks/“…/settings.yml”, “…/example.settings.yml”

`

Now look at where the first and second double quotes are. That looks funky to me.

Surely the first element in the array should be entirely wrapped in double quotes? Also, having ‘…/’ anywhere other than at the start of a path is surely going to cause problems.

It appears as if Ansible is prefixing an absolute path (the path to the playbook making the call: /Volumes/Data/Projects/vlad/vlad/vlad/playbooks/) to my expanded variable, irrespective of what my variable contains. This in turn is making the expanded variable a bit garbled, hence the error.

Yes, the quotes definitely look funky.

Thanks for bearing with me and my reading comprehension skills.

If this occurs on 1.7.1 or devel, can you file a bug on this one? This most definitely should not happen and we’ll want to correct this.

Thanks!

No worries :slight_smile:

This is with Ansible v1.7.1 so I’ll file the bug report and point back here for reference.

Bug report: https://github.com/ansible/ansible/issues/8881

Thanks!