More converting from Nautobot GraphQL/JSON to Netbox objects

netbox
Daniel Ankers 2024-06-28 15:17:34 +01:00
parent 5114a4fcad
commit c6cab802be
2 changed files with 57 additions and 61 deletions

View File

@ -1,6 +1,6 @@
{% macro systemsection(device,root_pw,users,syslog_servers,ntp_servers) %}
system {
host-name {{ device['hostname'] }};
host-name {{ device.hostname }};
root-authentication {
encrypted-password "{{ root_pw }}"; ## Client Higher
}
@ -78,20 +78,20 @@ chassis {
{% macro interfaceconfig(interface,rack = None) %}
{{ interface['name'] }} {
{{ interface.name }} {
{% if interface.get('description') != '' %}
description "{{ interface['description'] }}";
description "{{ interface.description }}";
{% endif %}
{% if interface.get('mtu') and not interface.get('lag') %}
mtu {{ interface['mtu'] }};
mtu {{ interface.mtu }};
{% endif %}
{% if interface.get('lag') %}
ether-options {
802.3ad {{ interface['lag']['name'] }};
802.3ad {{ interface.lag.name }};
}
{% endif %}
{% if interface['name'].startswith('ae') %}
{% if interface['_custom_field_data']['esi_lag'] %}
{% if interface.name.startswith('ae') %}
{% if interface.custom_field_data['esi_lag'] %}
esi {
auto-derive {
lacp;
@ -102,35 +102,35 @@ chassis {
aggregated-ether-options {
lacp {
periodic fast;
{% if interface['custom_field_data']['system_id'] %}
system-id {{ interface['custom_field_data']['system_id'] }};
{% elif interface['custom_field_data']['esi_lag'] %}
system-id 00:00:{{ '%02d' % rack|int }}:{% if interface['name'][2:]|int < 99 %}{{ '%02d' % interface['name'][2:]|int }}{% else %}{{ '%02x' % interface['name'][2:]|int }}{% endif %}:00:01;
{% if interface.custom_field_data['system_id'] %}
system-id {{ interface.custom_field_data['system_id'] }};
{% elif interface.custom_field_data['esi_lag'] %}
system-id 00:00:{{ '%02d' % rack|int }}:{% if interface.name[2:]|int < 99 %}{{ '%02d' % interface.name[2:]|int }}{% else %}{{ '%02x' % interface.name[2:]|int }}{% endif %}:00:01;
{% endif %}
}
}
{% endif %}
{% if interface['ip_addresses']|length > 0 %}
{% if interface.ip_addresses.all()|length > 0 %}
unit 0 {
family inet {
{% if interface['ip_addresses'][0]['status']['name'] == 'Reserved' %}inactive: {% endif %}address {{ interface['ip_addresses'][0]['address'] }};
{% if interface.ip_addresses[0].status == 'reserved' %}inactive: {% endif %}address {{ interface.ip_addresses[0].address }};
}
}
{% endif %}
{% if interface.get('mode') is not none %}
{% if interface.mode <> '' %}
unit 0 {
family ethernet-switching {
{% if interface['mode']=='ACCESS' %}
{% if interface.mode=='ACCESS' %}
interface-mode access;
vlan {
members {{ interface['untagged_vlan']['vid'] }};
members {{ interface.untagged_vlan.vid }};
}
{% elif interface['mode']=='TAGGED' %}
{% elif interface.mode=='TAGGED' %}
interface-mode trunk;
vlan {
members [ {% for vlan in interface['tagged_vlans'] %}{{ vlan['vid'] }} {% endfor %}];
members [ {% for vlan in interface.tagged_vlans %}{{ vlan.vid }} {% endfor %}];
}
{% elif interface['mode']=='TAGGED_ALL' %}
{% elif interface.mode=='TAGGED_ALL' %}
interface-mode trunk;
vlan {
members [ all ];
@ -146,33 +146,33 @@ chassis {
{% macro interfacesection(device,vlans) %}
interfaces {
{# Physical interfaces should be sorted by FPC/PIC/Port value - that's a challenge to do in Jinja so we cheat for now by putting the xe- interfaces first. This won't work for all devices! #}
{% for interface in device['interfaces'] if interface['name'].startswith('xe-') %}
{% for interface in device.interfaces.filter(name__startswith=='xe-') %}
{{ interfaceconfig(interface) }}
{% endfor %}
{% for interface in device['interfaces'] if interface['name'].startswith('et-') %}
{% for interface in device.interfaces.filter(name__startswith=='et-') %}
{{ interfaceconfig(interface) }}
{% endfor %}
{% for interface in device['interfaces'] if interface['name'].startswith('ae') %}
{{ interfaceconfig(interface,device['rack']['name'][5:]) }}
{% for interface in device.interfaces.filter(name__startswith=='ae') %}
{{ interfaceconfig(interface,device.rack.name[5:]) }}
{% endfor %}
{% for interface in device['interfaces'] if interface['name']=='em0' %}
{% for interface in device.interfaces.filter(name__exact=='em0') %}
{{ interfaceconfig(interface) }}
{% endfor %}
{% for interface in device['interfaces'] if interface['name'].startswith('irb') %}
{% for interface in device.interfaces.filter(name__startswith=='irb') %}
{% if loop.first %}
irb {
{% endif %}
unit {{ interface['name'][4:] }} {
description "{{ interface['description'] }}";
unit {{ interface.name[4:] }} {
description "{{ interface.description }}";
family inet {
{% if interface['ip_addresses'][0]['status']['name'] == 'Reserved' %}inactive: {% endif %}address {{ interface['ip_addresses'][0]['address'] }};
{% if interface.ip_addresses[0].status == 'reserved' %}inactive: {% endif %}address {{ interface.ip_addresses[0].address }};
}
}
{% if loop.last %}
}
{% endif %}
{% endfor %}
{% for interface in device['interfaces'] if interface['name'].startswith('lo') %}
{% for interface in device.interfaces.filter(name__startswith=='lo') %}
{{ interfaceconfig(interface) }}
{% endfor %}
}
@ -199,7 +199,7 @@ snmp {
{% macro routingoptionssection(primary_ip4, overlay_as, gateway) %}
routing-options {
router-id {{ primary_ip4['address'][:-3] }};
router-id {{ primary_ip4.address.ip }};
autonomous-system {{ overlay_as }};
{% if gateway %}
static {
@ -229,8 +229,8 @@ routing-options {
multiplier 3;
session-mode automatic;
}
{% for dst_switch in spines if dst_switch['primary_ip4']['address'] != device['primary_ip4']['address'] %}
neighbor {{ dst_switch['primary_ip4']['address'][:-3] }};
{% for dst_switch in spines if dst_switch.primary_ip4.address != device.primary_ip4.address %}
neighbor {{ dst_switch.primary_ip4.address.ip }};
{% endfor %}
}
{%- endmacro %}
@ -238,12 +238,12 @@ routing-options {
{% macro bgpoverlaygroup(device) %}
group OVERLAY {
type internal;
local-address {{ device['primary_ip4']['address'][:-3] }};
local-address {{ device.primary_ip4.address.ip }};
family evpn {
signaling;
}
{% if device['device_role']['name'] == 'Spine' %}
cluster {{ device['primary_ip4']['address'][:-3] }};
{% if device.role.name == 'Spine' %}
cluster {{ device.primary_ip4.address.ip }};
multipath;
{% endif %}
bfd-liveness-detection {
@ -251,8 +251,8 @@ routing-options {
multiplier 3;
session-mode automatic;
}
{% for dst_switch in device['peering_interfaces'] %}
neighbor {{ dst_switch['cable_peer_interface']['device']['primary_ip4']['address'][:-3] }};
{% for interface in device.interfaces.filter(cable__isnull=False) %}
neighbor {{ interface.connected_endpoints[0].primary_ip4.address.ip }};
{% endfor %}
}
{%- endmacro %}
@ -267,13 +267,13 @@ routing-options {
unicast;
}
export BGP_LOOPBACK0;
local-as {{ device['local_context_data']['underlay_as'] }};
local-as {{ device.local_context_data['underlay_as'] }};
multipath {
multiple-as;
}
{% for dst_switch in device['peering_interfaces'] %}
neighbor {{ dst_switch['cable_peer_interface']['ip_addresses'][0]['address'][:-3] }} {
peer-as {{ dst_switch['cable_peer_interface']['device']['local_context_data']['underlay_as'] }};
{% for interface in device.interfaces.filter(cable__isnull=False) %}
neighbor {{ interface.connected_endpoints[0].primary_ip4.address.ip }} {
peer-as {{ interface.connected_endpoints[0].local_context_data['underlay_as'] }};
}
{% endfor %}
}
@ -282,18 +282,14 @@ routing-options {
{% macro bgpsection(device,spines) %}
{% if device['device_role']['name'] in ['Spine','Lab-Spine'] %}
{% if device.role.name in ['Spine','Lab-Spine'] %}
{% set role='Spine' %}
{% set other_role='Leaf' %}
{% else %}
{% set role='Leaf' %}
{% set other_role='Spine' %}
{% endif %}
{% if device['device_role']['name'] == 'Lab-Spine' %}
{% set name=device['name'][4:] %}
{% else %}
{% set name=device['name'] %}
{% endif %}
{% set name=device.name %}
bgp {
{{ bgpoverlaygroup(device) }}
{% if role == 'Spine' %}
@ -320,17 +316,17 @@ routing-options {
{% macro vlanssection(vlans,device) %}
vlans {
{# This next line selects all of the VLANs which are configured on this device #}
{% for vlan in vlans if (device.interfaces.all()|selectattr('untagged_vlan.vid','equalto',vlan)|list|count or interfaces|map(attribute='tagged_vlans')|sum(start=[])|selectattr('vid','equalto',vlan)|list|count) or device.interfaces.all()|selectattr('name', 'equalto', 'irb.'+vlan['vid']|string)|list|count %}
vl{{ vlan['vid'] }} {
{% if vlan['name'] != '' %}
description "{{ vlan['name'] }}";
{% for vlan in vlans if (device.interfaces.all()|selectattr('untagged_vlan.vid','equalto',vlan)|list|count or interfaces|map(attribute='tagged_vlans')|sum(start=[])|selectattr('vid','equalto',vlan)|list|count) or device.interfaces.all()|selectattr('name', 'equalto', 'irb.'+vlan.vid|string)|list|count %}
vl{{ vlan.vid }} {
{% if vlan.name != '' %}
description "{{ vlan.name }}";
{% endif %}
vlan-id {{ vlan['vid'] }};
{% if device.interfaces.all()|selectattr('name', 'equalto', 'irb.'+vlan['vid']|string)|list|count %}
l3-interface irb.{{vlan['vid']}};
vlan-id {{ vlan.vid }};
{% if device.interfaces.all()|selectattr('name', 'equalto', 'irb.'+vlan.vid|string)|list|count %}
l3-interface irb.{{vlan.vid}};
{% endif %}
vxlan {
vni {{ vlan['vid'] }};
vni {{ vlan.vid }};
}
}
{% endfor %}

View File

@ -1,11 +1,11 @@
{% import "juniper-macros.j2" as junos %}
{{ junos.systemsection(device,root_pw,users,syslog_servers,ntp_servers) }}
{% if device.role['name'] == 'Leaf' %}
{% if device.role.name == 'Leaf' %}
{{ junos.chassissection(device,breakout_ports) }}
{% endif %}
{{ junos.interfacesection(device,ipam.VLAN.objects.filter(tenant=device.tenant)) }}
{{ junos.snmpsection(rack, snmp) }}
{% if device_role['name'] == 'Leaf' %}
{% if device.role.name == 'Leaf' %}
forwarding-options {
storm-control-profiles default {
all;
@ -17,7 +17,7 @@ policy-options {
term TERM1 {
from {
protocol direct;
route-filter {{ primary_ip4['address'] }} exact;
route-filter {{ device.primary_ip4.address }} exact;
}
then accept;
}
@ -31,7 +31,7 @@ policy-options {
{{ junos.routingoptionssection(primary_ip4, overlay_as, gateway) }}
protocols {
{{ junos.bgpsection(device,dcim.Device.objects.filter(tenant=device.tenant,role__name='Spine')) }}
{% if role['name'] == 'Leaf' %}
{% if device.role.name == 'Leaf' %}
evpn {
encapsulation vxlan;
extended-vni-list all;
@ -44,10 +44,10 @@ protocols {
{{ junos.sflowsection(sflow) }}
{% endif %}
}
{% if role['name'] == 'Leaf' %}
{% if device.role.name == 'Leaf' %}
switch-options {
vtep-source-interface lo0.0;
route-distinguisher {{ primary_ip4['address'][:-3] }}:1;
route-distinguisher {{ device.primary_ip4.address.ip }}:1;
vrf-target {
target:64512:1111;
auto;