# Jinja2 - variable inside a variable

**URL:** <https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450>\
**Category:** Ansible Project\
**Created:** [October 7, 2015, 2:40am UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450 "2015-10-07T02:40:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [October 7, 2015, 2:40am UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450/1 "2015-10-07T02:40:34Z")

</div>

Hello Everyone,

I am trying to iterate over a dictionary in a Jinja2 template (in Ansible). One of the arrays or keys in the dictionary is ‘abcd’  
This works fine, but key ‘abcd’ varies in each dictionary.  
`{{ item.value.abcd.port }}`

I am looking to do something like below using a variable ‘nginx\_dir’.  
`{% set nginx_dir = item.value.keys().1 %} {% set my_port = item.value.nginx_dir.port %}`

Regards,  
Vikas

---

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [October 7, 2015, 5:14am UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450/2 "2015-10-07T05:14:09Z")

</div>

I had to use this

`{% set my_port = item.value.get(nginx_dir).port %}`

Regards,  
Vikas

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 7, 2015, 6:56pm UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450/3 "2015-10-07T18:56:55Z")

</div>

this should also work:  
{{ item.value[abcd]port }}

---

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [October 8, 2015, 12:58am UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450/4 "2015-10-08T00:58:55Z")

</div>

Thanks Brian, that is a neat option.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 8, 2015, 4:10pm UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450/5 "2015-10-08T16:10:42Z")

</div>

@vikas, can you please share the complete example, so that it will help others. Thanks

---

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [October 8, 2015, 4:27pm UTC](https://forum.ansible.com/t/jinja2-variable-inside-a-variable/19450/6 "2015-10-08T16:27:55Z")

</div>

Hi Arbab,

Here you go.

I have multiple fact files generated thru YAML files. Consider below yaml file as an example, which I have converted into JSON and placed in ansible facts directory. See this [thread](http://stackoverflow.com/questions/32321911/ansible-iterate-and-include-multiple-variable-files) for converting YAML into JSON and store them as facts.

```auto

---
app_name: dev1 
apps.d:
  port: 1234
  server: test1
  instance: dev

```

When iterating over ansible facts, I didn’t wanted to hard code anything in playbooks or Jinja2 templates, thus below variables becomes very handy.

`  
{% set nginx\_dir = item.value.keys().1 %}  
{% set my\_port = item.value[nginx\_dir].port %}  
{% set my\_instance = item.value[nginx\_dir].instance %}

`

Here, the first line will have apps.d variable in nginx\_dir  
and second line will have 1234 in my\_port variable and so on.

Now, you can these use variables in the Jinja2 template.

Hope this helps.

Regards,  
Vikas
