# Resolve nested variable in template

**URL:** <https://forum.ansible.com/t/resolve-nested-variable-in-template/14600>\
**Category:** Ansible Project\
**Created:** [February 19, 2014, 2:03pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600 "2014-02-19T14:03:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [February 19, 2014, 2:03pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/1 "2014-02-19T14:03:59Z")

</div>

Hi

I want to generate the variable names based on another variable value. Something like this:

Vars:  
environements: [“trunk”, “dev01”]  
my\_trunk\_port: 8080  
my\_dev01\_port: 8180

Template:  
{{ my\_{{ env }}\_port }}

---

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [February 19, 2014, 3:32pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/2 "2014-02-19T15:32:45Z")

</div>

I’ve been using this pattern:

vars:  
environments: [“trunk”, “dev01”]  
my\_ports:  
trunk: 8080  
dev01: 8081

template:  
{{ my\_ports[env] }}

-dave

---

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [February 19, 2014, 4:05pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/3 "2014-02-19T16:05:12Z")

</div>

Hi

Thx for the answer. But this is not a option here. The vars is already defined in this format:

my\_trunk\_port: 8080  
my\_dev01\_port: 8180

my\_dev02\_port: 8280

my\_dev03\_port: 8480

and then I have an env var [trunk, dev01, dev02…] to construct the final var name

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [February 19, 2014, 4:11pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/4 "2014-02-19T16:11:17Z")

</div>

​try something like this:  
​  
{{ ​hostvars[inventory\_hostname]["my\_" + ​env + "\_port"] }}

if I'm not mistaken..

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [February 19, 2014, 4:34pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/5 "2014-02-19T16:34:41Z")

</div>

vars:  
&nbsp;&nbsp;port\_vars:  
&nbsp;&nbsp;&nbsp;my\_trunk\_port: 8080  
&nbsp;&nbsp;&nbsp;my\_dev01\_port: 8180  
&nbsp;&nbsp;&nbsp;my\_dev02\_port: 8280  
&nbsp;&nbsp;&nbsp;my\_dev03\_port: 8480

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;my\_ports: {}

- pause: seconds=0  
&nbsp;&nbsp;&nbsp;when: (my\_ports[item[0].substring(3).substring(0,len(item[0]) - 3)] = item[1]) and false or false  
&nbsp;&nbsp;&nbsp;with\_items: port\_vars.items()

- debug: msg="{{ my\_ports['trunk'] }}"

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [February 19, 2014, 4:34pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/6 "2014-02-19T16:34:51Z")

</div>

Hmm, much better than my convoluted example.

---

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [February 19, 2014, 7:48pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/7 "2014-02-19T19:48:23Z")

</div>

Hi

I have added my vars via include\_vars and not in hostvars

- include\_vars: /path/myvars.yml

- template: src=input.j2 dest=output.j2

so {{ ​hostvars[inventory\_hostname][“my\_” + ​env + “\_port”] }} will not working

What will the name of the array then be?

---

<div class="post-metadata">

**Author:** ![Khuong\_Dinh\_Pham](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khuong\_Dinh\_Pham](https://forum.ansible.com/u/Khuong_Dinh_Pham)\
**Post date:** [February 19, 2014, 8:21pm UTC](https://forum.ansible.com/t/resolve-nested-variable-in-template/14600/8 "2014-02-19T20:21:18Z")

</div>

It was a typo error from my side. The solution is working fine. Thx a lot
