# Variable as array/object key in playbook

**URL:** <https://forum.ansible.com/t/variable-as-array-object-key-in-playbook/18574>\
**Category:** Ansible Project\
**Created:** [June 23, 2015, 10:50am UTC](https://forum.ansible.com/t/variable-as-array-object-key-in-playbook/18574 "2015-06-23T10:50:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mallett](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Michael\_Mallett](https://forum.ansible.com/u/Michael_Mallett)\
**Post date:** [June 23, 2015, 10:50am UTC](https://forum.ansible.com/t/variable-as-array-object-key-in-playbook/18574/1 "2015-06-23T10:50:39Z")

</div>

As asked here

[http://stackoverflow.com/questions/30985183/variable-as-array-object-key-in-ansible](http://stackoverflow.com/questions/30985183/variable-as-array-object-key-in-ansible)

I am writing a playbook that creates a network with a name I assign. Later on in the playbook I need to access the IP that is assigned by this network task, so I get it from the hostvars. So for example it if I was calling the network ‘my\_website’ the value I would be targeting in hostvars would be

```auto
server: "{{hostvars.localhost.rax_nfs.results[0].success[0].rax_addresses.my_website[0].addr}}"

```

This is fine, but I want to name the network based on the contents of a variable passed in by a var file to make it reusable across multiple setups, and then still be able to get that IP back, so

```auto
network_label: "{{ my_website }}"
server: "{{hostvars.localhost.rax_nfs.results[0].success[0].rax_addresses.network_label[0].addr}}"

```

Obviously this doesn’t work as it just assigns a string. How do I use network\_label as the key inside another variable? Like in php something like

$array[$variable], or $object-\>$variable

Is this possible?

---

<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:** [June 26, 2015, 4:24am UTC](https://forum.ansible.com/t/variable-as-array-object-key-in-playbook/18574/2 "2015-06-26T04:24:19Z")

</div>

I think you want this:

server: "{{hostvars.localhost.rax\_nfs.results[0].success[0].rax\_addresses[network\_label][0].addr}}"
