Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

That's the part:

vars_files:
  - vars/vars.default.yml
  - vars/vars.yml

If a file vars/vars.yml does not exist - here is an error.

ERROR: file could not read: /.../vars/vars.yml

How can I load additional variables from this file only if it exists? (with no errors)

share|improve this question

2 Answers 2

up vote 9 down vote accepted

It's quite simple really. You can squash your different vars_files items into a single tuple and Ansible will automatically go through each one until it finds a file that exists and load it. E.x.:

vars_files:
  - [ "vars/foo.yml", "vars/bar.yml", "vars/default.yml" ]
share|improve this answer
    
Seems like this should be the accepted answer... –  Manuel Meurer Jul 3 at 13:32
    
According to Ansible developers, this solution will load all of the files, not just the first one found. –  tjanez Nov 24 at 12:13

According to Ansible developers, the proper way to solve this is to use something like:

vars_files_var: ['../path/to/file1', '../path/to/file2', ...]

- include_vars: {{item}}
  with_first_found: vars_files_var

Furthermore, they say:

The above will properly load only the first file found, and is more flexible than trying to do this via the vars_files language keyword.

share|improve this answer
    
"only the first file found" - the idea was to redefine some variables, not all of them –  Sergey Nov 24 at 17:44
    
@Sergey, reading again your question, I see that what you wanted is a bit different. Thanks for pointing this out. I'll leave the answer as is if someone else finds it useful. –  tjanez Nov 25 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.