-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvm_inventory_file.py_old
109 lines (94 loc) · 3.82 KB
/
kvm_inventory_file.py_old
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
import openpyxl
import re
ip_plan = 'project_files\\Tele2_IP_plan_v2.01.xlsx'
inventory_file = 'c:\\temp\\inventory\\kvm'
mr = ['EKT']
# mr = ['SPB', 'MOS', 'ROS', 'NIN', 'EKT', 'NSK']
base_srv_types = ['pre', 'psm', 'pic', 'apic']
ext_srv_types = ['epsm', 'rb', 'log', 'rs']
wb = openpyxl.load_workbook(ip_plan, True)
def site_groups(region, site):
members = []
for srv_type in base_srv_types:
members.append('kvm_' + region.lower() + site + '_' + srv_type)
groups = {'name': '[kvm_' + region.lower() + site + ':children]', 'members': members}
return groups
def mr_groups(region, srv_type):
members = []
for i in ['1', '2']:
members.append('kvm_' + region.lower() + i + '_' + srv_type)
group = {'name':'[kvm_' + region.lower() + '_' + srv_type + ':children]', 'members': members}
return group
def global_group(srv_type):
members = []
for region in mr:
members.append('kvm_' + region.lower() + '_' + srv_type)
group = {'name': '[' + srv_type + ':children]', 'members': members}
return group
def vm_names(srv):
vm_name = []
for row in srv_list:
if row['hostname'] == srv and row['vlan'] == 'vm_Mgmt':
vm_name.append(row['vm'][:-18])
return vm_name
def rows_to_dict(region):
kvm_list = []
ws = wb[region]
for row in ws.iter_rows():
kvm_list.append({'hostname': row[0].value, 'vm': row[1].value,
'vlan': row[3].value, 'ip': row[5].value, 'site': row[6].value})
return kvm_list
with open(inventory_file, 'w', newline='\n') as f:
f.write('# List of KVMs\n\n')
for region in mr:
srv_list = rows_to_dict(region)
print('Collecting data about KVM in ' + region)
f.write('# ' + region + '\n')
for i in ['1','2']:
for srv_type in base_srv_types:
f.write('[kvm_' + region.lower() + i + '_' + srv_type + ']\n')
for row in srv_list:
vm = vm_names(row['hostname'])
if row['vlan'] == 'Host_Mgmt' and row['site'][-1] == i and re.search("^" + srv_type, vm[0]):
f.write(row['hostname'][:-13] + ' ansible_host=' + row['ip'] +
' vm_name=' + vm[0] + ' vm_list="' + str(vm) + '"\n')
f.write('\n')
for srv_type in ext_srv_types:
f.write('[kvm_' + region.lower() + i + '_' + srv_type + ']\n')
for row in srv_list:
vm = vm_names(row['hostname'])
if row['vlan'] == 'Host_Mgmt' and row['site'][-1] == i and str(vm).find(srv_type) != -1:
f.write(row['hostname'][:-13] + '\n')
f.write('\n')
group = site_groups(region, i)
f.write(group['name'] + '\n')
for member in group['members']:
f.write(member + '\n')
f.write('\n')
for srv_type in ext_srv_types:
group = mr_groups(region, srv_type)
f.write(group['name'] + '\n')
for member in group['members']:
f.write(member + '\n')
f.write('\n')
for srv_type in base_srv_types:
group = mr_groups(region, srv_type)
f.write(group['name'] + '\n')
for member in group['members']:
f.write(member + '\n')
f.write('\n')
f.write('\n# Global groups\n\n')
for srv_type in ext_srv_types:
group = global_group(srv_type)
f.write(group['name'] + '\n')
for member in group['members']:
f.write(member + '\n')
f.write('\n')
for srv_type in base_srv_types:
group = global_group(srv_type)
f.write(group['name'] + '\n')
for member in group['members']:
f.write(member + '\n')
f.write('\n')
wb.close()
print('Done')