# \-include: {{ var }} not working

**URL:** <https://forum.ansible.com/t/include-var-not-working/15972>\
**Category:** Ansible Project\
**Created:** [August 17, 2014, 9:54am UTC](https://forum.ansible.com/t/include-var-not-working/15972 "2014-08-17T09:54:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akos\_Vandra](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Akos\_Vandra](https://forum.ansible.com/u/Akos_Vandra)\
**Post date:** [August 17, 2014, 9:54am UTC](https://forum.ansible.com/t/include-var-not-working/15972/1 "2014-08-17T09:54:13Z")

</div>

Hello!

I am trying to include a file based on a variable name like so:

- do preparation stuff
- include: ‘{{ include\_before\_symlink }}’  
when: ‘{{ include\_before\_symlink != None }}’
- do symlinking

This is to be able to do a yield, similar to this:

- include: deploy\_revision  
vars:
- foo: bar
- include\_before\_symlink: roles/myrole/tasks/before\_symlink.yml

Which would execute the deploy\_revison task up to the point where it would start executing the before\_symlink task file, and then return and finish up the deploy\_revision task. Emphasis is that the caller should be able to provide what to do BEFORE the symlinkink.

The error I get is:

akovanm0:water-playbook avandra$ ansible-playbook --private-key=~/.vagrant.d/insecure\_private\_key -i .vagrant/provisioners/ansible/inventory/vagrant\_ansible\_inventory site.yml -v  
Traceback (most recent call last):  
File “/usr/local/Cellar/ansible/1.6.10/libexec/bin/ansible-playbook”, line 5, in   
pkg\_resources.run\_script(‘ansible==1.6.10’, ‘ansible-playbook’)  
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg\_resources.py”, line 489, in run\_script  
self.require(requires)[0].run\_script(script\_name, ns)  
File “/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg\_resources.py”, line 1207, in run\_script  
execfile(script\_filename, namespace, namespace)  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/EGG-INFO/scripts/ansible-playbook”, line 317, in   
sys.exit(main(sys.argv[1:]))  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/EGG-INFO/scripts/ansible-playbook”, line 257, in main  
pb.run()  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/ **init**.py”, line 289, in run  
play = Play(self, play\_ds, play\_basedir, vault\_password=self.vault\_password)  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 152, in **init**  
self.\_tasks = self.\_load\_tasks(self.\_ds.get(‘tasks’, ), load\_vars)  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 588, in \_load\_tasks  
loaded = self.\_load\_tasks(data, mv, default\_vars, included\_sudo\_vars, list(included\_additional\_conditions), original\_file=include\_filename, role\_name=new\_role)  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 588, in \_load\_tasks  
loaded = self.\_load\_tasks(data, mv, default\_vars, included\_sudo\_vars, list(included\_additional\_conditions), original\_file=include\_filename, role\_name=new\_role)  
File “/usr/local/Cellar/ansible/1.6.10/lib/python2.7/site-packages/ansible-1.6.10-py2.7.egg/ansible/playbook/play.py”, line 576, in \_load\_tasks  
(k,v) = t.split(“=”, 1)  
ValueError: need more than 1 value to unpack  
akovanm0:water-playbook avandra$

Thanks for your help,  
Akos vandra

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 17, 2014, 1:09pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/2 "2014-08-17T13:09:39Z")

</div>

If you see that traceback in 1.7.1 (the latest release) please do file a bug in GitHub so we can improve the split or make a better error message.  
Let us know!

As a sidenote, this syntax is not neccessary, so a quick primer on conditionals:

- include: ‘{{ include\_before\_symlink }}’  
when: ‘{{ include\_before\_symlink != None }}’

At a basic level, you can drop the Jinja2 brackets, as that will result in a string value that you don’t want:

- include: “{{ foo }}”  
when: include\_before\_symlink != None

OR (more simply, if you wish to only define it when it is to be used)

- include: “{{ foo }}”  
when: include\_before\_symlink is defined

OR (if you just want to reply on the true/false value)

- include: “{{ foo }}”  
when: include\_before\_symlink | default(False)

Defaults can also set in roles/rolename/defaults/main.yml, which make this even nicer:

- include: “{{ foo }}”  
when: include\_before\_symlink

---

<div class="post-metadata">

**Author:** ![Akos\_Vandra](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Akos\_Vandra](https://forum.ansible.com/u/Akos_Vandra)\
**Post date:** [August 17, 2014, 1:26pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/3 "2014-08-17T13:26:36Z")

</div>

Working on 1.7.1

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 18, 2014, 12:27pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/4 "2014-08-18T12:27:46Z")

</div>

Excellent! Thanks for testing!

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Ziegler](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@Wolfgang\_Ziegler](https://forum.ansible.com/u/Wolfgang_Ziegler)\
**Post date:** [September 4, 2014, 7:44pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/5 "2014-09-04T19:44:29Z")

</div>

Looks like this is about task include. I did something similar with a playbook include and it used to work in \<1.7. However, after updating to 1.7.1 it fails now (with the same error). Is the following playbook supposed to work?

`

- vars:
- playbook: “file”

include: “{{ playbook }}.yml”

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 5, 2014, 1:32am UTC](https://forum.ansible.com/t/include-var-not-working/15972/6 "2014-09-05T01:32:55Z")

</div>

No, it shouldn’t.

For one, the YAML is conceptually mixed up a bit:

- vars:
- playbook: “file”

include: “{{ playbook }}.yml”

Perhaps you meant a task include, but basically there’s a lot of made-up syntax here and I can’t make sense of it.

It may be easier to step back and ask what you are trying to do, maybe in a new thread, and we could help you model it.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Ziegler](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@Wolfgang\_Ziegler](https://forum.ansible.com/u/Wolfgang_Ziegler)\
**Post date:** [September 5, 2014, 9:37am UTC](https://forum.ansible.com/t/include-var-not-working/15972/7 "2014-09-05T09:37:44Z")

</div>

Yeah, I was thinking that might be the case so I posted my general of what I want to achieve at [https://groups.google.com/forum/?utm\_medium=email&utm\_source=footer#!topic/ansible-project/txSI-DE2-ts](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/ansible-project/txSI-DE2-ts).

> Perhaps you meant a task include, but basically there’s a lot of made-up syntax here and I can’t make sense of it.

I wanted to include a playbook based on the value of a variable which e.g. I could pass in via extra-vars. But as variables are at play-scope I tried defining it as above. Unfortunately, there is not much docs about playbook includes but the example in the docs is structured the same minus the vars part:

`

- include: load\_balancers.yml
- include: webservers.yml
- include: dbservers.yml

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 5, 2014, 7:12pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/8 "2014-09-05T19:12:55Z")

</div>

"I wanted to include a playbook based on the value of a variable which e.g. I could pass in via extra-vars. But as variables are at play-scope I tried defining it as above. "

Extra vars would be fine here.

Variables defined in a play can be used in task includes if passed to the include, but not to determine the filename.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Ziegler](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@Wolfgang\_Ziegler](https://forum.ansible.com/u/Wolfgang_Ziegler)\
**Post date:** [September 7, 2014, 9:23am UTC](https://forum.ansible.com/t/include-var-not-working/15972/9 "2014-09-07T09:23:43Z")

</div>

> Extra vars would be fine here.
> 
> Variables defined in a play can be used in task includes if passed to the include, but not to determine the filename.

I see - but is there a way to provide a default value for the extra-vars if it’s omitted? I’d dislike having the playbook require an extra-var to actually work at all. Having a vars\_prompt that works on playbook level would be useful here.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 7, 2014, 12:31pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/10 "2014-09-07T12:31:49Z")

</div>

“I see - but is there a way to provide a default value for the extra-vars if it’s omitted”

Yes, in your playbook where you use the variable, use this useful Jinja2 filter:

{{ variable\_name | default(‘my\_fault’) }}

“Having a vars\_prompt that works on playbook level would be useful here.”

While vars\_prompt appears nice, and it can be, it also reduces the ease at which a playbook can be run by other-automation or inserted into a script or continuous deployment workflow. As such, it’s better to avoid it if possible.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Ziegler](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@Wolfgang\_Ziegler](https://forum.ansible.com/u/Wolfgang_Ziegler)\
**Post date:** [September 19, 2014, 8:49am UTC](https://forum.ansible.com/t/include-var-not-working/15972/11 "2014-09-19T08:49:00Z")

</div>

Unfortunately, it looks like it’s not possible to use {{ variable\_name | default(‘my\_fault’) }} for playbook-level includes :-/

> “Having a vars\_prompt that works on playbook level would be useful here.”
> 
> While vars\_prompt appears nice, and it can be, it also reduces the ease at which a playbook can be run by other-automation or inserted into a script or continuous deployment workflow. As such, it’s better to avoid it if possible.

Yep, I’m aware of this - thanks. I just use for required variables which usually are passed via extra-vars. When the playbook is invoked manually, the prompt is nicer though than the extra vars syntax.

---

<div class="post-metadata">

**Author:** ![Rushyendra\_Bandaru](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@Rushyendra\_Bandaru](https://forum.ansible.com/u/Rushyendra_Bandaru)\
**Post date:** [November 17, 2015, 3:35pm UTC](https://forum.ansible.com/t/include-var-not-working/15972/12 "2015-11-17T15:35:37Z")

</div>

Hello All,

I’ve define one role/tasks/main.yml as below

- name: Include module variables

include\_vars: ‘{{ module }}.yml’

- include: prereq-{{ module }}-status.yml

I’ve defined each yml (prereq-cq-status.yml, prereq-dmgr-status.yml,prereq-ihs-status.yml) in same role/tasks

and files are mentioned in roles/vars (cq.yml,dmgr.yml,ihs.yml)

Note :module defined in inventory

got syntax error, ERROR: file could not read: (role/tasks/prereq-{{module}}-status.yml)

Can some one help me please.how to fix this
