- set:
    router:
      bgp:
        enable: on
    vrf:
      default:
        router:
          bgp:
{% for af in ['ipv4','ipv6'] if bgp[af] is defined %}
{%   if loop.first %}
            address-family:
{%   endif %}
              {{ af }}-unicast:
                enable: on
                network:
{%   if loopback[af] is defined and bgp.advertise_loopback %}
                  {{ loopback[af]|ipaddr(0) }}: {}
{%   endif %}
{%   for l in interfaces|default([]) if l.bgp.advertise|default("") and l[af] is defined and not 'vrf' in l %}
                  {{ l[af]|ipaddr(0) }}: {}
{%   endfor %}
{%   for pfx in bgp.originate|default([]) if af == 'ipv4' %}
                  {{ pfx|ipaddr('0') }}: {}
{%   endfor %}
{% endfor %}
            autonomous-system: {{ bgp.as }}
            neighbor:
{% for n in bgp.neighbors %}
{%   if n.unnumbered is defined and n.unnumbered is sameas true %}
              {{ n.local_if }}
                type: unnumbered
                address-family:
                  ipv4-unicast:
                    enable: on
                    community-advertise:
                      extended: on
                  ipv6-unicast:
                    enable: on
                    community-advertise:
                      extended: on
{%   else %}
              {{ n.ipv4 }}:
                remote-as: {{ n.as }}
{%     for af in ['ipv4','ipv6'] %}
{%       if loop.first %}
                address-family:
{%       endif %}
{%       if n[af] is defined %}
                  {{ af }}-unicast:
                    enable: on
{%         if n.type == 'ibgp' %}
                    nexthop-setting: self
{%           if bgp.rr|default('') and not n.rr|default('') %}
                    route-reflector-client: on
{%           endif %}
{%           if bgp.community.ibgp|default([]) %}
                    community-advertise:
{%             for ibgp_community in bgp.community.ibgp %}
                      {{ ibgp_community.replace("standard","regular") }}: on
{%             endfor %}
{%           endif %}
{%         else %}
{%           if bgp.community.ebgp|default([]) %}
                    community-advertise:
{%             for ebgp_community in bgp.community.ebgp %}
                      {{ ebgp_community.replace("standard","regular") }}: on
{%             endfor %}
{%           endif %}
{%         endif %}
{%       endif %}
{%     endfor %}
{%   endif %}
{% endfor %}
{% if 'router_id' in bgp %}
            router-id: {{ bgp.router_id }}
{% endif %}
{% if bgp.originate is defined %}
          static:
{%   for pfx in bgp.originate|default([]) %}
            {{ pfx|ipaddr('0') }}:
              address-family: ipv4-unicast
              via:
                blackhole:
                  type: blackhole
{%   endfor %}
{% endif %}
