question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support nested variables

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
ThiefMastercommented, Dec 7, 2016

http://serverfault.com/questions/762079/how-to-loop-through-interface-facts

Sounds like you can do this:

{{ hostvars[inventory_hostname]['ansible_%s' | format(interface)].ipv4.address }}

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…

3reactions
davidismcommented, Dec 7, 2016
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Variables | InsightConnect Documentation
Nested Variables. InsightConnect steps ingest and produce variables that can be strings, integers, booleans, arrays, or objects.
Read more >
Tables with nested variables
When you nest variables in a table, the elements of a nested variable are displayed for each element of the variable within which...
Read more >
Nested Variables - Informatica Documentation
The Data Integration Service resolves one level of variable values. ... variable values that are nested within another workflow parameter or variable.
Read more >
Terraform tricks : How to mimic nested variable substitution ...
It'll probably reduce duplication and improve reusability. This article answers just that as it will demonstrate how dynamic interpolation of a ...
Read more >
nested variables are not yet supported in the build/release ...
Hello,. We are working on a new hub-spoke topology infrastructure on Azure and we are using Azure DevOps to automate our deployments (Terraform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found