can you do nested variables?

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

:slight_smile:

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.

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:

   login_password: "{{ mysql_replication_password[platform] }}"

Works brilliantly! thank you so much!!!