loop over host group to create string

I have following host group

[zookeeper]
zk001.abc.cc zookeeper_myid=1
zk002.abc.cc zookeeper_myid=2
zk003.abc.cc zookeeper_myid=3

I want following to be created when playbook run

zookeeper.connect=zk001.abc.cc: 2181,zk002.abc.cc: 2181,zk003.abc.cc:2181

I tried following in a template which worked

zookeeper.connect={{ groups[‘zookeeper’]|join(‘:2181,’) }}:2181

but now I want to use zookeeper_port var instead of 2181, so I tried following but it didn’t worked.

zookeeper.connect={{ groups[‘zookeeper’ ]|join(‘:{{ zookeeper_port }},’) }}:{{ zookeeper_port }}

Any idea how to do this good way

Thanks

I don’t think you need the nested {{}}. Jinja takes something similar to python code when it renders the templates.

so

zookeeper.connect={{ groups[‘zookeeper’] | join( ‘:’ + zookeeper_port + ‘,’ ) }}:{{ zookeeper_port }}

should work. I didn’t test this out myself but I think thats correct.

Tony

Hi Roy,

Here is what I use for a mapr drill-override template:

drill.exec: {
cluster-id: "cloud-drillbits",
zk.connect: "{% for host in groups['zookeeper'] %}{{
hostvars[host]['inventory_hostname'] }}:5181{% if not loop.last %},{%
endif %}{% endfor %}"
}

Hope this helps.

Thanks following works for me.

zookeeper.connect={% for node in groups[‘eventstore-zookeeper-’ ] %}{{ node }}:2181{% if not loop.last %},{% endif %}{% endfor %}

Great, glad it worked for you!