router bgp {{ bgp.as }}
 address-family l2vpn evpn
  advertise-all-vni
  advertise-svi-ip
{% for af in ['ipv4','ipv6'] if af in bgp %}
  advertise {{ af }} unicast
{% endfor %}

! Configure explicit Route Targets and RD per L2 VNI; auto-derived differs
{% for vlan in (vlans|default({})).values() if 'vni' in vlan and 'evpn' in vlan %}
  vni {{ vlan.vni }}
   rd {{ vlan.evpn.rd }}
   route-target export {{ vlan.evpn.export[0] }}
   route-target import {{ vlan.evpn.import[0] }}
  exit-vni
{% endfor %}

{% for n in bgp.neighbors if n.evpn|default(False) %}
{%  for af in ['ipv4','ipv6'] if af in n %}
{%   set peer = n[af] if n[af] is string else n.local_if|default('?') %}
  neighbor {{ peer }} activate
{%   if bgp.rr|default('') and not n.rr|default('') %}
  neighbor {{ peer }} route-reflector-client
{%   endif %}
{%  endfor %}
{% endfor %}

 exit-address-family
!
 
exit

! L3 VRF EVPN handling
{% if vrfs is defined %}
{% for vname,vdata in vrfs.items() if 'evpn' in vdata %}
vrf {{ vname }}
{% if vdata.evpn.transit_vni is defined %}
 vni {{ vdata.evpn.transit_vni }}
{% endif %}
 exit-vrf

router bgp {{ bgp.as }} vrf {{ vname }}
 address-family l2vpn evpn
{% if vdata.evpn.transit_vni is defined %}
  ! This sets the L3 VNI RD/RT for this VRF
  rd {{ vdata.rd }}
  route-target export {{ vdata.export[0] }}
  route-target import {{ vdata.import[0] }}
{% endif %}
  ! Enable RT5 advertisements
{% for af in ['ipv4','ipv6'] if af in vdata.af|default([]) and vdata.af[af] %}
  advertise {{ af }} unicast
{% endfor %}
 exit-address-family
exit
!

{% endfor %}
{% endif %}
