# How to use include\_vars  with "name=" in playbook?

**URL:** <https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960>\
**Category:** Ansible Project\
**Created:** [November 4, 2016, 12:57pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960 "2016-11-04T12:57:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LXC-dev\_Koha-Lappi](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@LXC-dev\_Koha-Lappi](https://forum.ansible.com/u/LXC-dev_Koha-Lappi)\
**Post date:** [November 4, 2016, 12:57pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/1 "2016-11-04T12:57:33Z")

</div>

Hi Ansible-project,

I’m trying to use “include\_vars” in my playbook, but have some problems understanding how should it be used.

Here is the relevant part of the playbook in role “myrole” tasks:

(partial) file: roles/myrole/tasks/main.yaml

- include\_vars:  
file=joe.yaml  
name=user1
- debug: var=user1

file: roles/myrole/vars/joe.yam  
—` `profile:` `name: joe` `````type: user`  
```shared: “{{ common }}”`  
``skills:

- item1
- item2  
common:
- my1  
`
- my2

` `But when I run my playbook against tow hosts (host1 and host2) the user1 var is always undefined while I expect it to be set to the content of joe.yaml.

Also if I add any debugs to main.yaml after the include\_vars task:

`

- debug: var=user1.profile
- debug. var=user1.skills
- debug: var=profile
- debug: var=skills
- debug: var=common

`

they all fail with similar reports like here below:

`  
TASK [myrolew : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1] =\> {  
“user1”: “VARIABLE IS NOT DEFINED!”  
}  
ok: [host2] =\> {  
“user1”: “VARIABLE IS NOT DEFINED!”  
}

`

Why does it not work, specifically the “name=” part seems to be not doing what it should?

What is the name-space to which the include\_vars “creates” that var when the “name=” option is used?

What am I doing wrong?

- jukka (Koha-developer)

---

<div class="post-metadata">

**Author:** ![LXC-dev\_Koha-Lappi](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@LXC-dev\_Koha-Lappi](https://forum.ansible.com/u/LXC-dev_Koha-Lappi)\
**Post date:** [November 4, 2016, 2:34pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/2 "2016-11-04T14:34:30Z")

</div>

Partially answering to my own question, it seems that the correct form for “include\_vars” stanza in myrole/tasks/main.yaml shoud be:

`

- include\_vars: file=“joe.yaml” name=“user1”  
`

but that still does not solve the problem of the var being undefined (but now only in the other host, which is very strange indeed)…

What could be the problem (I’m on Ansible-2.2.0.0) and are you able to repeat this issue in your own ansible/play-books?

-jukka

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 4, 2016, 2:50pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/3 "2016-11-04T14:50:57Z")

</div>

I cannot reproduce:

- hosts: locals  
gather\_facts: false  
tasks:
- include\_vars: file=testvars.yml name=testing
- debug: var=testing

# testvars.yml

var1:  
test1: val1  
test2: val2

output:  
ok: [local3] =\> {  
“testing”: {  
“var1”: {  
“test1”: “val1”,  
“test2”: “val2”  
}  
}  
}  
ok: [local4] =\> {  
“testing”: {  
“var1”: {  
“test1”: “val1”,  
“test2”: “val2”  
}  
}  
}  
ok: [local  
​1​  
] =\> {  
“testing”: {  
“var1”: {  
“test1”: “val1”,  
“test2”: “val2”  
}  
}  
}  
ok: [local2] =\> {  
“testing”: {  
“var1”: {  
“test1”: “val1”,  
“test2”: “val2”  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 4, 2016, 3:06pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/4 "2016-11-04T15:06:00Z")

</div>

My guess is that you are "getting variable is not defined" because common is not defined.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 4, 2016, 3:30pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/5 "2016-11-04T15:30:04Z")

</div>

none of these should work since you are using name= to create them all under the user1 variable

- debug: var=profile
- debug: var=skills
- debug: var=common

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [November 4, 2016, 3:35pm UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/6 "2016-11-04T15:35:14Z")

</div>

Kai Stian Olstad is right. If you remove this part:  
shared: "{{ common }}"  
the user1 variable won't be undefined.

Cheers,  
Marko

---

<div class="post-metadata">

**Author:** ![LXC-dev\_Koha-Lappi](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@LXC-dev\_Koha-Lappi](https://forum.ansible.com/u/LXC-dev_Koha-Lappi)\
**Post date:** [November 6, 2016, 2:04am UTC](https://forum.ansible.com/t/how-to-use-include-vars-with-name-in-playbook/22960/7 "2016-11-06T02:04:24Z")

</div>

Hi all,

thanks for your replies, based on those I investigated this more and found out that it is in fact the “{{ comman }}” variable reference in the included file that caused this problem.

I was under the impression that the variable defs in the included files could reference other vars in them but this is not the case.

They all have to be defined elsewhere and the after they have been included can be referenced and used in the playbooks.

There were also some syntax errrors in my playbooks and the variable “common” had been defined in the other host group/role files but not for the other. That’s why after correcting the include vars stanza the other host still reported the whole include vars to caseu “undefined error”

Also, this was very difficult to debug cause even the -vvvv verbosity clauses in the playbook run did not reveal which part in the included vars file has the undefined var or syntax error or anything. It only passed thru these tasks but the variables (all an/or for one host) remained undefined.

Once again, thanks for your comments, they were very helpful for me to sort this out, now my playbook works as excpected and I’m just testing what is the best way to include sevaral files (actually individual LXC-definitions) to be added as a list or hash to a collection var e.g. lxchosts.lxc1 or lxchosts[‘lxc1’]… presumably like:

`

- name: Load LXCs

include\_vars: “lxc-{{ item }}.yaml” name=“lxchosts.{{ item }}”

with\_items:lxcnames

`

Cheers for now,

-jukka
