-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_inventory.py
32 lines (26 loc) · 1.03 KB
/
custom_inventory.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
from nornir.core.deserializer.inventory import Inventory
import pandas as pd
class CustomInventory(Inventory):
def __init__(self, **kwargs):
hosts = self.get_file_content(kwargs["filename"])
groups = {}
defaults = {}
super().__init__(hosts=hosts, groups=groups, defaults=defaults, **kwargs)
def get_file_content(self, filename: str):
# df : This host format can be used in netmiko directly.
if ".csv" in filename.lower():
df = pd.read_csv(filename)[['host', 'device_type', 'device_name']].to_dict(
orient='records')
else:
df = pd.read_excel(filename)[['host', 'device_type', 'device_name']].to_dict(
orient='records')
hosts = {}
for item in df:
host = {
item["device_name"]: {
'hostname': item["host"],
'platform': item["device_type"]
}
}
hosts.update(host)
return hosts