-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosts.py
127 lines (101 loc) · 3.74 KB
/
hosts.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
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
# This class will hold all the information needed to configure any host
import ipaddress as ip
import inventory
class Host:
def __init__(self, host_data = None, ip_data=None):
if not host_data is None:
self._readin_host_data(host_data)
if not ip_data is None:
self._readin_ip_data(ip_data)
def _readin_host_data(self, data):
self._data = {}
self._data["interfaces"] = {}
self._data["hostrole"] = ""
if data["primary_ip"]:
self._data["ansible_host"] = str(ip.ip_interface(data["primary_ip"]["address"]).ip)
#self._data["ansible_host"] = data["primary_ip"]["address"]
for k in data.keys():
self._data[k] = data[k]
if 'device_role' in self._data.keys():
self._data['type'] = 'device'
else:
self._data['type'] = 'virtual_machine'
if self.data["primary_ip"]:
self._data["primary_ip"] = str(ip.ip_interface(data["primary_ip"]["address"]).ip)
if self.data["primary_ip4"]:
self._data["primary_ip4"] = str(ip.ip_interface(data["primary_ip4"]["address"]).ip)
if self.data["primary_ip6"]:
self._data["primary_ip6"] = str(ip.ip_interface(data["primary_ip6"]["address"]).ip)
self._get_role()
def _get_role(self):
role_id = 0
if self.type == "device" and self.device_role:
role_id = self.device_role["id"]
elif self.role:
role_id = self.role["id"]
if role_id != 0:
self._data["hostrole"] = inventory.get_role_data(role_id)["name"]
else:
self._data["hostrole"] = "ungrouped"
def _readin_ip_data(self, data):
for ip in data:
if not ip["interface"] is None:
if not ip["interface"][self.type] is None:
if ip["interface"][self.type]["id"] == self.id:
interface_name = ip["interface"]["name"]
if not interface_name in self.interfaces.keys():
self.interfaces[interface_name] = Interface(inventory.get_interface_data(ip["interface"]["id"]))
ip_struct = IP(ip)
if (ip_struct.family == 4):
if not self.primary_ip4 is None:
if self.primary_ip4 == str(ip_struct.ip.ip):
ip_struct.primary = True
else:
if not self.primary_ip6 is None:
if self.primary_ip6 == str(ip_struct.ip.ip):
ip_struct.primary = True
self.interfaces[interface_name].add_ip(ip_struct)
def __getattr__(self, key):
if key == "data":
return self._data
return self._data.get(key)
def __str__(self):
to_print = "{ Host %s\n" % self.name
to_print += "\tType: %s \n" % self.type
to_print += "\tTags: %s \n" % self.tags
to_print += "\tRole: %s \n" % self.hostrole
if (len(self.interfaces) > 0):
to_print += "\tInterfaces:\n"
for i in self.interfaces:
to_print += "\t\t%s\t%s\t%s\n" % (i.name, i.ip.ip, i.primary)
to_print += "}"
return to_print
class IP:
def __init__(self, ip_data):
self.family = ip_data["family"]["value"]
self.primary = False
if ip_data["dns_name"]:
self.dns_name = ip_data["dns_name"]
if (self.family == 4):
self.ip = ip.IPv4Interface(ip_data["address"])
elif (self.family == 6):
self.ip = ip.IPv6Interface(ip_data["address"])
class Interface:
def __init__(self, interface_data):
self.name = interface_data["name"]
self.enabled = interface_data["enabled"]
if interface_data["mode"]:
self.vlan_mode = interface_data["mode"]["label"]
if interface_data["untagged_vlan"]:
self.vlan_untagged_id = interface_data["untagged_vlan"]["vid"]
self.vlan_untagged_name = interface_data["untagged_vlan"]["name"]
if interface_data["tagged_vlans"]:
self.vlans_tagged = [{"id": i["vid"], "name": i["name"]} for i in interface_data["tagged_vlans"]]
if interface_data["tags"]:
self.tags = interface_data["tags"]
if interface_data["mac_address"]:
self.mac_address = interface_data["mac_address"]
self.netbox_id = interface_data["id"]
self.ips = []
def add_ip(self, ip):
self.ips.append(ip)