# getting a variable indirectly

**URL:** <https://forum.ansible.com/t/getting-a-variable-indirectly/29863>\
**Category:** Ansible Project\
**Created:** [March 11, 2019, 8:05pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863 "2019-03-11T20:05:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 11, 2019, 8:05pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/1 "2019-03-11T20:05:11Z")

</div>

Hi all, I need to get the value of a variable whose name is saved in another one, in other words I need the lookup plugin vars  
Unfortunately I can’t upgrade ansible to a release upper to 2.4, so I have to reinvent the wheel.. unpleasant feeling.

Is there a way to implement it in another way?

I tried like so

# when: lookup(‘vars’, item|basename, default=false)

when: confobj  
vars:  
confobj: “{% set output = globals()[item|basename] if globals()[item|basename] is defined else false %}  
{{ output }}”

But the variable is not defined in the global namespace..

Actually the problem is a little more simple.. the variable could be set in {host,group}\_vars or in roles/\*/{default,vars}

For the former case I could get it thanks the available var plugin with host\_vars[inventory\_hostname][item|basename]  
but how to retrieve those variables set by roles stuff?

regards

Luca

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 12, 2019, 1:54am UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/2 "2019-03-12T01:54:51Z")

</div>

sorry for the mess, but the previous replies was wrong..

it’s important to keep no space between different lines:

- hosts: servers  
#gather\_facts: false  
roles:
- role: myrole1  
vars:  
confobjs: “{% set objs = {} %}  
{% if confobj1 is defined %}  
{% set objs1 = objs|combine({‘confobj1’: confobj1}) %}  
{% set objs = objs1 %}  
{% endif %}  
{% if confobjn is defined %}  
{% set objs1 = objs|combine({‘confobjn’: confobjn}) %}  
{% set objs = objs1 %}  
{% endif %}  
{{ objs }}”

in the task:

#not yet working in ansible release 2.3.2

# when: lookup(‘vars’, item|basename, default=false)

#workaround see role variable confobjs in the playbook launcher  
when: confobjs[item|basename] is defined  
register: template\_result

it seems to work ugly well

---

<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:** [March 12, 2019, 6:10am UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/3 "2019-03-12T06:10:07Z")

</div>

I'm not sure I understand what you are trying to achieve, but I think you are looking for this

hostvars[inventory\_hostname][item|basename] is defined

It's not recommended to use vars, but this also works

vars[inventory\_hostname][item|basename] is defined

If you want to combine variable with a string the syntax is

hostvars[inventory\_hostname][myvar ~ 'my\_string'] is defined

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 12, 2019, 1:41pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/4 "2019-03-12T13:41:12Z")

</div>

Hi Kai, in my current release 2.3.2.0 the hash vars is undefined as well as the lookup plugin vars.

What’s more the hostvars dictionary collects only the inventories variables (e.g. those defined in the files {host,group}\_var/_)  
Whereas if the variables related to a role (e.g. those defined in the roles/_/{default,vars}/main.yml) are not gathered by the hash hostvars

The only way I found to workaround the missing vars plugin is to build the dictionary by myself, item by item  
Is there a way to retrieve the variable name string given a variable? I’d like to build the dictionary with a loop at least.

thanks for your support

Luca

---

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [March 15, 2019, 5:07am UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/5 "2019-03-15T05:07:36Z")

</div>

i’m not sure if it would work on that old version, but couldn’t you just copy the [vars plugin](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/vars.py) file into your local “lookup\_plugins” directory?

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 16, 2019, 12:37am UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/6 "2019-03-16T00:37:35Z")

</div>

interesting, but a bit scary, I’ll give it a go at home…  
I’ll setup the old gears on my upgraded host with pyp and virtualenv, or better I hope to manage to setup the environment 😃

---

<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:** [March 16, 2019, 6:46am UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/7 "2019-03-16T06:46:03Z")

</div>

I have been using vars for a long time so it's strange it don't work for you,  
so I did a test in my Ansible 2.3.2 test environment and it work great for me.

$ find roles/test/  
roles/test/  
roles/test/defaults  
roles/test/defaults/main.yml

roles/test/default/main.yml

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 17, 2019, 5:45pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/8 "2019-03-17T17:45:57Z")

</div>

I managed to install the release 2.3.2.0 with virtualenv on my pc, easier than expected…

@kai  
I confirm that the variable vars is defined and it works as described at least on my host.

I think I tested it in a wrong way (maybe trying to access vars[inventory\_hostname][item|basename], but I’m not sure…), I’ll double check on Monday

By the way I didn’t find the variable description in the documentation pages.

@Adam E

I also tried to use the lookup plugin vars with ansible 2.3.2  
I copied the plugin code /usr/lib/python2.7/dist-packages/ansible/plugins/lookup/vars.py in a test directory test/lookup\_plugins/vars.py  
I regenarate the bytecode (useless)

(v2.3.2) $ python -m py\_compile vars.py  
and tested it getting the following failure:

task path: /home/fusillator/Code/ansible/test/role.yml:8  
The full traceback is:  
Traceback (most recent call last):  
File “/home/fusillator/Code/ansible/v2.3.2/local/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 89, in run  
items = self.\_get\_loop\_items()  
File “/home/fusillator/Code/ansible/v2.3.2/local/lib/python2.7/site-packages/ansible/executor/task\_executor.py”, line 214, in \_get\_loop\_items  
items = mylookup.run(terms=loop\_terms, variables=self.\_job\_vars, wantlist=True)  
File “/home/fusillator/Code/ansible/test/lookup\_plugins/vars.py”, line 72, in run  
self.set\_options(direct=kwargs)  
AttributeError: ‘LookupModule’ object has no attribute ‘set\_options’

I think the code is not backward compatible with 2.3.2, and I’m a python newbe (not enough devotee) to further investigate on the issue.

thanks a lot for the support guys. You rock.

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 17, 2019, 6:33pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/9 "2019-03-17T18:33:50Z")

</div>

for the sake of completeness here’s the playbook and the error message:

(v2.3.2) $ cat role.yml

- hosts: localhost  
gather\_facts: false  
roles:
- role: myrole  
tasks:
- debug:  
var: vars[‘mydefaultvar’]
- debug:  
var: myvar  
vars:  
myvar: “{{ lookup(‘vars’, ‘mydefaultvar’) }}”  
(v2.3.2) $ ansible-playbook role.yml -vvv  
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: role.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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*\*  
task path: /home/fusillator/Code/ansible/test/role.yml:8  
fatal: [localhost]: FAILED! =\> {  
“failed”: true,  
“msg”: “An unhandled exception occurred while running the lookup plugin ‘vars’. Error was a \<type ‘exceptions.AttributeError’\>, original message: ‘LookupModule’ object has no attribute ‘set\_options’”  
}  
to retry, use: --limit @/home/fusillator/Code/ansible/test/role.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=1 changed=0 unreachable=0 failed=1

regards

Luca

---

<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:** [March 17, 2019, 6:46pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/10 "2019-03-17T18:46:44Z")

</div>

As I mentioned previously, using vars in not recommended and I let Brian Coca explain why  
[https://groups.google.com/d/msg/ansible-project/ZplFYotOfj0/x\_g8WEAOCQAJ](https://groups.google.com/d/msg/ansible-project/ZplFYotOfj0/x_g8WEAOCQAJ)

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [March 18, 2019, 11:49pm UTC](https://forum.ansible.com/t/getting-a-variable-indirectly/29863/11 "2019-03-18T23:49:56Z")

</div>

thanks for the useful reference. So I think I’ll keep the snipped code to prevent error in case of upgrade.  
and I’ll do a new branch in order to use the var plugin, new loops and the other new features to be ready for release 2.7/2.8

thanks again for your help
