# {{ ansible_managed }}

{% set network = item.value %}
{% set network_name = item.value.name | d(item.key) %}
{% set tinc__tpl_seen_keys = [ 'name', 'tinc-up', 'tinc-down', 'host-up', 'host-down', 'subnet-up', 'subnet-down' ] %}
{% if network.tinc_options|d() %}
Name = {{ network.hostname|d(network.tinc_options.Name|d(tinc__hostname )) | replace("-","_") }}
{%   for key, value in network.tinc_options.items() %}
{%     if key | lower not in tinc__tpl_seen_keys %}
{%       if value is string %}
{%         if key | lower in [ 'connectto' ] %}
{{ key }} = {{ value.split(".") | first | replace("-","_") }}
{%         else %}
{{ key }} = {{ value }}
{%         endif %}
{%       else %}
{%         for element in value %}
{%           if key | lower in [ 'connectto' ] %}
{{ key }} = {{ element.split(".") | first | replace("-","_") }}
{%           else %}
{{ key }} = {{ element }}
{%           endif %}
{%         endfor %}
{%       endif %}
{%     endif %}
{%   endfor %}
{%   if not (network.node_reachable|d(True) | bool) and "BindToInterface"|lower not in tinc__tpl_seen_keys %}
# Disabled because with this experimental feature enabled Tinc failed to
# connect to the VPN with "Error looking up [peer]: Name or service not known"
# as of 1.0.24-2.1+b1.
#BindToInterface = lo
{%   endif %}
{% endif %}
