# less ugly way to 'map' in a template?

**URL:** <https://forum.ansible.com/t/less-ugly-way-to-map-in-a-template/15096>\
**Category:** Ansible Project\
**Created:** [April 26, 2014, 5:18pm UTC](https://forum.ansible.com/t/less-ugly-way-to-map-in-a-template/15096 "2014-04-26T17:18:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [April 26, 2014, 5:18pm UTC](https://forum.ansible.com/t/less-ugly-way-to-map-in-a-template/15096/1 "2014-04-26T17:18:59Z")

</div>

Given the following inventory and variables:

ansible-mesos:$ cat hosts  
...  
[zookeepers]  
zk1.dom zk\_id=1  
zk2.dom zk\_id=2  
zk3.dom zk\_id=3  
....

nsible-mesos:$ cat group\_vars/all  
...  
zk\_client\_port: 2181  
mesos\_cluster: democluster  
.....

I want to create a one-line file that looks like:

zk://zk1.dom:2181,zk2.dom:2181,zk3.dom:2181/democluster

groups['zookeepers'] holds the list of hostnames i want, so all i really  
need to do is map() each element to get a new list of  
'fqdn:port' fragments and then join them with ','.

But the best I could manage in an ansible template was:

ansible-mesos:$ cat roles/mesos-common/templates/etc/mesos/zk.j2  
{# clanky hack ahoy! #}  
zk://{% for z in groups['zookeepers'] %}  
{{ hostvars[z][service\_interface].ipv4.address }}:{{ zk\_client\_port  
}}{% if not loop.last %},{% endif %}  
{% endfor %}/{{ mesos\_cluster }}  
ansible-mesos:$

[ i can live with it in this case but annoyingly there are several other  
parts of the service configs that require the same hack and it's  
getting a bit ugly  
in there ]

Is there something like pythons map() I can use in templates (ideally  
without requiring any Jinja add-ons)?

---

<div class="post-metadata">

**Author:** ![Andrew\_Teil](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Andrew\_Teil](https://forum.ansible.com/u/Andrew_Teil)\
**Post date:** [September 14, 2015, 2:35pm UTC](https://forum.ansible.com/t/less-ugly-way-to-map-in-a-template/15096/2 "2015-09-14T14:35:20Z")

</div>

Hi Dick,

I have the same issue. Have you solved it?
