Support nested variables
See original GitHub issueI have been searching through documentation and various online articles and could not seem to find any mention of having nested variables so I am opening up a feature request. This could help benefit many projects by allowing more complex and automatic variable calls.
A normal variable can be defined like this:
{{ variable }}
It would be useful to allow dynamic information from another variable to be used to populate another variables name.
{{ variable{{ generated_var }} }}
My exact use case is with Ansible. I am grabbing a list of network interface devices from one variable (ansible_interfaces). Then I can use this to reference the information from another variable (ansible_eth0, for example). Here is a basic framework of what is trying to accomplish.
{% for interface in ansible_interfaces %}
IPADDR{{ loop.index }}={{ ansible_{{ interface }}.ipv4.address }}
NETMASK{{ loop.index }}={{ ansible_{{ interface }}.ipv4.netmask }}
{% endfor %}
Jinja2 does not like that when it renders as it complains about the extra brackets existing. For options of how to implement this I was thinking along the lines of one of these two different ideas.
(1) Allow variable expansion. This is exactly what I was showing earlier; allowing variables to be parsed from the inside-out.
(2) Add a filter to convert a string into a variable name.
{% for interface in ansible_interfaces %}
{% set interface_string="ansible_%s.ipv4.address"|format(interface) %}
IPADDR{{ loop.index }}={{ interface_string|variable }}
NETMASK{{ loop.index }}={{ interface_string|variable }}
{% endfor %}
Here is some rough (non-working) code showing the second idea.
# vim jinja2/jinja2/filters.py
@environmentfilter
def do_variable(environment, s):
string_to_variable = "{{ %s }}" % s
return environment.from_string(string_to_variable).render()
Ideally option 1 would be less complex in terms of end-user use. Let me know your thoughts on the matter. This is my first time ever looking into the code for Jinja2 but I would love to contribute back if any help is needed with the code.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:13
- Comments:7 (4 by maintainers)
http://serverfault.com/questions/762079/how-to-loop-through-interface-facts
Sounds like you can do this:
I’m very strong 👎 on variable variable names. It’s usually a sign of bad architecture if an application doesn’t provide a proper dict/list of data if you need to access it by dynamic key or iterate over it. FWIW, I think this
hostvars
dict is somewhat ugly compared to a proper dict mapping interface names to interface data. I’d open an issue with Ansible, suggesting to change that list to a dict. Since iterating over a dict yields you its keys it might not even be backwards incompatible if they changed it to a dict…Please see http://stackoverflow.com/q/1373164/400617.