%set %%ethernet_routes = {}
µµµµµµµµµµ Add default route to ethernet interface when bonding is disabled
%if not %%is_empty(%%adresse_ip_gw) and not %%getVar('activer_bonding_eth0', 'non') == 'oui'
    %silent %%ethernet_routes.setdefault(%%nom_zone_eth0, []).append(
      {'dest': 'default',
       'gw': %%adresse_ip_gw}
    )
%end if
%set %%bond_routes = {}
µµµµµµµµµµ Add default route to bond interface when bonding is enabled
%if not %%is_empty(%%adresse_ip_gw) and %%getVar('activer_bonding_eth0', 'non') == 'oui'
    %silent %%bond_routes.setdefault(%%nom_zone_eth0, []).append(
      {'dest': 'default',
       'gw': %%adresse_ip_gw}
    )
%end if
%set %%vlan_routes = {}
%for %%interface in range(0, %%int(%%nombre_interfaces)):
    %set current_interface = 'eth' + str(%%interface)
    %for %%vlan in %%getVar('vlan_id_' + %%current_interface, [])
        %set %%vlan_gw = %%getattr(%%vlan, 'vlan_gw_' + %%current_interface, False)
        %if %%vlan_gw
        %silent %%vlan_routes.setdefault(%%int(%%str(%%vlan)), []).append(
          {'dest': 'default',
           'gw': %%vlan_gw}
        )
        %end if
    %end for
%end for
%for %%route in %%getVar('route_adresse', [])
    %set %%interface = %%route.route_int
    %if %%int(%%interface) >= %%int(%%nombre_interfaces)
        %continue
    %end if
    %set %%carte = %%getVar(u'nom_zone_eth'+ %%interface)
    %if %%is_empty(%%route.route_vlan_id)
        %if %%getVar(u'activer_bonding_eth'+ %%interface) == 'non'
            %silent %%ethernet_routes.setdefault(%%carte, []).append(
              {'dest': %%route,
               'gw': %%route.route_gw,
               'netmask': %%route.route_netmask}
            )
        %else
            %silent %%bond_routes.setdefault(%%carte, []).append(
              {'dest': %%route,
               'gw': %%route.route_gw,
               'netmask': %%route.route_netmask}
            )
        %end if
    %else
        %silent %%vlan_routes.setdefault(%%route.route_vlan_id, []).append(
          {'dest': %%route,
           'gw': %%route.route_gw,
           'netmask': %%route.route_netmask}
        )
    %end if
%end for
%if %%ethernet_routes or %%bond_routes or %%vlan_routes
network:
   version: 2
   renderer: networkd
   %if %%ethernet_routes
   ethernets:
   %for %%interface in %%ethernet_routes:
       %%interface:
           routes:
           %for %%route in %%ethernet_routes[interface]:
               %if %%route.dest == 'default'
               - to: %%route.dest
                 via: %%route.gw
               %else
               - to: %%route.dest/%%calc_classe(%%route.netmask)
                 via: %%route.gw
                 metric: 100
               %end if
           %end for
   %end for
   %end if
   %if %%vlan_routes
   vlans:
   %for %%vlan_id in %%vlan_routes:
       vlan%%vlan_id:
           routes:
           %for %%route in %%vlan_routes[vlan_id]:
               %if %%route.dest == 'default'
               - to: %%route.dest
                 via: %%route.gw
               %else
               - to: %%route.dest/%%calc_classe(%%route.netmask)
                 via: %%route.gw
                 metric: 100
               %end if
           %end for
   %end for
   %end if
   %if %%bond_routes
   bonds:
   %for %%interface in %%bond_routes
       %%interface:
           routes:
           %for %%route in %%bond_routes[interface]:
               %if %%route.dest == 'default'
               - to: %%route.dest
                 via: %%route.gw
               %else
               - to: %%route.dest/%%calc_classe(%%route.netmask)
                 via: %%route.gw
                 metric: 100
               %end if
           %end for
    %end for
    %end if
%end if
