Why no variable substitution in top level includes?

571 views
Skip to first unread message

anatoly techtonik

unread,
Feb 21, 2014, 2:47:54 PM2/21/14
to ansible...@googlegroups.com
 
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. =)

Brian Coca

unread,
Feb 21, 2014, 2:52:13 PM2/21/14
to ansible...@googlegroups.com
Variable substitution works, just not with inventory variables, which confuses people a lot.

anatoly techtonik

unread,
Feb 21, 2014, 2:58:02 PM2/21/14
to ansible...@googlegroups.com
On Friday, February 21, 2014 10:52:13 PM UTC+3, Brian Coca wrote:
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.

Brian Coca

unread,
Feb 21, 2014, 2:59:38 PM2/21/14
to ansible...@googlegroups.com
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.

anatoly techtonik

unread,
Feb 21, 2014, 3:22:41 PM2/21/14
to ansible...@googlegroups.com
On Friday, February 21, 2014 10:59:38 PM UTC+3, Brian Coca wrote:
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.

Michael DeHaan

unread,
Feb 21, 2014, 3:47:36 PM2/21/14
to ansible...@googlegroups.com
- 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.






--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

anatoly techtonik

unread,
Feb 23, 2014, 1:33:38 AM2/23/14
to ansible...@googlegroups.com
On Friday, February 21, 2014 11:47:36 PM UTC+3, Michael DeHaan wrote:
- 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
---
- hosts: localhost
- include: showdist.yml host='localhost'


showdist.yml - first version:
---
- hosts: "{{ host | default('otherhost') }}"
  tasks:
  - name: show OS version info
    debug
: msg="ping"

$  ansible-playbook -i hosts showdist.yml
PLAY [otherhost] ***
$  ansible-playbook -i hosts playbook.yml
PLAY [localhost] ***
PLAY [localhost] ***

Not that the last PLAY message is from showdist.yml included from playbook.yml
It run correctly against 'localhost'. For me - this works as expected
 
Now I add vars section in showdist.yml v2:
---
- hosts: "{{ host | default('otherhost') }}"
  vars:
    - irrelevant: "just for vars section"
  tasks:
  - debug: msg="ping"

And showdist.yml stops to run against localhost when included from
playbook.yml

$  ansible-playbook -i hosts showdist.yml
PLAY [otherhost] ***
$  ansible-playbook -i hosts playbook.yml
PLAY [localhost] ***
PLAY [otherhost] ***

Why?

Michael DeHaan

unread,
Feb 23, 2014, 8:54:02 AM2/23/14
to ansible...@googlegroups.com
The basics here are that task includes can pass parameters, playbook includes never had that implemented.







--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

anatoly techtonik

unread,
Feb 24, 2014, 12:06:32 AM2/24/14
to ansible...@googlegroups.com
On Sunday, February 23, 2014 4:54:02 PM UTC+3, Michael DeHaan wrote:
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?

anatoly techtonik

unread,
Feb 24, 2014, 12:10:06 AM2/24/14
to ansible...@googlegroups.com
Does this one means that 1.5.0 will have this implemented?
https://github.com/ansible/ansible/issues/5801

Michael DeHaan

unread,
Feb 24, 2014, 8:12:13 AM2/24/14
to ansible...@googlegroups.com
"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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Feb 24, 2014, 8:14:13 AM2/24/14
to ansible...@googlegroups.com
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.




anatoly techtonik

unread,
Feb 26, 2014, 11:49:52 AM2/26/14
to ansible...@googlegroups.com
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
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/WdvouAZ3BUM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAEVJ8QOLvV7JOdxdsT3L-w5eP9cKE_39VOxe0aNyNeBh0WM7nw%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.



--
anatoly t.
Reply all
Reply to author
Forward
0 new messages