forked from JulioPDX/auto_mpls_l3vpn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathl3vpn_deploy.py
95 lines (76 loc) · 2.43 KB
/
l3vpn_deploy.py
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
"""
Script used for MPLS L3VPN deployment using Nornir
"""
from nornir import InitNornir
from nornir_scrapli.tasks import send_configs
from nornir_jinja2.plugins.tasks import template_file
from nornir_napalm.plugins.tasks import napalm_get
from nornir_utils.plugins.tasks.files import write_file
from nornir_utils.plugins.functions import print_result
from net_utils import address, mask
def l3vpn(task):
"""
Main function built for L3VPN deployment tasks
"""
task1_result = task.run(
name=f"{task.host.name}: Creating VRFs Configuration",
task=template_file,
template="vrf.j2",
path="templates/",
data=task.host["vrfs"],
)
vrf_config = task1_result[0].result
task2_result = task.run(
name=f"{task.host.name}: Configuring VRFs on PE Nodes",
task=send_configs,
configs=vrf_config.split("\n"),
)
task3_result = task.run(
name=f"{task.host.name}: Create VRF to Interfaces Configuration",
task=template_file,
template=f"interfaces.j2",
path="templates/",
data=task.host.data,
address=address,
mask=mask,
)
int_config = task3_result[0].result
task4_result = task.run(
name=f"{task.host.name}: Configuring VRFs on Interfaces",
task=send_configs,
configs=int_config.split("\n"),
)
task5_result = task.run(
name=f"{task.host.name}: Create BGP Neighbor Configuration",
task=template_file,
template=f"bgp.j2",
path="templates/",
data=task.host.data,
)
bgp_config = task5_result[0].result
task6_result = task.run(
name=f"{task.host.name}: Configuring BGP Neighbors under VRFs",
task=send_configs,
configs=bgp_config.split("\n"),
)
# Backup because, why not?
# backup_result = task.run(
# name="Get Configuration", task=napalm_get, getters=["config"]
# )
# task.run(
# task=write_file,
# content=str(backup_result[0].result["config"]["running"]),
# filename=f"backups/{task.host.name}_CFG.txt",
# )
def main():
"""
Main that calls l3vpn function
"""
nornir = InitNornir(config_file="config.yaml")
print("Nornir initialized with the following hosts:\n")
for host in nornir.inventory.hosts.keys():
print(f"{host}\n")
result = nornir.run(task=l3vpn)
print_result(result)
if __name__ == "__main__":
main()