-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddto_sevone.yml
358 lines (303 loc) · 9.39 KB
/
addto_sevone.yml
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
--
# Purpose: This playbook creates a post connection to login to an API server, lists out a list of server devices, then adds a new server to sevone.
- name: Make a POST connection verify connection succeeds.
hosts: all
gather_facts: no
vars:
#- hostname: hostname
#- ipAddress: ipaddress
#- cmdbid: cmdbid
#- vdclocation: vdclocation
##- tier: tier
#- os: os
#- fqdn: fqdn
#- submission: submission
#- agency: agency
# Purpose: The play must match one of the tiers, or it ends. The play
tasks:
- name: end play if nothing to upgrade
meta: end_play
when: tier == "Semi-Managed"
- name: Check that you can POST to login to API server
uri:
url: http://204.1.120.1:80/api/v2/authentication/signin?nmsLogin=false
method: POST
body: { name: "myUserName", password: "myPassword"}
body_format: json
validate_certs: no
register: login
delegate_to: localhost
- debug: var=login
- name: set fact - someLogin
set_fact:
someLogin: "{{ login.json.token }}"
- name: Get list of peers
uri:
url: http://204.1.120.1:80/api/v2/peers
method: GET
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
register: peersList
delegate_to: localhost
#- debug: var=peersList
- name: print the vdc location
debug:
msg: "{{vdclocation}}"
delegate_to: localhost
- name: print the vdc location
debug:
msg: "{{hostname}}"
delegate_to: localhost
- name: grab list of filtered peers
set_fact:
peers: "{{ peersList.json.content | selectattr('name','contains', vdclocation ) | rejectattr('model','equalto','DNC' ) | rejectattr('name','equalto','NSOSDCS600-TXD') | rejectattr('name','equalto','NSOADCS600-TXD' ) | list }}"
delegate_to: localhost
- debug: var=peers
- name: loop through peers list to get the serverLoad
set_fact:
peersServerLoad: "{{ peers | map(attribute='serverLoad') | list }}"
delegate_to: localhost
- debug: var=peersServerLoad
- name: loop through peers list to get the flowLoad
set_fact:
peersFlowLoad: "{{ peers | map(attribute='flowLoad') | list }}"
delegate_to: localhost
- debug: var=peersFlowLoad
- name: Initiate peers top List array
set_fact:
combinedvalues: []
delegate_to: localhost
- name: Add new JSON Objects to List
set_fact:
combinedvalues: "{{ combinedvalues + [{ 'total': (item.serverLoad + item.flowLoad), 'capacity': item.capacity, 'id': item.serverId }] }}"
loop: "{{ peers }}"
delegate_to: localhost
- debug: var=combinedvalues
- name: Initiate peers into an array
set_fact:
utilization: []
delegate_to: localhost
- name: Add new JSON Objects List array
set_fact:
utilization: "{{ utilization + [{ 'total': (item.total / item.capacity), 'id': item.id }] }}"
loop: "{{ combinedvalues }}"
delegate_to: localhost
- debug: var=utilization
- name: filter to lowest utilized
set_fact:
lowestUtilized: "{{ (utilization | sort(attribute='total'))[0]['id'] }}"
delegate_to: localhost
- debug: var=lowestUtilized
- name: Create new device
uri:
url: http://204.1.120.1:80/api/v2/devices
method: POST
status_code: 201
headers:
X-AUTH-TOKEN: "{{ sometLogin }}"
validate_certs: no
body: {
name: "{{ fqdn }}",
alternateName: "{{ cmdbid }}",
ipAddress: "{{ ipAddress }}",
peerId: "{{ lowestUtilized }}"
}
body_format: json
delegate_to: localhost
register: device
- name: set fact - deviceid
set_fact:
deviceid: "{{ device.json.id }}"
delegate_to: localhost
- name: Get group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/filter?size=1000
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
body: { name: "{{ agency}}", pageSize: 1000 }
body_format: json
delegate_to: localhost
register: groupsJson
- name: set fact - groupsJsonContent
set_fact:
groupList: "{{ groupsJson.json.content }}"
delegate_to: localhost
# - debug: var=groupList
- name: Grab name in the content array through list of groups
set_fact:
ParsedList: "{{ groupList | selectattr('name','equalto', agency ) | list }}"
delegate_to: localhost
# - debug: var=ParsedList
- name: set fact - groupid
set_fact:
groupid: "{{ ParsedList[0].id }}"
delegate_to: localhost
- debug: var=groupid
- name: Add to group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/{{ groupid }}/members/{{ deviceid }}
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
delegate_to: localhost
register: groupsAddResult
- debug: var=groupsAddResult
- name: set DC for ADC
set_fact:
groupName: "Data Center-A)"
when: vdclocation == 'DCA'
delegate_to: localhost
# - debug: var='vdclocation'
- name: set DC for SDC
set_fact:
groupName: "Data Center-B"
when: vdclocation == 'DCB'
delegate_to: localhost
# - debug: var=groupName
- name: Get group
uri:
url: http://204.67.120.25:80/api/v2/devicegroups/filter?size=1000
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
body: {
name: "{{ groupName }}",
pageSize: 1000
}
body_format: json
register: groupsJson
delegate_to: localhost
# - debug: var=groupsJson
- name: set fact - groupsJsonContent
set_fact:
groupList: "{{ groupsJson.json.content }}"
delegate_to: localhost
# - debug: var=groupList
- name: Grab name in the content array through list of groups
set_fact:
ParsedList: "{{ groupList | selectattr('name','equalto', groupName ) | list }}"
delegate_to: localhost
# - debug: var=ParsedList
- name: set fact - groupid
set_fact:
groupid: "{{ ParsedList[0].id }}"
delegate_to: localhost
- debug: var=groupid
- name: Add to group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/{{ groupid }}/members/{{ deviceid }}
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
register: groupsAddResult
delegate_to: localhost
- debug: var=groupsAddResult
- name: set os group name windows
set_fact:
groupName: "TPC {{ tier }} Windows"
when: os == 'Windows' and submission == 'source system'
delegate_to: localhost
- debug: var=groupname
- name: set os group name linux
set_fact:
groupName: "TPC {{ tier }} Linux"
when: os != 'Windows' and submission == 'source system'
delegate_to: localhost
#will be used for all group gets
- name: Get group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/filter?size=1000
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
body: {
name: "{{ groupName }}",
pageSize: 1000
}
body_format: json
delegate_to: localhost
register: groupsJson
- name: set fact - groupsJsonContent
set_fact:
groupList: "{{ groupsJson.json.content }}"
delegate_to: localhost
# - debug: var=groupList
- name: Grab name in the content array through list of groups
set_fact:
ParsedList: "{{ groupList | selectattr('name','equalto', groupName ) | list }}"
delegate_to: localhost
# - debug: var=ParsedList
- name: set fact - groupid
set_fact:
groupid: "{{ ParsedList[0].id }}"
delegate_to: localhost
- debug: var=groupid
- name: Add to group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/{{ groupid }}/members/{{ deviceid }}
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
register: groupsAddResult
delegate_to: localhost
- debug: var=groupsAddResult
- name: This is being assembled.
set_fact:
groupName: "TPC {{tier}} - Being Assembled"
when: submission == 'Internal'
delegate_to: localhost
- name: This is not being assembled.
set_fact:
groupName: "TPC {{tier}}"
when: submission == 'source system'
delegate_to: localhost
#will be used for all group gets
- name: Get group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/filter?size=1000
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
body: {
name: "{{ groupName }}",
pageSize: 1000
}
body_format: json
delegate_to: localhost
register: groupsJson
- name: set fact - groupsJsonContent
set_fact:
groupList: "{{ groupsJson.json.content }}"
delegate_to: localhost
# - debug: var=groupList
- name: Grab name in the content array through list of groups
set_fact:
ParsedList: "{{ groupList | selectattr('name','equalto', groupName ) | list }}"
delegate_to: localhost
# - debug: var=ParsedList
- name: set fact - groupid
set_fact:
groupid: "{{ ParsedList[0].id }}"
delegate_to: localhost
- debug: var=groupid
# Add new server to SevOne
- name: Add to group
uri:
url: http://204.1.120.1:80/api/v2/devicegroups/{{ groupid }}/members/{{ deviceid }}
method: POST
headers:
X-AUTH-TOKEN: "{{ someLogin }}"
validate_certs: no
register: groupsAddResult
delegate_to: localhost
- debug: var=groupsAddResult
# end of group get