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.
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?
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.
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
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.
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.
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 ************************************************************************************************************************************************************************************************************************************************************
1 plays in role.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************
META: ran handlers
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