Is there anything in jinja templating to “eval” a variable name? Where I can add a string to a variable, and reference another variable
I’m interesting in doing something like this (example pseudocode)
#print the mac addresses and driver (or similar)
{% for interface in ansible_interfaces %}
Is there anything in jinja templating to "eval" a variable name? Where I
can add a string to a variable, and reference another variable
I'm interesting in doing something like this (example pseudocode)
I did this recently for testing a lookup plugin; I think it's along the
lines of what you want:
{% for name in names -%}
{# Build param consisting of key and file= #}
{%
set param = ' '.join(( name, "file=input.csv", "default=OhNo!", "delimiter=;"))
-%}
{% set val = lookup('csvfile', param) -%}
{{ name }} is in fact "{{ val }}"
{% endfor %}
Random aside: using that CSV lookup plugin will be pretty inefficient – particularly in larger setups. It will load the local file for every host in the host loop, and then once for every name in the names list.
A better way to do this would be to save the mapping as a YAML dictionary and use the stock systems in Ansible here. However, that’s not always possible.
The idea of a way to cache lookup plugin results was explored previously though technically requires some IPC so we maintain the cache in one process OR we extend fact caching proposed for 1.5 to also cache lookup plugin results when evaluated, with, say, lookup_cached, versus lookup, and specify a timeout.