# loop over host group to create string

**URL:** <https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699>\
**Category:** Ansible Project\
**Created:** [November 5, 2015, 10:23pm UTC](https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699 "2015-11-05T22:23:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roy](https://forum.ansible.com/u/Roy)\
**Post date:** [November 5, 2015, 10:23pm UTC](https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699/1 "2015-11-05T22:23:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tony\_Kinsley](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@Tony\_Kinsley](https://forum.ansible.com/u/Tony_Kinsley)\
**Post date:** [November 8, 2015, 9:30pm UTC](https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699/2 "2015-11-08T21:30:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_Favorite](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@John\_Favorite](https://forum.ansible.com/u/John_Favorite)\
**Post date:** [November 8, 2015, 9:47pm UTC](https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699/3 "2015-11-08T21:47:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Roy](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Roy](https://forum.ansible.com/u/Roy)\
**Post date:** [November 11, 2015, 6:27pm UTC](https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699/4 "2015-11-11T18:27:34Z")

</div>

> Thanks following works for me.

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

---

<div class="post-metadata">

**Author:** ![John\_Favorite](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@John\_Favorite](https://forum.ansible.com/u/John_Favorite)\
**Post date:** [November 12, 2015, 11:20am UTC](https://forum.ansible.com/t/loop-over-host-group-to-create-string/19699/5 "2015-11-12T11:20:13Z")

</div>

Great, glad it worked for you!
