How to use include_vars with "name=" in playbook?

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)

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

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”
}
}
}

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

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

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

Cheers,
Marko

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