# vars\_files precedence unexpected behavior

**URL:** <https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222>\
**Category:** Ansible Project\
**Created:** [July 30, 2013, 8:09pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222 "2013-07-30T20:09:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [July 30, 2013, 8:09pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/1 "2013-07-30T20:09:53Z")

</div>

Suposse I’m using the recommended file structure and I want to import external vars\_files at the top level:

# file: site.yml

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 30, 2013, 8:53pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/2 "2013-07-30T20:53:20Z")

</div>

Can you elaborate what " The odd thing is that “/etc/external.yml” is not." means?

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [July 30, 2013, 9:40pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/3 "2013-07-30T21:40:48Z")

</div>

It’s not available for use at the time webservers.yml is executed.

It seems that loading a vars\_files that depends on a “host\_var” is loaded before any of the other playbooks inside the main site.yml playbook and a “vars\_file” that not depends on a “host\_var” like “/etc/external.yml” it’s not available for the sub-playbooks inside site.yml

I noticed that if I move the vars\_files section inside webservers.yml it works but I like the idea of having globally defined vars\_files at site.yml level so any subplaybook there can make use of it.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 30, 2013, 10:59pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/4 "2013-07-30T22:59:38Z")

</div>

What happens is vars without facts in them are loaded globally, variables that do not contain facts are loaded earlier, because they don’t have to be per-host.

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [July 30, 2013, 11:17pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/5 "2013-07-30T23:17:39Z")

</div>

But it’s the other way,

/etc/{{ foo }}.yml is being loaded earlier  
/etc/external.yml is being loaded only in the subplaybook

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 30, 2013, 11:38pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/6 "2013-07-30T23:38:24Z")

</div>

I’m very very skeptical.

If you can put a minimal playbook up on gist somewhere that reproduces this, perhaps we can take a look.

While I note what you have above, copy and pasted partial playbooks are a bit hard to repro.

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [July 31, 2013, 2:46pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/7 "2013-07-31T14:46:41Z")

</div>

Sure, better a repo [https://github.com/victorcoder/ext\_vars](https://github.com/victorcoder/ext_vars)

Just clone and run: ansible-playbook site.yml (runs in local)

The first debug message [https://github.com/victorcoder/ext\_vars/blob/master/webservers.yml#L6](https://github.com/victorcoder/ext_vars/blob/master/webservers.yml#L6) outputs ‘One’  
The second can’t find the var.

Notice (my ignorance): If this declaration [https://github.com/victorcoder/ext\_vars/blob/master/site.yml#L10](https://github.com/victorcoder/ext_vars/blob/master/site.yml#L10) is moved on top of the file, it can’t find any var.

Notice2: one\_var = ‘One var’ (only One is printed in the debug output, why?)

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [August 20, 2013, 8:22am UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/8 "2013-08-20T08:22:52Z")

</div>

I’m also finding strange things with this and roles with\_items too, could you check?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [August 20, 2013, 3:34pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/9 "2013-08-20T15:34:14Z")

</div>

Victor, have you tried this with the new role dependencies support (available in the development branch of the git repository)? With role dependencies, vars files for each role are scoped locally as well as globally, so there is no longer any concern that another role may override the variable set in a vars file. This may still happen at the top level task scope, since the vars files are still loaded globally, however roles should now see a consistent set of variables.

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 9:03am UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/10 "2013-09-18T09:03:57Z")

</div>

Hi James,

sorry to bring this issue up again but this issue is stopping me.

To simplify:

Given {{ env }} is inventory var and “{{ env }}.yml” loads foo: something

vars\_files:

- {{ env }}.yml

roles:

- {role: x, bar: ‘{{ foo }}’}

Variable “foo” is not available to the role through variable “bar” (not defined)

How can I use a var defined in a dynamic vars\_files in a role?

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 9:15am UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/11 "2013-09-18T09:15:48Z")

</div>

Sorry,

The example is not correct, I mean that with\_items are not available.

Given {{ env }} is inventory var and “{{ env }}.yml” loads foo: {item1: ‘something’}

vars\_files:

- {{ env }}.yml

roles:

- {role: x, bar: ‘{{ item.item1 }}’, with\_items: foo }

Then foo is undefined.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 18, 2013, 12:13pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/12 "2013-09-18T12:13:37Z")

</div>

This is invalid syntax because you started an entry with a"{", which means YAML thinks you are starting a hash and therefore you should quote the line.

vars\_files:

- {{ env }}.yml

Should be:

vars\_files:

- “{{ env }}.yml”

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 18, 2013, 12:14pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/13 "2013-09-18T12:14:14Z")

</div>

with\_items is not really supported for roles either.

You should always loop _inside_ the task.

We’ve been over this many times before on the list 🙂

---

<div class="post-metadata">

**Author:** ![Victor\_Castell](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@Victor\_Castell](https://forum.ansible.com/u/Victor_Castell)\
**Post date:** [September 18, 2013, 3:02pm UTC](https://forum.ansible.com/t/vars-files-precedence-unexpected-behavior/13222/14 "2013-09-18T15:02:23Z")

</div>

Thanks Michael,

> This is invalid syntax because you started an entry with a"{", which means YAML thinks you are starting a hash and therefore you should quote the line.  
> vars\_files:
> 
> - {{ env }}.yml  
> Should be:  
> vars\_files:
> - “{{ env }}.yml”

{{ env }}.yml was an example, the actual playbook is something like: …/some/path/{{ env }}.yml

Therefore valid syntax.

> with\_items is not really supported for roles either.  
> You should always loop _inside_ the task.  
> We’ve been over this many times before on the list 🙂

Yes I know but, it “almost” works 🙂
