Why no variable substitution in top level includes?

From http://docs.ansible.com/playbooks_roles.html#task-include-files-and-encouraging-reuse

Note that you cannot do variable substitution when including one playbook inside another.

I actually wanted to do this in my - setup-new-server.yml playbook:

  • include: client-dns-setup.yml host=new-server

But it looks this is impossible. Why?

P.S. It is possible to turn on pasting of code snippets in Google Groups settings.
P.P.S. Ansible was very awesome so far. =)

Variable substitution works, just not with inventory variables, which confuses people a lot.

Just to clarify - do you mean inventory variables in included files are immune? Because I am passing a hardcoded string as playbook parameter.

include gets evaluated before hosts are included and you cannot use a inventory variable (host dependant) in the include.

Think of it as a ‘preprocessing macro’ more than a task.

include gets evaluated before hosts are included and you cannot use a inventory variable (host dependant) in the include.

Think of it as a ‘preprocessing macro’ more than a task.

I still don’t get it. I have the file showdist.yml:


  • hosts: “{{ host | default(‘localhost’) }}”
    tasks:
  • name: show OS version info
    debug: msg=“{{ansible_distribution}} {{ansible_distribution_version}}”

Now I include it from main playbook.yml:


  • hosts: localhost
  • include: showdist.yml

It fails with:

PLAY [otherhost] **************************************************************
skipping: no hosts matched

But when I do this:


  • hosts: localhost
  • include: showdist.yml host=‘localhost’

It works. And I am surprised, because for more complicated file it didn’t work.

  • include: showdist.yml host=‘localhost’

You have no variables here, so this is not an inventory variable substitution issue.

But you would be setting a variable called “host” that has no special meaning for Ansible.

Minor pet-peeve/process note – when you say something doesn’t work, share what it did, and what it expected to do. I don’t know what “doesn’t work” means 80% of the time.

Let me try again:

playbook.yml - this file doesn’t change
`

The basics here are that task includes can pass parameters, playbook includes never had that implemented.

But both examples are playbook includes. Is it a bug?

Does this one means that 1.5.0 will have this implemented?
https://github.com/ansible/ansible/issues/5801

“But both examples are playbook includes. Is it a bug?”

Not really, there’s not an attach point for variables at that level.

-e sets global variables, task level include variables are a thing, etc.

Ok, I stand corrected, this is interjecting into the vars field.

I can’t say I understand the details of James’s patch as the dict constructor should be equivalent.

I still don't understand. There are different levels of variables, but
how do they stacked and overriden?

Is it like this? (I can't check right now)

[globals] (-e) - variable overrides everything below if set
[playbook] (vars section) - variable is accessible by includes
[task include] (no vars section) - uses variables from caller
[top include] (no vars section) - uses variables from caller
[top include] (vars section) - replaces all vars from levels above