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
Il giorno lunedì 11 marzo 2019 21:05:11 UTC+1, fusillator ha scritto:
>
> 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
>
>
>
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/85af3448-9fef-4e20-9b41-7c70921955e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.