GMaster
GMaster

Reputation: 1521

Ansible nested variable

I have a variable like this:

unicast_ip_group: "{{ groups['elasticsearch-demo'] | map('extract', hostvars, ['ansible_host']) | join(':9300,') }}:9300"

I want to make the static value elasticsearch-demo a variable. I have tried this but this looks like is not supported in Ansible:

unicast_ip_group: "{{ groups['{{ my_variable }}'] | map('extract', hostvars, ['ansible_host']) | join(':9300,') }}:9300"

Upvotes: 1

Views: 787

Answers (1)

techraf
techraf

Reputation: 68629

The following should work:

unicast_ip_group: "{{ groups[my_variable] | map('extract', hostvars, ['ansible_host']) | join(':9300,') }}:9300"

You already opened a Jinja2 expression with {{, so you can use variables just by referring their names.

Upvotes: 3

Related Questions