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?
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: