vars_files precedence unexpected behavior

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

file: site.yml

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

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.

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.

But it’s the other way,

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

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.

Sure, better a repo 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 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 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?)

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

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.

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?

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.

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”

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 :slight_smile:

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 :slight_smile:

Yes I know but, it “almost” works :slight_smile: