# can you do nested variables?

**URL:** <https://forum.ansible.com/t/can-you-do-nested-variables/28211>\
**Category:** Ansible Project\
**Created:** [July 31, 2018, 1:43pm UTC](https://forum.ansible.com/t/can-you-do-nested-variables/28211 "2018-07-31T13:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IP\_Freely](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@IP\_Freely](https://forum.ansible.com/u/IP_Freely)\
**Post date:** [July 31, 2018, 1:43pm UTC](https://forum.ansible.com/t/can-you-do-nested-variables/28211/1 "2018-07-31T13:43:43Z")

</div>

I have a playbook that I need to pull a variable based on environment, Im using the following hardcoded enviroment dev currently:

> login\_password: “{{ mysql\_replication\_password [‘dev’] }}”

What I would like to do is use a variable the we already have defined so that I could do something like this:

> login\_password: “{{ mysql\_replication\_password [‘{{ platform }}’] }}”

but this is the error message I keep getting:

> “msg”: "The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘{{ platform }}’\n\nThe error appears to have been in ‘/home/john/xxxxxxxx/ansible/tasks/mysql-enable-replication.yml’: line 67, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem

Could someone point me in the right direction and show me the error of my ways? many thanks in advance

🙂

> 

Vars File:

> mysql\_replication\_password:  
> dev: “XXXXXXXXXXXXXXXXXXX”  
> stage: “YYYYYYYYYYYYYYYYYYYY”  
> prod: “ZZZZZZZZZZZZZZZZZZZZZZ”

from the inventory script:

> “platform”: “dev”

PS this {{ platform }} var works in other places as expected within this playbook.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2018, 2:23pm UTC](https://forum.ansible.com/t/can-you-do-nested-variables/28211/2 "2018-07-31T14:23:12Z")

</div>

You can't have a space between the name and the [

{{ }} indicates you are already in jinja template and can't use {{ }} inside {{ }}. Jinja knows what a variable is so just use it name.  
So the correct syntax will be:

&nbsp;&nbsp;&nbsp;login\_password: "{{ mysql\_replication\_password[platform] }}"

---

<div class="post-metadata">

**Author:** ![IP\_Freely](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@IP\_Freely](https://forum.ansible.com/u/IP_Freely)\
**Post date:** [July 31, 2018, 2:37pm UTC](https://forum.ansible.com/t/can-you-do-nested-variables/28211/3 "2018-07-31T14:37:52Z")

</div>

Works brilliantly! thank you so much!!!
