# Variable of variable doesn't work in ansible 1.4

**URL:** <https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051>\
**Category:** Ansible Project\
**Created:** [December 3, 2013, 10:17am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051 "2013-12-03T10:17:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giorgio\_Crivellari](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Giorgio\_Crivellari](https://forum.ansible.com/u/Giorgio_Crivellari)\
**Post date:** [December 3, 2013, 10:17am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/1 "2013-12-03T10:17:59Z")

</div>

I used to use “hosts” property, in ansible playbook, dinamically for example:

> hosts: ${$jobprofile.hosts}

where $jobprofile was an external variable that allow me to change hosts based on profile.

Since last 1.4 version, my previous playbook doesn’t works any more.  
Have you some suggestion to do an EVAL into a playbook?

Thanks  
Giorgio

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 4, 2013, 1:26am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/2 "2013-12-04T01:26:54Z")

</div>

Yeah this is a very outdated usage of legacy variables, and a bit of an anti-pattern whenever you want to define a variable by a variable name. (There’s a reason even Perl tries to discourage this, and that’s Perl).

I don’t know where you are defining “jobprofile”, but can we step back and talk about the use case?

Public service announcement, variables look like this:

foo: “{{ bar }}”

And if you want to get at a variable by name that is scoped to a host, you can do so if you want – which you won’t need often – like so:

{{ hostvars[inventory\_hostname][fact\_name] }}

This doesn’t of course apply to the host specifier – we haven’t even got to figure out what host we’ve talked to yet, hence I’m wanting to understand more.

This seems like a case better suited to seperate inventory files, or the case of a dynamic inventory script, the dynamic inventory script responding to an environment variable.

---

<div class="post-metadata">

**Author:** ![Giorgio\_Crivellari](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Giorgio\_Crivellari](https://forum.ansible.com/u/Giorgio_Crivellari)\
**Post date:** [December 5, 2013, 9:56am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/3 "2013-12-05T09:56:29Z")

</div>

Hi Michael,  
thanks for you response and for this awesome tool!

We’re using it for deployment jobs, as post-build of jenkins jobs, and we’re trying to do something like this:

-------------- job playbook ---------------

- vars:  
devel:  
host: “host02”  
prod:  
host: “host01”

hosts: $vars[$prof][host]

tasks:

- debug: msg=“Hello”

---

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [December 5, 2013, 9:28pm UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/4 "2013-12-05T21:28:56Z")

</div>

Hi Michael,

We are also using double evaluation in many places - mostly to not repeat definitions of the same variable

**Example 1**

user.yml - user uid values

userA: 800  
userB: 801

main.yml - role variables

user: userA  
user\_uid: ${${user}} # will be resolved to 800

**Example 2**

type1\_data:  
var1: value1  
var2: value1  
type2\_data:  
var1: value2  
var2: value2

type\_data: ${${type}\_data}  
type\_var1: ${type\_data.var1}  
type\_var2: ${type\_data.var2}

By overriding single variable we get different plays (all var blocks can be defined in role and block selection can go to single var in vars\_files)  
This also allows command line override with -e option by passing just one key=value.

What will be the appropriate way to use those after old syntax removed?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 5, 2013, 9:30pm UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/5 "2013-12-05T21:30:50Z")

</div>

I feel like I’ve answered this a lot in the last 2 days, see the archives and we can add an entry in the FAQ.

– Michael

---

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [December 5, 2013, 11:03pm UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/6 "2013-12-05T23:03:59Z")

</div>

Though I couldn’t find anything related within past days - I just found solution to my problem using following vars 🙂

users:  
userA: 800  
user: userA  
user\_uid: “{{ users[user] }}”

data:  
type1:  
key: value1  
type2:  
key: value2  
type: type1  
type\_key: “{{ data[type].key }}”

Thanks

---

<div class="post-metadata">

**Author:** ![Giorgio\_Crivellari](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Giorgio\_Crivellari](https://forum.ansible.com/u/Giorgio_Crivellari)\
**Post date:** [December 6, 2013, 9:46am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/7 "2013-12-06T09:46:13Z")

</div>

Dmitry you’re a f\*\*\*\*\* genius!!

Michael, please add Dimitry example in variables website documentation page… many users will appreciate!

Thanks guys!  
Giorgio

---

<div class="post-metadata">

**Author:** ![Sankalp\_Khare](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Sankalp\_Khare](https://forum.ansible.com/u/Sankalp_Khare)\
**Post date:** [October 29, 2014, 8:09am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/8 "2014-10-29T08:09:01Z")

</div>

I second Giorgio. Dmitry, you’re a lifesaver 😃

---

<div class="post-metadata">

**Author:** ![senorsmile](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/senorsmile/32/9024_2.png) [@senorsmile](https://forum.ansible.com/u/senorsmile)\
**Post date:** [May 11, 2015, 6:28pm UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/9 "2015-05-11T18:28:54Z")

</div>

Dmitry, this is the solution to the ultimate problem I’ve been having. Thanks!!!

---

<div class="post-metadata">

**Author:** ![Iliya\_Strakovich](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@Iliya\_Strakovich](https://forum.ansible.com/u/Iliya_Strakovich)\
**Post date:** [July 16, 2016, 9:02am UTC](https://forum.ansible.com/t/variable-of-variable-doesnt-work-in-ansible-1-4/14051/10 "2016-07-16T09:02:46Z")

</div>

Dmitry,

You really saved my day !!!
