{{ ansible_managed | comment }}

{# {% for config in  network_interfaces %} #}
{% for config in item.1 %}

{%   if config.headline is defined and config.headline %}
#-----------------------------
# {{ config.headline }}
#-----------------------------

{%   endif %}
{# {%   if config.auto is defined and config.auto is sameas true %} #}
{%   if config.auto | default(loop.first) %}
auto {{ config.device }}
{%   endif %}
{%   for stanza in config.allow | default([]) %}
allow-{{ stanza }}
{%   endfor -%}
iface {{ config.device }} {{ config.family | default('inet', true) }} {{ config.method | default('static', true) }}
{%   if config.method == "static" %}
{%     if (config.description is defined and config.description)  %}
   description {{ config.description }}
{%     endif %}
{%     if config.hwaddress is defined and config.hwaddress %}
   hwaddress {{ config.hwaddress }}
{%     endif %}
{%     if (config.address is defined) and (0 < config.address | length) %}
{%       if config.netmask is defined %}
   address {{ config.address }}/{{ config.netmask }}
{%       else %}
   address {{ config.address }}
{%       endif -%}
{%     endif -%}
{%     set iface_keys = ['gateway', 'metric', 'pointopoint', 'media', 'mtu', 'scope'] %}
{%     for key in iface_keys %}
{%       if key in config and config[key] %}
   {{ key }} {{ config[key] }}
{%       endif %}
{%     endfor -%}
{%   elif config.method == "dhcp" %}
{%     set iface_keys = ['hwaddress', 'hostname', 'metric', 'leasehours', 'vendor', 'client' ] %}
{%     for key in iface_keys %}
{%       if key in config and config[key] %}
   {{ key }} {{ config[key] }}
{%       endif %}
{%     endfor -%}
{%   elif config.method == "ppp" %}
{%      if (config.provider is defined and config.provider)  %}
   provider {{ config.provider }}
{%     endif %}
{%   elif config.method == "wvdial" %}
{%      if (config.provider is defined and config.provider)  %}
   provider {{ config.provider }}
{%     endif %}
{%   elif config.method == "bootp" %}
{%     set iface_keys = ['hwaddr', 'bootfile', 'server'] %}
{%     for key in iface_keys %}
{%       if key in config and config[key] %}
   {{ key }} {{ config[key] }}
{%       endif %}
{%     endfor -%}
{%   endif %}

{# #}
{# subnets #}
{%-  if (config.subnets is defined) and (0 < config.subnets | length) %}
   # additional subnets
{%     for subnet in config.subnets %}
   up /sbin/ip addr add {{ subnet }} dev {{ config.device }}
   down /sbin/ip addr del {{ subnet }} dev {{ config.device }}
{%     endfor %}

{%   endif -%}
{# #}
{# bridge settings #}
{%- if config['bridge'] is defined %}
   # bridge settings
{%   for key in config.bridge %}
   bridge_{{ key }} {{ config.bridge[key] }}
{%   endfor %}

{% endif -%}

{# #}
{# bond parameters #}
{%   set bond_keys = ['master', 'primary', 'slaves', 'mode', 'miimon', 'lacp-rate', 'ad-select', 'downdelay', 'updelay'] %}
{%-  if (config.bond is defined) and (bond_keys | intersect(config.bond.keys())) %}
   # bond parameters
{%     for key in bond_keys %}
{%       if key in config.bond %}
   bond-{{ key }} {{ config.bond[key] }}
{%       endif %}
{%     endfor %}

{%   endif -%}

{# #}
{# nameservers #}
{% set ns = namespace(ips='') %}
{%-  if (config.nameservers is defined and config.nameservers) or (config.search is defined and config.search) %}
   # dns-* options are implemented by the resolvconf package, if installed
   # sets entries in /etc/resolv.conf
   #
{% endif -%}
{%  if config.search is defined and config.search %}
   dns-search {{ config.search }}
{%   endif -%}
{%-  if (config.nameservers is defined) and config.nameservers %}
{%     for _ip in config.nameservers %}
{% set ns.ips = ns.ips + ' ' + _ip %}
{%     endfor %}
   dns-nameservers{{ ns.ips }}
{%   endif %}
{%-  if (config.nameservers is defined and config.nameservers) or (config.search is defined and config.search) %}

{%   endif -%}

{# #}
{# vlan #}
{%   set vlan_keys = ['raw-device'] %}
{%-  if (config.vlan is defined) and (vlan_keys | intersect(config.vlan.keys())) -%}
   # vlan
{%     for key in vlan_keys %}
{%       if key in config.vlan %}
   vlan-{{ key }} {{ config.vlan[key] }}
{%       endif %}

{%     endfor %}
{# #}
{%   endif -%}
{# #}
{# hook scripts #}
{%   set hook_keys = ['pre-up', 'up', 'post-up', 'pre-down', 'down', 'post-down'] %}
{%-  if hook_keys | intersect(config.keys()) %}
   # hook scripts
{%     for key in hook_keys %}
{%       if key in config %}
{%         for value in config[key] %}
   {{ key }} {{ value }}
{%         endfor %}
{%       endif %}
{%     endfor %}
{%   endif %}

{% endfor %}
