aboutsummaryrefslogtreecommitdiffstats
path: root/examples/tg25/templates/juniper.j2
blob: 5787500128f80663ab76850bfa5d92d85cc61c32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{% include 'templates/juniper-global.j2' %}

{% set interfaces = [] %}
{% if device.virtual_chassis %}
    {% for vc_member in dcim.VirtualChassis.objects.get(id=device.virtual_chassis.id).members.all() %}
        {% for vc_interfaces in dcim.Interface.objects.filter(device_id=vc_member.id) %}
            {% do interfaces.append(vc_interfaces) %}
        {% endfor %}
    {% endfor %}
{% else %}
    {% set interfaces = device.interfaces.filter() %}
{% endif %}

{% set edge_interfaces = [] %}
{% for interface in interfaces if interface.type not in ["virtual", "lag"] %}
    {% if interface.mode == "access" %}
        {% do edge_interfaces.append(interface) %}
    {% endif %}
{% endfor %}

interfaces {
{% if edge_interfaces | length > 0  %}
    interface-range edge-ports {
        description "edge-ports";
    {% for interface in edge_interfaces %}
        member {{ interface.name }};
    {% endfor %}
    }
{% endif %}
{% for interface in interfaces %}
    {% if interface.type in ["virtual"] %}
    {% set interface_name_parts =  interface.name.split(".") %}
    {{ interface_name_parts[0] }} {
        unit {{ interface_name_parts[1] }} {
        {% if interface.description %}
            description "{{ interface.description }}";
        {% endif %}
        {% if interface.count_ipaddresses > 0 %}
            {% for ip in interface.ip_addresses.all() %}
                {% if ip.family == 4 %}
            family inet {
                filter {
                    input mgmt-v4;
                }
                {% elif ip.family == 6 %}
            family inet6 {
                filter {
                    input mgmt-v6;
                }
                {% endif %}
                address {{ ip.address }};
            }
            {% endfor %}
        {% endif %}
        }
    }
    {% elif interface.type in ["1000base-t", "lag", "10gbase-x-sfpp"] %}
    {{ interface.name }} {
    {% if interface.description %}
        description "{{ interface.description }}";
    {% endif %}
    {% if interface.type == "lag" %}
        aggregated-ether-options {
            lacp {
                active;
            }
        }
    {% endif %}
    {% if interface.lag and 'fap-interface' in interface.tags.slugs() and interface.connected_endpoints[0].device.status == "active" %}
        ether-options {
            802.3ad {{ interface.lag.name }};
        }
        inactive: unit 0 {
            family ethernet-switching {
                port-mode access;
                vlan {
                    members juniper-mgmt;
                }
            }
        }
    {% elif interface.lag and 'fap-interface' in interface.tags.slugs() and interface.connected_endpoints[0].device.status == "staged" %}
        inactive: ether-options {
            802.3ad {{ interface.lag.name }};
        }
        unit 0 {
            family ethernet-switching {
                port-mode access;
                vlan {
                    members juniper-mgmt;
                }
            }
        }
    {% elif interface.lag %}
        ether-options {
            802.3ad {{ interface.lag.name }};
        }
    {% else %}
        unit 0 {
        {% if interface.count_ipaddresses > 0 %}
            {% for ip in interface.ip_addresses.all() %}
                {% if ip.family == 4 %}
            family inet {
                filter {
                    input mgmt-v4;
                }
                {% elif ip.family == 6 %}
            family inet6 {
                filter {
                    input mgmt-v6;
                }
                {% endif %}
                address {{ ip.address }};
            }
            {% endfor %}
        {% elif interface.mode == "access" %}
            family ethernet-switching {
                port-mode access;
                vlan {
                    members {{ interface.untagged_vlan.name }};
                }
            }
        {% elif interface.mode == "tagged" or interface.mode == "tagged-all" %}
            family ethernet-switching {
                port-mode trunk;
                vlan {
            {% if interface.mode == "tagged-all" %}
                    members all;
            {% else %}
                    members [ {% for vlan in interface.tagged_vlans.all() %}{{ vlan.name }} {% endfor -%} ];
            {% endif %}
                }
                {% if interface.untagged_vlan %}
                native-vlan-id {{ interface.untagged_vlan.vid }};
                {% endif %}
            }
        {% endif %}
        }
    {% endif %}
    }
    {% else %}
    {% endif %}
{% endfor %}
}

ethernet-switching-options {
    secure-access-port {
        vlan juniper-mgmt {
            dhcp-option82 {
                circuit-id {
                    prefix hostname;
                    use-vlan-id;
                }
            }
        }
    }
}

{% set vlans = [] %}
{% for interface in interfaces %}
    {% for vlan in interface.tagged_vlans.all() %}
        {% if vlan not in vlans %}
            {% do vlans.append(vlan) %}
        {% endif %}
    {% endfor %}
    {% if interface.untagged_vlan and interface.untagged_vlan not in vlans %}
        {% do vlans.append(interface.untagged_vlan) %}
    {% endif %}
{% endfor %}
ethernet-switching-options {
    secure-access-port {
        vlan juniper-mgmt  {
            dhcp-option82 {
                circuit-id {
                    prefix hostname;
                    use-vlan-id;
                }
            }
        }
{% if device.role.slug == "access-switch" %}
        interface edge-ports {
            no-dhcp-trusted;
        }
    {% for vlan in vlans if not vlan.name == "juniper-mgmt" %} {# TOOD maybe also ignore wifi vlans #}
        vlan {{ vlan.name }} {
            arp-inspection;
            examine-dhcp;
            examine-dhcpv6;
            inactive: neighbor-discovery-inspection;
            ip-source-guard;
            ipv6-source-guard;
            dhcp-option82 {
                circuit-id {
                    use-vlan-id;
                }
            }
            no-option-37;
            /* inactive due to DHCP drops on MX platform */
            inactive: dhcpv6-option18 {
                use-option-82;
            }
        }
        {% endfor %}
        ipv6-source-guard-sessions {
            max-number 128;
        }
    {% endif %}
    }
    port-error-disable {
        /* 30 minutes in seconds */
        disable-timeout 1800;
    }
    storm-control {
    {% if device.role.slug == "access-switch" %}
        action-shutdown;
        interface edge-ports {
            bandwidth 20000;
            multicast;
        }
    {% else %}
        {% for interface in interfaces if interface.type == "lag" and interface.name != "ae0" %}
        interface {{ interface }}.0;
        {% endfor %}
    {% endif %}
    }
}
vlans {
{% for vlan in vlans %}
    {{ vlan.name }} {
        vlan-id {{ vlan.vid }};
    {% if vlan.name == "juniper-mgmt" %}
        l3-interface vlan.{{ vlan.vid }};
    {% endif %}
    }
{% endfor %}
}

{% if device.role.slug == "utskutt-distro" %}
event-options {
    {% for interface in interfaces if interface.type == "lag" and interface.name != "ae0" %}
        {% set fap_interface = dcim.Interface.objects.filter(lag_id=interface.id) %}
    policy {{ interface.name }}down {
        events snmp_trap_link_down;
        attributes-match {
            snmp_trap_link_down.interface-name matches "{{ interface.name }}$";
        }
        then {
            change-configuration {
                retry count 10 interval 10;
                commands {
                    "activate interfaces {{ fap_interface[0].name }} unit 0";
                    "deactivate interfaces {{ fap_interface[0].name }} ether-options";
                }
                user-name tech;
                commit-options {
                    log "Autoconfig-script: {{ interface.name }}went down so removed {{ fap_interface[0].name }} from bundle";
                }
            }
        }
    }
    policy {{ interface.name }}up {
        events snmp_trap_link_up;
        attributes-match {
            snmp_trap_link_up.interface-name matches "{{ interface.name }}$";
        }
        then {
            change-configuration {
                retry count 10 interval 10;
                commands {
                    "deactivate interfaces {{ fap_interface[0].name }} unit 0";
                    "activate interfaces {{ fap_interface[0].name }} ether-options";
                }
                user-name tech;
                commit-options {
                    log "Autoconfig-script: {{ interface.name }} came up so added {{ fap_interface[0].name }} to bundle";
                }
            }
        }
    }
    {% endfor %}
}
{% endif %}