Variable as array/object key in playbook

24 views
Skip to first unread message

Michael Mallett

unread,
Jun 23, 2015, 6:50:39 AM6/23/15
to ansible...@googlegroups.com
As asked here



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

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

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?

Brian Coca

unread,
Jun 26, 2015, 12:24:22 AM6/26/15
to ansible...@googlegroups.com
I think you want this:

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



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages