{% macro ip_addresses(intf,ipv6_ra,is_system) %}
{% if 'ipv4' in intf %}
    ipv4:
     address:
     - ip-prefix: "{{ intf.ipv4 }}"
{% if not is_system %}
       primary: [null]
{% endif %}
{% endif %}
{% if 'ipv6' in intf %}
    ipv6:
     address:
     - ip-prefix: "{{ intf.ipv6 }}"
{% if ipv6_ra %}
     router-advertisement:
      router-role:
       admin-state: enable            # no ipv6 nd suppress-ra
       max-advertisement-interval: 5  # ipv6 nd ra-interval 5
{% endif %}
{% endif %}
{% endmacro %}

updates:
- path: interface[name=lo0]/subinterface[index=0]
  val:
{{  ip_addresses(loopback,False,False)  }}
- path: interface[name=system0]/subinterface[index=0]
  val:
{{  ip_addresses(loopback,False,True)  }}

{% for l in interfaces|default([]) %}
- path: interface[name={{ 'lo0' if not l.neighbors|default([]) else l.ifname }}]
  val:
{% if l.name is defined %}
   description: "{{ l.name|replace('->','~')|regex_replace('[\\[\\]]','') }}{{ " ("+l.role+")" if l.role is defined else '' }}"
{% elif l.type|default("") == "stub" %}
   description: "Stub interface"
{% endif %}
   subinterface:
    index: {{ l.ifindex if not l.neighbors|default([]) else 0 }}
{{  ip_addresses(l,True,False) }}
{% if l.bandwidth is defined %}
 # bandwidth {{ l.bandwidth  }}
{% endif %}
{% endfor %}

{# Make sure the default VRF is called 'default' #}
- path: network-instance[name=default]
  val:
   type: default
   interface:
   - name: {{ 'system0.0' if 'sr' in module|default([]) else 'lo0.0' }}
{% for l in interfaces|default([]) %}
   - name: {{ 'lo0.%d'|format(l.ifindex) if not l.neighbors|default([]) else (l.ifname+'.0') }}
{% endfor %}
