{%- if configuration.asset.version -%}
version {{ configuration.asset.version | default('15.2') }}
{%- endif %}
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
service compress-config
!
{%- if configuration.system.config.hostname %}
hostname {{ configuration.system.config.hostname.value }}
{%- endif %}
!
boot-start-marker
boot-end-marker
!
!
{%- if configuration.system.logging -%}
{%-   if configuration.system.logging.config and configuration.system.logging.config.get('buffer_size') %}
logging buffered {{ configuration.system.logging.config['buffer_size'].value | default(4096) }}
{%-   endif %}
{%- endif %}
{%- for ni in configuration.network_instances.values() %}
{%-   if ni.name.value != "global" %}
vrf definition {{ ni.name.value }}
 !
 address-family ipv4
 exit-address-family
 !
 address-family ipv6
 exit-address-family
!
{%-   endif %}
{%- endfor %}
!
!
{%- if configuration.system.aaa.config is defined and configuration.system.aaa.config.enabled %}
aaa new-model
{%-   if configuration.system.aaa.server_groups %}
{%-     for group_name, group in configuration.system.aaa.server_groups.items() %}
{%-       if group.enable and group.enable.value %}
{%-         if group.tacacs %}
!
aaa group server tacacs+ {{ group_name }}
{%-           for server_name in group.tacacs.keys() %}
 server name {{ server_name }}
{%-           endfor %}
{%-         endif %}
{%-         if group.radius %}
!
aaa group server radius {{ group_name }}
{%-           for server_name in group.radius.keys() %}
 server name {{ server_name }}
{%-           endfor %}
{%-         endif %}
{%-       endif %}
{%-     endfor %}
{%-   endif %}
{%- else %}
no aaa new-model
{%- endif %}
!
!
!
!
!
!
!
!
{%- if configuration.system.config.domain_name is defined and configuration.system.config.domain_name %}
ip domain-name {{ configuration.system.config.domain_name.value }}
{%- endif %}
{%- if configuration.cef is not defined or configuration.cef %}
ip cef
{%- endif %}
no ipv6 cef
!
!
!
spanning-tree mode pvst
spanning-tree extend system-id
!
{%- if configuration.system.vtp and configuration.system.vtp.config -%}
{%-   set vtp = configuration.system.vtp.config %}
{%-   if vtp.domain_name %}
vtp domain {{ vtp.domain_name.value }}
{%-   endif %}
{%-   if vtp.mode %}
vtp mode {{ vtp.mode.value }}
{%-   endif %}
{%-   if vtp.version %}
vtp version {{ vtp.version.value }}
{%-   endif %}
{%-   if vtp.password %}
vtp password {{ vtp.password.value }}
{%-   elif vtp.password_hashed %}
vtp password {{ vtp.password_hashed.value }} hidden
{%-   endif %}
{%- endif %}
!
!
!
!
!
!
!
!
!
!
!
!
!
!
{%- if configuration.interface_templates %}
{%-  for _, tmpl in configuration.interface_templates.items() %}
template {{ tmpl.name.value }}
{%-    if tmpl.description %}
 description {{ tmpl.description.value }}
{%-    endif %}
{%-    if tmpl.switchport and tmpl.switchport.value %}
 switchport
{%-      if tmpl.switchport_mode and tmpl.switchport_mode.value == "access" %}
 switchport mode access
{%-        if tmpl.access_vlan %}
 switchport access vlan {{ tmpl.access_vlan.value }}
{%-        endif %}
{%-      endif %}
{%-      if tmpl.switchport_mode and tmpl.switchport_mode.value == "trunk" %}
 switchport mode trunk
{%-        if tmpl.native_vlan %}
 switchport trunk native vlan {{ tmpl.native_vlan.value }}
{%-        endif %}
{%-        if tmpl.trunk_allowed_vlans %}
 switchport trunk allowed vlan {{ tmpl.trunk_allowed_vlans.value | join(',') }}
{%-        endif %}
{%-      endif %}
{%-      if tmpl.voice_vlan %}
 switchport voice vlan {{ tmpl.voice_vlan.value }}
{%-      endif %}
{%-    endif %}
{%-    if tmpl.dtp_negotiation and tmpl.dtp_negotiation.value == False %}
 switchport nonegotiate
{%-    endif %}
{%-    if tmpl.storm_control %}
{%-      if tmpl.storm_control.broadcast_pps %}
 storm-control broadcast level pps {{ tmpl.storm_control.broadcast_pps.value }}
{%-      endif %}
{%-      if tmpl.storm_control.multicast_pps %}
 storm-control multicast level pps {{ tmpl.storm_control.multicast_pps.value }}
{%-      endif %}
{%-      if tmpl.storm_control.unknown_unicast_pps %}
 storm-control unknown-unicast level pps {{ tmpl.storm_control.unknown_unicast_pps.value }}
{%-      endif %}
{%-      if tmpl.storm_control.action %}
 storm-control action {{ tmpl.storm_control.action.value }}
{%-      endif %}
{%-    endif %}
{%-    if tmpl.logging_link_status and tmpl.logging_link_status.value == False %}
 no logging event link-status
{%-    endif %}
{%-    if tmpl.snmp_link_status_trap and tmpl.snmp_link_status_trap.value == False %}
 no snmp trap link-status
{%-    endif %}
{%-    if tmpl.mtu %}
 mtu {{ tmpl.mtu.value }}
{%-    endif %}
{%-    if tmpl.speed %}
 speed {{ tmpl.speed.value }}
{%-    endif %}
{%-    if tmpl.duplex %}
 duplex {{ tmpl.duplex.value }}
{%-    endif %}
{%-    if tmpl.negotiation is defined and tmpl.negotiation and tmpl.negotiation.value == False %}
 no negotiation auto
{%-    endif %}
{%-    if tmpl.auto_mdix is defined and tmpl.auto_mdix and tmpl.auto_mdix.value == False %}
 no mdix auto
{%-    endif %}
{%-    if tmpl.stp_edge_port and tmpl.stp_edge_port.value == True %}
 spanning-tree portfast
{%-    endif %}
{%-    if tmpl.stp_bpdu_guard and tmpl.stp_bpdu_guard.value == True %}
 spanning-tree bpduguard enable
{%-    endif %}
{%-    if tmpl.stp_bpdu_filter and tmpl.stp_bpdu_filter.value == True %}
 spanning-tree bpdufilter enable
{%-    endif %}
{%-    if tmpl.stp_loop_guard and tmpl.stp_loop_guard.value == True %}
 spanning-tree guard loop
{%-    endif %}
{%-    if tmpl.stp_root_guard and tmpl.stp_root_guard.value == True %}
 spanning-tree guard root
{%-    endif %}
{%-    if tmpl.stp_port_priority %}
 spanning-tree port-priority {{ tmpl.stp_port_priority.value }}
{%-    endif %}
{%-    if tmpl.stp_cost %}
 spanning-tree cost {{ tmpl.stp_cost.value }}
{%-    endif %}
{%-    if tmpl.stp_link_type %}
 spanning-tree link-type {{ tmpl.stp_link_type.value }}
{%-    endif %}
{%-    if tmpl.enabled is defined and tmpl.enabled and tmpl.enabled.value == False %}
 shutdown
{%-    endif %}
!
{%-  endfor %}
{%- endif %}
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "softwareLoopback" %}
interface Loopback{{ intf.index.value }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%-  endif %}
{%- endfor %}
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "ieee8023adLag" %}
interface {{ intf.name }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%- if 'ipv4' in intf %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%- endif %}
{%-  if intf.switchport and intf.switchport.value %}
{%-    if intf.switchport_mode and intf.switchport_mode.value == "access" %}
 switchport mode access
{%-      if intf.native_vlan %}
 switchport access vlan {{ intf.native_vlan.value }}
{%-      endif %}
{%-    endif %}
{%-    if intf.switchport_mode and intf.switchport_mode.value == "trunk" %}
 switchport mode trunk
{%-      if intf.native_vlan %}
 switchport trunk native vlan {{ intf.native_vlan.value }}
{%-      endif %}
{%-      if intf.trunk_allowed_vlans %}
 switchport trunk allowed vlan {{ intf.trunk_allowed_vlans.value | join(',') }}
{%-      endif %}
{%-    endif %}
{%-  endif %}
{%-    if intf.interface_template %}
 source template {{ intf.interface_template.pointer.split('.') | last }}
{%-    endif %}
{%-  if intf.enabled and intf.enabled.value %}
 no shutdown
{%-    else %}
 shutdown
{%-   endif %}
{%-  endif %}
{%-  if intf.type == "ieee8023adLag" %}
!
{%-  endif %}
{%- endfor %}
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "ethernetCsmacd" %}
interface {{ intf.name }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%- if 'ipv4' in intf %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%- endif %}
{%-  if intf.access_vlan %}
 switchport access vlan {{ intf.access_vlan.value }}
{%-  endif %}
{%-  if intf.switchport_mode and intf.switchport_mode.value == "access" %}
 switchport mode access
{%-  endif %}
{%-  if intf.switchport_mode and intf.switchport_mode.value == "trunk" %}
 switchport mode trunk
{%-  endif %}
{%-  if intf.native_vlan %}
 switchport trunk native vlan {{ intf.native_vlan.value }}
{%-  endif %}
{%-  if intf.trunk_allowed_vlans %}
 switchport trunk allowed vlan {{ intf.trunk_allowed_vlans.value | join(',') }}
{%-  endif %}
{%-  if intf.voice_vlan %}
 switchport voice vlan {{ intf.voice_vlan.value }}
{%-  endif %}
{%-  if intf.lacp_enabled %}
 channel-group {{ intf.aggregate_id.value }} mode {{ intf.lacp_mode.value | default('active') }}
{%-    if intf.lacp_port_priority %}
 lacp port-priority {{ intf.lacp_port_priority.value }}
{%-    endif %}
{%-    if intf.lacp_interval %}
 lacp rate {{ intf.lacp_interval.value }}
{%-    endif %}
{%-  endif %}
{%-  if intf.dtp_negotiation and intf.dtp_negotiation.value == False %}
 switchport nonegotiate
{%-  endif %}
{%-  if intf.storm_control %}
{%-    if intf.storm_control.broadcast_pps %}
 storm-control broadcast level pps {{ intf.storm_control.broadcast_pps.value }}
{%-    endif %}
{%-    if intf.storm_control.multicast_pps %}
 storm-control multicast level pps {{ intf.storm_control.multicast_pps.value }}
{%-    endif %}
{%-    if intf.storm_control.unknown_unicast_pps %}
 storm-control unknown-unicast level pps {{ intf.storm_control.unknown_unicast_pps.value }}
{%-    endif %}
{%-    if intf.storm_control.action %}
 storm-control action {{ intf.storm_control.action.value }}
{%-    endif %}
{%-  endif %}
{%-  if intf.logging_link_status and intf.logging_link_status.value == False %}
 no logging event link-status
{%-  endif %}
{%-  if intf.snmp_link_status_trap and intf.snmp_link_status_trap.value == False %}
 no snmp trap link-status
{%-  endif %}
{%-  if intf.auto_mdix and intf.auto_mdix.value == False %}
 no mdix auto
{%- endif %}
{%-    if intf.interface_template %}
 source template {{ intf.interface_template.pointer.split('.') | last }}
{%-    endif %}
{%-  if intf.stp_portfast and intf.stp_portfast.value == True %}
 spanning-tree portfast
{%- endif %}
{%-  if intf.enabled and intf.enabled.value == False %}
 shutdown
{%-   endif %}
{%-  endif %}
{%-  if intf.type == "ethernetCsmacd" %}
!
{%-  endif %}
{%- endfor %}
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "l3ipvlan" and intf.vlan_id %}
interface vlan{{ intf.vlan_id.value }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%-    if intf.enabled and intf.enabled.value %}
 no shutdown
{%- else %}
 shutdown
{%-     endif %}
!
{%-  endif %}
{%- endfor %}
!
{%- for _, intf in configuration.interfaces.items() %}
{%-  if intf.type == "ManagementInterface" and intf.name %}
interface {{ intf.name.value }}
{%-    if intf.description %}
 description {{ intf.description.value }}
{%-    endif %}
{%- if 'vrf' in intf %}
 vrf forwarding {{ intf.vrf}}
{%- endif %}
{%-    if intf.ipv4 %}
 ip address {{ intf.ipv4.value|cidr_to_addrmask }}
{%-     endif %}
{%-    if intf.enabled and intf.enabled.value %}
 no shutdown
{%- else %}
 shutdown
{%-     endif %}
{%-  endif %}
{%-  if intf.type == "ManagementInterface" and intf.name %}
!
{%-  endif %}
{%- endfor %}
!
{%- if configuration.system.config.default_gateway is defined and configuration.system.config.default_gateway %}
ip default-gateway {{ configuration.system.config.default_gateway }}
{%- endif %}
ip forward-protocol nd
!
ip http server
ip http secure-server
!
{% set ssh = configuration.system.ssh.config -%}
{%- if ssh and ssh.get('enable') and ssh.get('enable').value %}
{%-   if ssh.get('protocol_version') %}
ip ssh version {{ ssh.get('protocol_version').value | default(2) }}
{%-   endif %}
{%-   if ssh.get('timeout') %}
ip ssh time-out {{ ssh.get('timeout').value }}
{%-   endif %}
{%-   if ssh.get('auth_retries') %}
ip ssh authentication-retries {{ ssh.get('auth_retries').value }}
{%-   endif %}
{%-   if ssh.get('source_interface') %}
ip ssh source-interface {{ ssh['source_interface'] }}
{%-   endif %}
{%- endif %}
!
{%- for _, group in configuration.system.aaa.server_groups.items() %}
{%-   if group.tacacs %}
{%-     for server_name, server in group.tacacs.items() %}
tacacs server {{ server_name }}
{%-       if server.address %}
 address ipv4 {{ server.address.value }}
{%-       endif %}
{%-       if server.port %}
 port {{ server.port.value }}
{%-       endif %}
{%-       if server.secret_key_hashed %}
 key 7 {{ server.secret_key_hashed.value }}
{%-       elif server.secret_key %}
 key 0 {{ server.secret_key.value }}
{%-       endif %}
{%-       if server.timeout %}
 timeout {{ server.timeout.value }}
{%-       endif %}
!
{%-     endfor %}
{%-   endif %}
{%-   if group.radius %}
{%-     for server_name, server in group.radius.items() %}
radius server {{ server_name }}
{%-       if server.address %}
 address ipv4 {{ server.address.value }}{% if server.auth_port %} auth-port {{ server.auth_port.value }}{% endif %}{% if server.acct_port %} acct-port {{ server.acct_port.value }}{% endif %}
{%-       endif %}
{%-       if server.secret_key_hashed %}
 key 7 {{ server.secret_key_hashed.value }}
{%-       elif server.secret_key %}
 key 0 {{ server.secret_key.value }}
{%-       endif %}
{%-       if server.timeout %}
 timeout {{ server.timeout.value }}
{%-       endif %}
!
{%-     endfor %}
{%-   endif %}
{%- endfor %}
{%- if "lacp" in configuration -%}
{%-   if configuration.lacp.config.system_priority %}
lacp system-priority {{ configuration.lacp.config.system_priority.value }}
{%-   endif %}
{%-   if configuration.lacp.config.system_id_mac %}
lacp system-id mac {{ configuration.lacp.config.system_id_mac.value }}
{%-   endif %}
{%-   if configuration.lacp.config.load_balance_algorithm %}
{%-     set alg = configuration.lacp.config.load_balance_algorithm.value %}
{%-     if alg is string %}
port-channel load-balance {{ alg }}
{%-     elif alg is sequence and alg | length >= 2 %}
port-channel load-balance extended {{ alg | join(' ') }}
{%-     elif alg is sequence and alg | length == 1 %}
port-channel load-balance {{ alg[0] }}
{%-     else %}
port-channel load-balance {{ alg }}
{%-     endif %}
{%-   endif %}
{%- endif %}
!
{%- if "snmp" in configuration -%}
{%- if configuration.snmp.community %}
snmp-server community {{ configuration.snmp.community }} {{ configuration.snmp.access | default('RO') }}
{%- endif %}
{%- if configuration.snmp.location %}
snmp-server location {{ configuration.snmp.location }}
{%- endif %}
{%- if configuration.snmp.contact %}
snmp-server contact {{ configuration.snmp.contact }}
{%- endif %}
{%- endif %}
!
{%- if configuration.system.ntp.servers -%}
{%- for server in configuration.system.ntp.servers.values() %}
{%-   if server.prefer %}
ntp server {{ server.address.value }} prefer
{%-   else %}
ntp server {{ server.address.value }}
{%-   endif %}
{%- endfor %}
{%- endif %}
!
{%- if configuration.system.logging -%}
{%-   if configuration.system.logging.get('remote_servers') %}
{%-     for server_group in configuration.system.logging.get('remote_servers').values() %}
{%-       for _, server in server_group.items() %}
{%-         if server.host and server.host.value %}
{%-           if server.source_address and server.source_address.value %}
logging source-interface {{ server.source_address.value }}
{%-           endif %}
logging host {{ server.host.value }}
{%-         endif %}
{%-       endfor %}
{%-     endfor %}
{%-   endif %}
{%- endif %}
!
line con 0
 logging synchronous
 exec-timeout 0 0
line vty 0 4
 transport input ssh
!
end