# Use lookup/dynamic variable names in jinja2 template

**URL:** <https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246>\
**Category:** Ansible Project\
**Created:** [June 24, 2020, 10:33am UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246 "2020-06-24T10:33:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willem\_Bos](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Willem\_Bos](https://forum.ansible.com/u/Willem_Bos)\
**Post date:** [June 24, 2020, 10:33am UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/1 "2020-06-24T10:33:03Z")

</div>

Hi,

The playbook below should generate a file with the content:  
a,b  
ssh\_host\_key  
ssh\_rsa\_host\_key

However, the way I construct the variable names results in either syntax/templating errors or ‘variable name does not exists’.

---

<div class="post-metadata">

**Author:** ![Martin\_Krizek](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Martin\_Krizek](https://forum.ansible.com/u/Martin_Krizek)\
**Post date:** [June 24, 2020, 12:39pm UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/2 "2020-06-24T12:39:09Z")

</div>

You can use the varnames lookup to get variable names that match given  
pattern: [https://docs.ansible.com/ansible/latest/plugins/lookup/varnames.html](https://docs.ansible.com/ansible/latest/plugins/lookup/varnames.html)

---

<div class="post-metadata">

**Author:** ![Willem\_Bos](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Willem\_Bos](https://forum.ansible.com/u/Willem_Bos)\
**Post date:** [June 25, 2020, 11:47am UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/3 "2020-06-25T11:47:07Z")

</div>

Hi Martin,

Thanks, but I’m not sure varnames is useful to me.

I know what the variable is called, but don’t know how to construct it programmatically (as the Ansible manual calls it). Taking the example code from the documentation results in an empty list:

…

- debug:  
msg: “{{ lookup(‘varnames’, ansible\_distribution + ‘.hostkeys’) }}”  
…  
TASK [debug] \*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”:   
}  
…

Regards,  
Willem.

---

<div class="post-metadata">

**Author:** ![Martin\_Krizek](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Martin\_Krizek](https://forum.ansible.com/u/Martin_Krizek)\
**Post date:** [June 25, 2020, 12:18pm UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/4 "2020-06-25T12:18:23Z")

</div>

Sorry, I misread your example and missed that the variable you want to  
look up is a dictionary and that you want to access a key within that  
dictionary. In that case you need to look up just the var and access  
the key on the result of the lookup:

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: yes  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;Fedora:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key: value  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ lookup('vars', ansible\_distribution)['key'] }}"

---

<div class="post-metadata">

**Author:** ![Willem\_Bos](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Willem\_Bos](https://forum.ansible.com/u/Willem_Bos)\
**Post date:** [June 25, 2020, 1:21pm UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/5 "2020-06-25T13:21:41Z")

</div>

Hi Martin,

Thanks for your quick response, really appreciate it. I hope you don’t mind an additional question: the vars section actually is one level deeper:

vars:  
sshd:  
CentOS:  
ciphers: “a,b”  
hostkeys:

- “ssh\_host\_key”
- “ssh\_rsa\_host\_key”

How do I add this to:  
{% for hostkey in lookup(‘vars’, ansible\_distribution)[‘hostkeys’] %}  
{{ hostkey }}  
{% endfor %}

I tried [‘sshd’]lookup(‘vars’, ansible\_distribution)[‘hostkeys’] and a few variants, but they all fail.

Sorry for being such a noob, but accessing vars/lists/dicts/etc. always gives me major headaches 🙂

Regards,  
Willem.

---

<div class="post-metadata">

**Author:** ![Martin\_Krizek](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Martin\_Krizek](https://forum.ansible.com/u/Martin_Krizek)\
**Post date:** [June 25, 2020, 1:53pm UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/6 "2020-06-25T13:53:17Z")

</div>

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: yes  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;top\_level:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fedora:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key: value  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ top\_level[ansible\_distribution]['key'] }}"

---

<div class="post-metadata">

**Author:** ![Willem\_Bos](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Willem\_Bos](https://forum.ansible.com/u/Willem_Bos)\
**Post date:** [June 26, 2020, 8:39am UTC](https://forum.ansible.com/t/use-lookup-dynamic-variable-names-in-jinja2-template/33246/7 "2020-06-26T08:39:58Z")

</div>

Hi Martin,

Thank you for your effort. And your patience.

Regards,  
Willem.
