getting a variable indirectly

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

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

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

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

i’m not sure if it would work on that old version, but couldn’t you just copy the vars plugin file into your local “lookup_plugins” directory?

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

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

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.

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

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
task path: /home/fusillator/Code/ansible/test/role.yml:6
ok: [localhost] => {
“vars[‘mydefaultvar’]”: “myvalue”
}

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
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

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

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