-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic_napalm.py
85 lines (64 loc) · 2.58 KB
/
basic_napalm.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
#!/usr/bin/env python
"""
Source from Nick Russo Automating Networks with Python
Course on Pluralsight.
"""
from napalm import get_network_driver
from jinja2 import Environment, FileSystemLoader
from yaml import safe_load
from rich import print
import urllib3
from network_utils import address, mask
# Disable warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def main():
"""
Main execution of program
"""
with open("hosts.yaml", "r") as handle:
host_root = safe_load(handle)
for host in host_root["host_list"]:
print(f"Getting {host['platform']} driver:\n")
driver = get_network_driver(host["platform"])
conn = driver(
hostname=host["mgmt"], username=host["username"], password=host["password"]
)
conn.open()
facts = conn.get_facts()
print(facts)
print(f"\n{host['name']} model type: {facts['model']}")
# aoscx does not support config change in current napalm driver
if host["platform"] != "aoscx":
with open(f"vars/{host['name']}.yaml", "r") as handle:
ospf = safe_load(handle)
j2_env = Environment(
loader=FileSystemLoader("."), trim_blocks=True, autoescape=True
)
# https://www.kite.com/python/answers/how-to-call-a-function-in-a-jinja2-template-in-python
j2_env.globals["address"] = address
j2_env.globals["mask"] = mask
template = j2_env.get_template(
f"templates/basic/{host['platform']}_ospf.j2"
)
new_ospf_config = template.render(data=ospf)
print(f"\n[blue]Configuration to be loaded on {host['name']}:[/]\n")
print(new_ospf_config)
conn.load_merge_candidate(config=new_ospf_config)
diff = conn.compare_config()
if diff:
print(f"\n[red]The following diff was found on {host['name']}[/]\n")
print(diff)
else:
print(f"[green]No diff on {host['name']}; config up to date[/]\n")
# NAPALM backup config option
print(f"\nSaving backup for {host['name']} ...")
with open(f"napalm_backups/{host['name']}.conf", "w") as writer:
backup = conn.get_config("running")
writer.writelines(backup["running"])
print(f"\nBackup saved for {host['name']}")
else:
print("\n[red]Feature not yet supported[/]\n")
conn.close()
print("\n[green]Job complete[/]\n")
if __name__ == "__main__":
main()