# Variables inside variables?

**URL:** <https://forum.ansible.com/t/variables-inside-variables/18937>\
**Category:** Ansible Project\
**Created:** [August 4, 2015, 2:22pm UTC](https://forum.ansible.com/t/variables-inside-variables/18937 "2015-08-04T14:22:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Ferrari](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Alejandro\_Ferrari](https://forum.ansible.com/u/Alejandro_Ferrari)\
**Post date:** [August 4, 2015, 2:22pm UTC](https://forum.ansible.com/t/variables-inside-variables/18937/1 "2015-08-04T14:22:06Z")

</div>

Hi, I like to know if possible made reference of a variable inside other variable.

Example:

{{ansible\_local.oracle.credentials.“{{websphere\_mb.odbc.schema\_name}}”}}

with this I try to build the final variable name that if I put manually works, but like to don’t hardcode Schame\_name and read from vars file.

this can be possible?

Thanks  
Ale

---

<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:** [August 4, 2015, 2:31pm UTC](https://forum.ansible.com/t/variables-inside-variables/18937/2 "2015-08-04T14:31:36Z")

</div>

no moustache stacking

{{ansible\_local.oracle.credentials[{{websphere\_mb.odbc.schema\_name]}}

---

<div class="post-metadata">

**Author:** ![Alejandro\_Ferrari](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Alejandro\_Ferrari](https://forum.ansible.com/u/Alejandro_Ferrari)\
**Post date:** [August 4, 2015, 4:11pm UTC](https://forum.ansible.com/t/variables-inside-variables/18937/3 "2015-08-04T16:11:20Z")

</div>

Thanks Brian,

THis is how work for me.

{{ansible\_local.oracle.credentials[websphere\_mb.odbc.schema\_name]}}

And ansible\_local really come from other node that I ask using

- name: Get sql master facts  
setup:  
delegate\_to: “{{websphere\_mb.odbc.host}}”  
register: db\_facts  
tags: mbconfig

Some better way to do this?

THanks  
Ale

---

<div class="post-metadata">

**Author:** ![Virendra\_Bhalothia](https://avatars.discourse-cdn.com/v4/letter/v/eb9ed0/32.png) [@Virendra\_Bhalothia](https://forum.ansible.com/u/Virendra_Bhalothia)\
**Post date:** [August 4, 2015, 5:05pm UTC](https://forum.ansible.com/t/variables-inside-variables/18937/4 "2015-08-04T17:05:50Z")

</div>

Ale/Brian

I’m trying to use variables in variables as well…not sure how to do it. I’m using set facts

Below is the when condition:

hardcoded:

when: “‘{{ hostvars[groups[‘app-a1’][0]][‘target\_app\_build’] }}’ != ‘{{ hostvars[groups[‘app-a1’][0]][‘actual\_app\_build’] }}’”

what I need:

when: “‘{{ hostvars[groups[‘app-a1’][{{ partition }}]][‘target\_app\_build’] }}’ != ‘{{ hostvars[groups[‘app-’][{{ partition }}]][‘actual\_app\_build’] }}’”

Here {{ partition }} - is an extra variable

It doesn’t seem to be working for me.

Thanks,  
Virendra

---

<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:** [August 4, 2015, 5:10pm UTC](https://forum.ansible.com/t/variables-inside-variables/18937/5 "2015-08-04T17:10:38Z")

</div>

same rule no nesting of moustaches:

when: "{{ hostvars[groups['app-a1'][partition]]['target\_app\_build']  
!= hostvars[groups['app-'][partition]]['actual\_app\_build']}}"

---

<div class="post-metadata">

**Author:** ![Rob\_White](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Rob\_White](https://forum.ansible.com/u/Rob_White)\
**Post date:** [September 11, 2015, 7:25am UTC](https://forum.ansible.com/t/variables-inside-variables/18937/6 "2015-09-11T07:25:37Z")

</div>

If there is no moustache stacking does that mean variables inside variables is impossible?

I.e.

environments:  
prod:  
vpc\_id: 12345  
stg:  
vpc\_id: 67890  
env: prod

debug: msg={{ ‘environments.’ + env + ‘.vpc\_id’ }}

This just gives me a string of ‘environments.prod.vpc\_id’ but what I want is ‘12345’

---

<div class="post-metadata">

**Author:** ![Stephan\_Hradek](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Stephan\_Hradek](https://forum.ansible.com/u/Stephan_Hradek)\
**Post date:** [September 11, 2015, 7:35am UTC](https://forum.ansible.com/t/variables-inside-variables/18937/7 "2015-09-11T07:35:33Z")

</div>

I’m completely new to all this, but looking at [http://jinja.pocoo.org/docs/dev/templates/#variables](http://jinja.pocoo.org/docs/dev/templates/#variables)

I’d try to do it like this:

`

environments:  
prod:  
vpc\_id: 12345  
stg:  
vpc\_id: 67890  
env: prod

debug: msg={{ environments[env].vpc\_id }}

`
