Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

That's the part:

  - 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
up vote 21 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/foo.yml", "vars/bar.yml", "vars/default.yml" ]
share|improve this answer
Seems like this should be the accepted answer... – Manuel Meurer Jul 3 '14 at 13:32
According to Ansible developers, this solution will load all of the files, not just the first one found. – tjanez Nov 24 '14 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 '14 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 '14 at 9:00
except that include_vars in the task will give a high precedence of variables compared to role defaults or vars – Alex F Mar 21 at 8:51

Your Answer


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.