#jinja2: lstrip_blocks: True
{% for tunnel in vpn_connections %}
{%   set __vpn_idx = loop.index0 %}
{%   if tunnel.auth_method == 'psk' %}
{%     for host, val in tunnel.hosts.items() %}
{%       if host == inventory_hostname or host == ansible_host %}
{%         for otherhost, otherval in __vpn_psks[__vpn_idx][host].items() %}
{%           if otherhost == item.item %}
{%             set host = tunnel.hosts[host].hostname | d((hostvars[host] | d({})).ansible_host | d(host)) %}
{%             set otherhost = tunnel.hosts[otherhost].hostname | d((hostvars[otherhost] | d({})).ansible_host | d(otherhost)) %}
{{ host | ipaddr | ternary('','@') }}{{ host }} {{ otherhost | ipaddr | ternary('','@') }}{{ otherhost }} : PSK "{{ otherval['pre_shared_key'] }}"
{%           endif %}
{%         endfor %}
{%       endif %}
{%     endfor %}
{%   elif tunnel.auth_method == 'cert' %}
{%     set otherhost = item.item %}
{%     for host, val in tunnel.hosts.items() %}
{%       if host == inventory_hostname or host == ansible_host %}
{%         set cert_name = tunnel.hosts[host].cert_name | d((hostvars[host] | d({})).cert_name) %}
{%         set host = tunnel.hosts[host].hostname | d((hostvars[host] | d({})).ansible_host | d(host)) %}
{%         set otherhost = tunnel.hosts[otherhost].hostname | d((hostvars[otherhost] | d({})).ansible_host | d(otherhost)) %}
{{ host | ipaddr | ternary('','@') }}{{ host }} {{ otherhost | ipaddr | ternary('','@') }}{{ otherhost }} : RSA "{{ cert_name }}"
{%       endif %}
{%     endfor %}
{%   endif %}
{% endfor %}
