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

1,966 views
Skip to first unread message

LXC-dev - Koha-Lappi

unread,
Nov 4, 2016, 8:58:53 AM11/4/16
to Ansible Project
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)

LXC-dev - Koha-Lappi

unread,
Nov 4, 2016, 10:34:30 AM11/4/16
to Ansible Project
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

Brian Coca

unread,
Nov 4, 2016, 10:51:09 AM11/4/16
to ansible...@googlegroups.com
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"
       }
   }
}

----------
Brian Coca

Kai Stian Olstad

unread,
Nov 4, 2016, 11:06:35 AM11/4/16
to ansible...@googlegroups.com
On 04. nov. 2016 13:57, LXC-dev - Koha-Lappi wrote:
> 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.

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

--
Kai Stian Olstad

Brian Coca

unread,
Nov 4, 2016, 11:30:17 AM11/4/16
to ansible...@googlegroups.com
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


----------
Brian Coca

Stankovic, Marko

unread,
Nov 4, 2016, 12:48:37 PM11/4/16
to ansible...@googlegroups.com
Kai Stian Olstad is right. If you remove this part:
shared: "{{ common }}"
the user1 variable won't be undefined.

Cheers,
Marko

______________________________________________________________________________________________________

CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or
its subsidiaries and may contain proprietary, confidential or trade secret information.
This message is intended solely for the use of the addressee. If you are not the intended recipient
and have received this message in error, please delete this message from your system. Any unauthorized
reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

LXC-dev - Koha-Lappi

unread,
Nov 5, 2016, 10:04:24 PM11/5/16
to Ansible Project
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


On Friday, 4 November 2016 14:58:53 UTC+2, LXC-dev - Koha-Lappi wrote:
Reply all
Reply to author
Forward
0 new messages