# Ansible templates. Joining item vars on one line with static text.

**URL:** <https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852>\
**Category:** Ansible Project\
**Created:** [August 18, 2024, 1:45pm UTC](https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852 "2024-08-18T13:45:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Starling](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Michael\_Starling](https://forum.ansible.com/u/Michael_Starling)\
**Post date:** [August 18, 2024, 1:45pm UTC](https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852/1 "2024-08-18T13:45:11Z")

</div>

Hello.

Consider I have this variable list that can contain as little as 1 and up to 4 items.

ldapservers:

- server1
- server2

Consider this line in my ldap.conf template.

URI ldap://{{ ldapservers(’ ') }}/

I would like the resulting file to look like this:

URI ldap://server1/ ldap://server2/

I know I can use “join” with something like this, but how do I keep the “URI” the " ldap://" and the trailing “/” consistent without adding them to the variable.

URI ldap://{{ ldap\_srv\_fqdn|join(’ ') }}/

Thanks in advance.

Mike

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [August 18, 2024, 2:11pm UTC](https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852/2 "2024-08-18T14:11:11Z")

</div>

There may be more elegant ways to achieve this, but I had a similar requirement from some Eventstore config a while back and ended up with a construct like:

# Construct each GossipSeed from ansible\_play\_hosts minus the inventory\_host

{%- set seeds = %}  
{%- for host in ansible\_play\_hosts %}  
{%- if host != inventory\_hostname %}  
{{ seeds.append(hostvars[host][‘ansible\_host’] ~ ‘:2113’) }}  
{%- endif %}  
{%- endfor %}

GossipSeed: {{ seeds | join(‘,’) }}

In your case you could maybe do something like (untested):

# Construct list of LDAP server URIs

{%- set ldap\_sever\_list = %}  
{%- for server in ldapservers %}  
{{ ldap\_sever\_list.append(‘ldap://’ ~ server ~ ‘/’) }}  
{%- endfor %}

URI: {{ ldap\_server\_list | join(‘,’) }}

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 18, 2024, 3:58pm UTC](https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852/3 "2024-08-18T15:58:09Z")

</div>

> ldapservers:  
> &nbsp;&nbsp;&nbsp;&nbsp;- server1  
> &nbsp;&nbsp;&nbsp;&nbsp;- server2
> 
> I would like the resulting file to look like this:
> 
> URI ldap://server1/ ldap://server2/

Put the below declarations as appropriate

&nbsp;&nbsp;&nbsp;&nbsp;ldapservers: [server1, server2]  
&nbsp;&nbsp;&nbsp;&nbsp;uri: "{{ ['ldap://'] |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product(ldapservers) | map('join') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product(['/'])| map('join') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join(' ') }}"

Then, the template is trivial

&nbsp;&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URI {{ uri }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/uri.test

gives what you want

> cat /tmp/uri.test

URI ldap://server1/ ldap://server2/

HTH,

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 18, 2024, 4:05pm UTC](https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852/4 "2024-08-18T16:05:00Z")

</div>

> There may be more elegant ways to achieve this

The below declaration should to the job

&nbsp;&nbsp;&nbsp;&nbsp;seeds: "{{ ansible\_play\_hosts |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference([inventory\_hostname]) |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, 'ansible\_host') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([':2113']) |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('join') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join(',') }}"

> {%- set seeds = %}  
> {%- for host in ansible\_play\_hosts %}  
> &nbsp;&nbsp;{%- if host != inventory\_hostname %}  
> &nbsp;&nbsp;&nbsp;&nbsp;{{ seeds.append(hostvars[host]['ansible\_host'] ~ ':2113') }}  
> &nbsp;&nbsp;{%- endif %}  
> {%- endfor %}
> 
> GossipSeed: {{ seeds | join(',') }}

For example, given the inventory

&nbsp;&nbsp;\> cat hosts  
&nbsp;&nbsp;host\_A ansible\_host=10.1.0.51  
&nbsp;&nbsp;host\_B ansible\_host=10.1.0.52  
&nbsp;&nbsp;host\_C ansible\_host=10.1.0.53

the below play

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seeds: "{{ ansible\_play\_hosts |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference([inventory\_hostname]) |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, 'ansible\_host') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;product([':2113']) |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('join') |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;join(',') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: seeds

gives (abridged)

ok: [host\_A] =\>  
&nbsp;&nbsp;seeds: 10.1.0.53:2113,10.1.0.52:2113  
ok: [host\_B] =\>  
&nbsp;&nbsp;seeds: 10.1.0.53:2113,10.1.0.51:2113  
ok: [host\_C] =\>  
&nbsp;&nbsp;seeds: 10.1.0.51:2113,10.1.0.52:2113

HTH,

---

<div class="post-metadata">

**Author:** ![Michael\_Starling](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Michael\_Starling](https://forum.ansible.com/u/Michael_Starling)\
**Post date:** [August 19, 2024, 1:02pm UTC](https://forum.ansible.com/t/ansible-templates-joining-item-vars-on-one-line-with-static-text/38852/5 "2024-08-19T13:02:26Z")

</div>

Thank you both for the suggestions. Much appreciated.
