Mickster
Mickster

Reputation: 683

Using a dynamic group name in an ansible template

I am putting together a role to provision several Kafka clusters. Each cluster needs to contain unique broker ids as well as a list of zookeeper nodes. Here is a snippet I am using to define the zookeeper nodes:

zookeeper.connect={% for host in groups['zookeeper'] -%} {{ host }}:2181{% if not loop.last %},{% endif %} {%- endfor %}

I'm trying to find a way to replace groups['zookeeper'] with something similar to the following:

zookeeper.connect={% for host in groups[{{ zookeeper_cluster_name }}] -%} {{ host }}:2181{% if not loop.last %},{% endif %} {%- endfor %}

The zookeeper_cluster_name is defined in my inventory and debug var=zookeeper_cluster_name shows that it's making its way into my role. The parser keeps spitting out errors, and I can't seem to find the correct syntax to specify the group through a variable. Does anyone happen to know if this is possible? If someone has an example (or better way to approach this) I would sure appreciate your sage wisdom.

Upvotes: 0

Views: 579

Answers (1)

Zeitounator
Zeitounator

Reputation: 44635

You are using a variable inside a jinja2 expression. In this case you simply use the variable name.

zookeeper.connect={% for host in groups[zookeeper_cluster_name] -%} {{ host }}:2181{% if not loop.last %},{% endif %} {%- endfor %}

Upvotes: 1

Related Questions