For loop in template with every host in group except the current one

Hi Everyone,

I’m trying to work out if something is possible. Imagine a playbook to configure a vpn mesh between a group of servers. If I created a group called ‘vpn’, then on each server I’d need to build template configs for each vpn tunnel with the server we’re writing the file one at one end and then all the other servers in the group at the other.

Therefore; is there a way to make a for loop either in the playbook or template that says:

(Excuse my pseudocode)

{% for host in groups[‘vpn’] except [‘thishost’] %}

vpn left side = {{ hostvars[thishost].ansible_hostname }}

vpn right side = {{ hostvars[host].ansible_hostname }}

{% endfor %}

Hope that makes sense…

Steve

iirc you need the loopcontrol extensions, which you can load, then just add indside the for loop:

if host = inventory_hostname
continue
endif

{% for host in groups[‘vpn’] if host != thishost %}

vpn left side = {{ hostvars[thishost].ansible_hostname }}
vpn right side = {{ hostvars[host].ansible_hostname }}

{% endfor %}

should work also

It would be cleaner to just put the if statement inside the for loop.