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

27 views
Skip to first unread message

Michael Starling

unread,
Aug 18, 2024, 9:45:47 AM8/18/24
to ansible...@googlegroups.com
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

Will McDonald

unread,
Aug 18, 2024, 10:11:46 AM8/18/24
to ansible...@googlegroups.com
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(',') }}



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAD8F6EndrOwpvRuOYWXauBQQ4xUs7-piNzwzNT1867AtaZLXmg%40mail.gmail.com.

Vladimir Botka

unread,
Aug 18, 2024, 11:58:39 AM8/18/24
to ansible...@googlegroups.com, Michael Starling
On Sun, 18 Aug 2024 09:45:11 -0400
Michael Starling <sturnis...@gmail.com> wrote:

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

Put the below declarations as appropriate

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

Then, the template is trivial

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

gives what you want

shell> cat /tmp/uri.test
URI ldap://server1/ ldap://server2/

HTH,

--
Vladimir Botka

Vladimir Botka

unread,
Aug 18, 2024, 12:05:29 PM8/18/24
to ansible...@googlegroups.com, Will McDonald
On Sun, 18 Aug 2024 15:11:11 +0100
Will McDonald <wmcd...@gmail.com> wrote:

> There may be more elegant ways to achieve this

The below declaration should to the job

seeds: "{{ ansible_play_hosts |
difference([inventory_hostname]) |
map('extract', hostvars, 'ansible_host') |
product([':2113']) |
map('join') |
join(',') }}"

> {%- set seeds = [] %}
> {%- for host in ansible_play_hosts %}
> {%- if host != inventory_hostname %}
> {{ seeds.append(hostvars[host]['ansible_host'] ~ ':2113') }}
> {%- endif %}
> {%- endfor %}
>
> GossipSeed: {{ seeds | join(',') }}

For example, given the inventory


shell> cat hosts
host_A ansible_host=10.1.0.51
host_B ansible_host=10.1.0.52
host_C ansible_host=10.1.0.53

the below play

- hosts: all
vars:
seeds: "{{ ansible_play_hosts |
difference([inventory_hostname]) |
map('extract', hostvars, 'ansible_host') |
product([':2113']) |
map('join') |
join(',') }}"
tasks:
- debug:
var: seeds

gives (abridged)

ok: [host_A] =>
seeds: 10.1.0.53:2113,10.1.0.52:2113
ok: [host_B] =>
seeds: 10.1.0.53:2113,10.1.0.51:2113
ok: [host_C] =>
seeds: 10.1.0.51:2113,10.1.0.52:2113

HTH,

--
Vladimir Botka

Michael Starling

unread,
Aug 19, 2024, 9:03:02 AM8/19/24
to ansible...@googlegroups.com, Will McDonald
Thank you both for the suggestions. Much appreciated.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages