-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_vars.py
191 lines (168 loc) · 6.29 KB
/
group_vars.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import openpyxl
import yaml
import ipaddress
import copy
import os
import re
from shared import sig_int
sig_int_file = 'project_files/Tele2_TMS_Signal_integration_v5.7.xlsx'
vars_dir = 'c:/temp/group_vars/'
# Open Excel file in read-only mode
wb = openpyxl.load_workbook(sig_int_file, True)
mr = ['SPB']
#mr = ['MOS', 'NIN', 'NSK', 'EKT']
#mr = ['spb', 'nin', 'ekt', 'nsk', 'ros', 'mos']
#mr = ['nin', 'ekt', 'nsk', 'mos']
sigs = ['radius', 'gx', 'gy']
def make_var_dirs(dir_name):
if dir_name not in os.listdir(vars_dir):
os.mkdir(vars_dir + dir_name)
dir = f'{vars_dir}{dir_name}'
return dir
def filter_net_list(wsheet, sig, nets):
fltr = f'{wsheet.lower()}_s\d_{sig}'
filtered_nets = { key:value for (key, value) in nets.items() if re.match(fltr, key) }
return filtered_nets
def summarise_nets(range): # Summarise addresses into /24 subnets
supernets = set()
for ip in range:
supernets.add(str(ipaddress.IPv4Network(ip).supernet(new_prefix=24)))
supernets = list(set(supernets))
return supernets
def check_site_of_net(net, region, sig):
for addr in signal_networks[region][sig]:
if ipaddress.IPv4Network(addr['ip']).subnet_of(ipaddress.IPv4Network(net)):
site = addr['site']
return site
def get_psm_static_route(sig, nets): # Signal Integration dependent part of routes
s_routes = []
next_hops = {
'radius': {
1: '{{ rb01_vip }}',
2: '{{ rb02_vip }}'
},
'gx': '{{ networks[net_scope]["Gx1" if inventory_hostname_short[-2:] | int is odd else "Gx2"].gw }}',
'gy': '{{ networks[net_scope]["Gy1" if inventory_hostname_short[-2:] | int is odd else "Gy2"].gw }}'
}
interfaces = {
'radius': "Radius",
'gx': "Gx",
'gy': "Gy"
}
for key, value in nets.items():
if sig == 'radius':
site = int(re.search('_s\d', key).group(0)[-1])
for net in value:
sr = {
'route': net,
'next_hop': next_hops['radius'][site],
'interface': interfaces['radius']
}
if s_routes.count(sr) == 0:
s_routes.append(sr)
else:
for net in value:
sr = {
'route': net,
'next_hop': next_hops[sig],
'interface': interfaces[sig]
}
if s_routes.count(sr) == 0:
s_routes.append(sr)
return s_routes
def get_psm_const_sr():
# Signal Integration independent part. Constant for all regions
static_routes = []
sr = {
'route': "{{ networks[other_site_net_scope].Provisioning.subnet + networks[other_site_net_scope].Provisioning.prefix }}",
'next_hop': "{{ networks[net_scope].Provisioning.gw }}",
'interface': "Provisioning"
}
static_routes.append(sr)
sr = {
'route': "{{ networks[other_site_net_scope].ClusterSync.subnet + networks[other_site_net_scope].ClusterSync.prefix }}",
'next_hop': "{{ networks[net_scope].ClusterSync.gw }}",
'interface': "ClusterSync"
}
static_routes.append(sr)
return static_routes
def get_rb_static_routes(nets):
s_routes = []
for key, value in nets.items():
for net in value:
sr = {
'route': net,
'next_hop': "{{ networks[net_scope].RadiusFE.gw }}",
'interface': "enp3s0"
}
if s_routes.count(sr) == 0:
s_routes.append(sr)
return s_routes
def get_epsm_static_routes(nets):
s_routes = []
for key, value in nets.items():
for net in value:
sr = {
'route': net,
'next_hop': "{{ networks[net_scope].Radius.gw }}",
'interface': "enp2s0"
}
if s_routes.count(sr) == 0:
s_routes.append(sr)
sr = {
'route': "{{ networks[other_site_net_scope].Provisioning.subnet + networks[other_site_net_scope].Provisioning.prefix }}",
'next_hop': "{{ networks[net_scope].Provisioning.gw }}",
'interface': "enp3s0"
}
s_routes.append(sr)
return s_routes
# Getting Signal Interation subnets
signal_networks = sig_int.get_sig_nets()
# Get summarized subnets
sum_networks = copy.deepcopy(signal_networks)
keys = list(sum_networks.keys())
for key in keys:
nets = summarise_nets(sum_networks[key])
nets.sort()
sum_networks[key] = nets
# Creates PSM group vars files with static routes
for region in mr:
wsheets = sig_int.get_sheets_list(region)
for ws in wsheets:
psm_sr = []
for sig in sigs:
nets = filter_net_list(ws, sig, sum_networks)
psm_sr.extend(get_psm_static_route(sig, nets))
psm_sr.extend(get_psm_const_sr())
dir = make_var_dirs(f'pl_{ws.lower()}_psm')
print(f'Generating file for for PSMs in {ws}...', end='')
with open(f'{dir}/static_routes.yml', 'w', newline='\n') as file:
yaml.dump({'static_routes': psm_sr}, file, width=120, sort_keys=False)
print(' Done')
print('')
# Create RB group vars files with static routes
for region in mr:
wsheets = list(filter(lambda i: re.match(region, i, re.I), wb.sheetnames))
for ws in wsheets:
sig = 'radius'
nets = filter_net_list(ws, sig, sum_networks)
rb_sr = get_rb_static_routes(nets)
dir = make_var_dirs(f'oth_{ws.lower()}_rb')
print(f'Generating file for for RBs in {ws}...', end='')
with open(f'{dir}/static_routes.yml', 'w', newline='\n') as file:
yaml.dump({'static_routes': rb_sr}, file, width=120, sort_keys=False)
print(' Done')
print('')
# Create ePSM group vars files with static routes
for region in mr:
wsheets = list(filter(lambda i: re.match(region, i, re.I), wb.sheetnames))
for ws in wsheets:
sig = 'radius'
nets = filter_net_list(ws, sig, sum_networks)
rb_sr = get_epsm_static_routes(nets)
dir = make_var_dirs(f'oth_{ws.lower()}_epsm')
print(f'Generating file for for EPSMs in {ws}...', end='')
with open(f'{dir}/static_routes.yml', 'w', newline='\n') as file:
yaml.dump({'static_routes': rb_sr}, file, width=120, sort_keys=False)
print(' Done')
print('')