%if %%nombre_interfaces != '1' or %%eth0_method != 'non géré'
%set bonds = []
%set vlans = []

network:
   version: 2
   renderer: networkd
   ethernets:
  %for %%interface in range(0, %%int(%%nombre_interfaces)):
    %set current_interface = 'eth' + str(%%interface)
    %set interface_mode = %%getVar(%%current_interface + '_method')
    %if %%interface_mode != 'non géré'
      %if %%getVar('activer_bonding_' + %%current_interface, "non") == 'non'
        %%getVar('nom_zone_'+ %%current_interface):
            ignore-carrier: yes
            link-local: [ ]
        %if %%interface_mode == 'statique' and %%getVar("adresse_ip_" + %%current_interface):
            addresses:
                - %%getVar("adresse_ip_" + %%current_interface)/%%calc_classe(%%getVar('adresse_netmask_' + %%current_interface))
                %if %%getVar('alias_' + %%current_interface, "non") == "oui"
                %for alias in %%getVar('alias_ip_' + %%current_interface)
                - %%alias/%%calc_classe(getattr(alias, 'alias_netmask_' + %%current_interface))
                %end for
                %end if
        %elif %%interface_mode == 'dhcp'
            dhcp4: yes
        %else
            dhcp4: no
        %end if
        %if %%ip_no_pmtu_disc == 'oui'
          %set mtu=%%getVar('valeur_mtu_' + %%current_interface)
          %if %%mtu is not None
            mtu: %%mtu
          %end if
        %end if
      %else
        %%bonds.append(%%current_interface)
        %for %%slave in %%getVar('nom_carte_' + %%current_interface):
        %%slave:
          %if %%interface_mode == 'manuel'
            activation-mode: manual
          %end if
            optional: false
        %end for
      %end if
      %if %%getVar('vlan_' + %%current_interface, 'non') == 'oui'
        %%vlans.append(%%current_interface)
      %end if
    %end if
  %end for

  %if len(%%bonds) > 0:
   bonds:
        %for bond in %%bonds:
            %set %%bond_mode = %%getVar(%%bond + '_method')
        %%getVar('nom_zone_' + %%bond):
            link-local: [ ]
            interfaces: [ %%custom_join(%%getVar('nom_carte_' + %%bond),',') ]
            %if %%bond_mode == 'statique'
            addresses:
                - %%getVar("adresse_ip_" + %%bond)/%%calc_classe(%%getVar('adresse_netmask_' + %%bond))
                %if %%getVar('alias_' + %%current_interface, "non") == "oui"
                %for alias in %%getVar('alias_ip_' + %%current_interface)
                - %%alias/%%calc_classe(getattr(alias, 'alias_netmask_' + %%current_interface))
                %end for
                %end if
            %elif %%bond_mode == 'dhcp'
            dhcp4: yes
            %else
            dhcp4: no
            %end if
            parameters:
                mode: %%getVar('bonding_mode_' + %%bond)
                mii-monitor-interval: %%getVar('bonding_miimon_' + %%bond)
                up-delay: %%getVar('bonding_updelay_' + %%bond)
                down-delay: %%getVar('bonding_downdelay_' + %%bond)
        %end for
  %end if

  %if len(vlans) > 0:
   vlans:
   %for vlan in %%vlans:
       %for vlan_id in %%getVar('vlan_id_' + %%vlan):
        vlan%%vlan_id:
            link-local: [ ]
            id: %%vlan_id
            link: %%getVar('nom_zone_' + %%vlan)
            addresses: [ %%getattr(vlan_id, 'vlan_ip_' + %%vlan)/%%calc_classe(%%getattr(vlan_id, 'vlan_netmask_' + %%vlan)) ]
        %end for
   %end for
  %end if
%end if
