# Jinja "eval" type method?

**URL:** <https://forum.ansible.com/t/jinja-eval-type-method/14012>\
**Category:** Ansible Project\
**Created:** [November 27, 2013, 10:16am UTC](https://forum.ansible.com/t/jinja-eval-type-method/14012 "2013-11-27T10:16:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Little](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@Brian\_Little](https://forum.ansible.com/u/Brian_Little)\
**Post date:** [November 27, 2013, 10:16am UTC](https://forum.ansible.com/t/jinja-eval-type-method/14012/1 "2013-11-27T10:16:43Z")

</div>

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 %}

{{ interface }}  
mac= {{ eval(‘ansible\_’ + interface).macaddress }}  
driver = {{ eval(‘ansible\_’ + interface).module }}

{% endfor %}

Where the host variables are like this ( an example ):

“ansible\_interfaces”: [  
“eth1”,  
“eth2”  
],  
“ansible\_eth1”: {  
“active”: false",  
“device”: “eth1”,  
“macaddress”: “00:00:00:FF:FF:FF”,  
“module”: “igb”,  
“mtu”: 1500,  
“type”: “ether”  
},  
“ansible\_eth2”: {  
“active”: false",  
“device”: “eth2”,  
“macaddress”: “AA:00:00:FF:FF:AA”,  
“module”: “ixgbe”,  
“mtu”: 9000,  
“type”: “ether”  
},

Is something like this possible, or do I have to some combination of templating using with\_items, and then using assemble to stick it together?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 27, 2013, 4:53pm UTC](https://forum.ansible.com/t/jinja-eval-type-method/14012/2 "2013-11-27T16:53:29Z")

</div>

Thank God, no Python evals.

You can access hostvars programatically like this:

hostvars[hostname][factname]

So you can build the factname string programatically and get to them that way.

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [November 28, 2013, 8:20am UTC](https://forum.ansible.com/t/jinja-eval-type-method/14012/3 "2013-11-28T08:20:54Z")

</div>

> 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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for name in names -%}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{# Build param consisting of key and file= #}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{%  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set param = ' '.join(( name, "file=input.csv", "default=OhNo!", "delimiter=;"))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-%}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% set val = lookup('csvfile', param) -%}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ name }} is in fact "{{ val }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

using the variables you want, of course. 🙂

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 1, 2013, 2:35pm UTC](https://forum.ansible.com/t/jinja-eval-type-method/14012/4 "2013-12-01T14:35:59Z")

</div>

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.

lookup\_cached(‘plugin\_name’, ‘param string’, timeout\_in\_seconds)

Hmmm… ideas… 🙂

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Adamo](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Massimiliano\_Adamo](https://forum.ansible.com/u/Massimiliano_Adamo)\
**Post date:** [April 10, 2017, 2:32pm UTC](https://forum.ansible.com/t/jinja-eval-type-method/14012/5 "2017-04-10T14:32:22Z")

</div>

Thanks God ERB templates allow to embed Ruby code.  
It is discouraged, but when it’s really necessary you can do it!.
