Given the following example:
foo: “y”
boo: “x is {{ foo }}”
This will renders in a template “x is y” (when referenced as {{ boo }}). However when called as hostvars[host][‘boo’] this renders as “x is {{ foo }}”. From what I can tell this is an interpolation or merging problem. I’ve tried different YAML formatting options to no avail. Appreciate any help or direction to share, thank you.
–Matt
the reason it 'works' in some places is because there is double
interpolation in some directives and templates, when accessing the value
directly you CORRECTLY get "x is {{ foo }}", YAML will NOT evaluate this on
loading, this evaluates in 'templated' directives and templates using
jinja2.
Nothing gets templated when accessed through hostvars, see (disclaimer: I’ve opened this issue).
Note that comments on old tickets are not something that will get red, there are simply too many posts of this nature.
Original ticket was rather unclear about what it was about, so it got closed.
To be honest, nested variable evaluation in hostvars is a can of worms to resolve and I don’t think it will be a thing.
Michael DeHaan <michael@ansible.com> napisał:
To be honest, nested variable evaluation in hostvars is a can of worms
to
resolve and I don't think it will be a thing.
Can you point to any specific problems? I'm using a filter that just resolves host's vars in the context of hostvars[host], and it seems to work ok
I could be very very wrong about my assumption.
Can we see the source to your filter? It might be instructive about what this might take.
Dnia czw, 17 lip 2014, 14:29:45 Michael DeHaan pisze:
I could be very very wrong about my assumption.
Can we see the source to your filter? It might be instructive about
what this might take.
I kind of hope you are, as it would be really handy to have this in Ansible core
My filter is attached. I'm afraid it ignores variables set with Jinja2 in current template (as it only looks at the context with "basedir" attribute), and I'm not sure about role's and play's vars. So far I've only used it to access variables that could be resolved without even looking at playbook, only with knowledge from inventory and {group,host}_vars.
Example usage: {{ ('other_host' | get_hostvars).other_hosts_variable }}
(attachments)
hostvars.py (661 Bytes)
Not too complicated.
I’d worry abit about the efficiency as this would need to be calculated a fair amount, but it’s something we can look into.
Note there is something of a backlog, but I’d file a ticket with info if not already and we can explore it.