-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot_delete_all_accounts.yaml
330 lines (329 loc) · 12.6 KB
/
bot_delete_all_accounts.yaml
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
---
- hosts: localhost
gather_facts: no
ignore_unreachable: yes
vars:
cml_servers: "{{ cml_server_list }}" # From POST
colab_email: "{{ colab_user_email }}" # From POST
colab_username: "{{ colab_user_username }}" # From POST
vcenter_address: "{{ vcenter_address }}" # From POST
room_id: "{{ colab_room_id }}" # From POST
cml_admin_account: "{{ cml_account }}" # From Vault
cml_admin_password: "{{ cml_password }}" # From Vault
aws_access_key: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}" # From Vault
aws_secret_key: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }}" # From Vault
aws_access_key_prod: "{{ aws_prod_key_vault }}" # From Vault
aws_secret_key_prod: "{{ aws_prod_token_vault }}" # From Vault
aws_region_prod: "{{ aws_prod_region_vault }}" # From Vault
wxt_bearer: "{{ wxt_bearer_vault }}" # From Vault
vcenter_admin_username: "{{ vcenter_admin_account_vault }}" # From Vault
vcenter_admin_password: "{{ vcenter_admin_password_vault }}" # From Vault
meraki_key: "{{ meraki_auth_vault }}" # From Vault
networkId: "{{ meraki_netid_vault }}" # From Vault
api_cml_authenticate: '/api/v0/authenticate'
api_cml_create_account: '/api/v0/users/'
api_cml_get_diagnostics: '/api/v0/diagnostics'
cml_results: []
message: ''
meraki_account_id: ""
tasks:
- name: Get Admin CML Tokens
uri:
url: "{{ item + api_cml_authenticate }}"
method: POST
return_content: yes
body: "{{ {'username': cml_admin_account, 'password': cml_admin_password} | to_json }}"
validate_certs: no
register: token_results
ignore_errors: yes
with_items: "{{ cml_servers }}"
- name: Get CML Diagnostics
uri:
url: "{{ item.item + api_cml_get_diagnostics }}"
method: GET
return_content: yes
body_format: json
validate_certs: no
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ item.json | default('') }}'}"
when: item.failed == false
register: diagnostics
ignore_errors: yes
with_items: "{{ token_results.results }}"
# - name: Create Lists of User's labs
# vars:
# cml_labs: []
# set_fact:
# cml_labs: "{{ cml_labs }} + [ { 'server': '{{ item.item.item }}', 'labs': {{ item.json.user_roles.labs_by_user[colab_username] | default([]) }}, 'admin_token': '{{ item.item.json }}' } ]"
# ignore_errors: yes
# when: item.item.failed == false
# loop: "{{ diagnostics.results }}"
# - name: Get User's CML Token
# uri:
# url: "{{ item + api_cml_authenticate }}"
# method: POST
# return_content: yes
# body: "{{ {'username': colab_username, 'password': colab_user_password} | to_json }}"
# validate_certs: no
# register: token_results_user
# ignore_errors: yes
# with_items: "{{ cml_servers }}"
# - name: Create User Token to Server Dictionary
# vars:
# user_token_server_dict: []
# set_fact:
# user_token_server_dict: "{{ user_token_server_dict | combine({item.item: item.json | default('') })}}"
# when: item.failed == false
# ignore_errors: yes
# with_items: "{{ token_results_user.results }}"
# - name: Create Full Dictionary with labs user token and admin token
# vars:
# complete_cml_labs: []
# set_fact:
# complete_cml_labs: "{{ complete_cml_labs }} + [ {{ item | combine({'user_token': user_token_server_dict[item.server] | default('missing')}) }} ]"
# ignore_errors: yes
# with_items: "{{ cml_labs }}"
# - name: Flatten results since haven't figured out loops in loops in ansible
# vars:
# flat_list_over_servers_labs_tokens: []
# set_fact:
# flat_list_over_servers_labs_tokens: "{{ flat_list_over_servers_labs_tokens }} + [ {{ {'lab': item.1, 'server': item.0.server, 'admin_token': item.0.admin_token, 'user_token': item.0.user_token } }} ]"
# ignore_errors: yes
# loop: "{{ complete_cml_labs | subelements('labs', 'skip_missing=True') }}"
- set_fact:
cml_labs: "{{ item.labs }}"
with_items: "{{ diagnostics.results[0].json.user_list }}"
when: item.username == colab_username
- name: Stop CML Labs
uri:
url: "{{ cml_servers[0] + '/api/v0/labs/' + item + '/stop' }}"
method: PUT
return_content: yes
body_format: json
validate_certs: no
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ token_results.results[0].json }}'}"
status_code: [ 200, 201, 204 ]
ignore_errors: yes
with_items: "{{ cml_labs }}"
- name: Wipe CML Labs
uri:
url: "{{ cml_servers[0] + '/api/v0/labs/' + item + '/wipe' }}"
method: PUT
return_content: yes
body_format: json
validate_certs: no
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ token_results.results[0].json }}'}"
status_code: [ 200, 201, 204 ]
ignore_errors: yes
with_items: "{{ cml_labs }}"
- name: Delete CML Labs
uri:
url: "{{ cml_servers[0] + '/api/v0/labs/' + item + '/wipe' }}"
method: DELETE
return_content: yes
body_format: json
validate_certs: no
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ token_results.results[0].json }}'}"
status_code: [ 200, 201, 204 ]
ignore_errors: yes
with_items: "{{ cml_labs }}"
- name: Get CML User ID
uri:
url: "{{ item.item + '/api/v0/users/' + colab_username + '/id' }}"
method: GET
return_content: yes
body_format: json
validate_certs: no
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ item.json }}'}"
when: item.failed == false
register: user_id_results
ignore_errors: yes
with_items: "{{ token_results.results }}"
- name: Delete CML Account
uri:
url: "{{ item.item + '/api/v0/users/' + user_id_results.results[0].json }}"
method: DELETE
return_content: yes
body_format: json
validate_certs: no
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ item.json | default('') }}'}"
status_code: [ 200, 201, 204 ]
ignore_errors: yes
register: cml_delete_results
with_items: "{{ token_results.results }}"
- name: Add Delete Success to CML results
vars:
temp_list: [{ 'server': '{{ item.item.item }}', 'status_code': '{{ item.status }}'}]
set_fact:
cml_results: "{{ cml_results + temp_list }}"
ignore_errors: yes
with_items: "{{ cml_delete_results.results }}"
- name: Generate CML Message Header
set_fact:
message: "{{ ' - CML\n' }}"
- name: Create Success messages for CML
set_fact:
message: "{{ message + ' - ' + item.server.replace('https://', '') + ' ' + 'Account Deleted! \n' }}"
check_flag: true
when: item.status_code == '204'
ignore_errors: yes
with_items: "{{ cml_results }}"
- name: Create Fail messages for CML
set_fact:
message: "{{ message + ' - ' + item.server.replace('https://', '') + ' ' + 'Failed: ' + item.status_code + ' \n' }}"
ignore_errors: yes
when: item.status_code != '204'
with_items: "{{ cml_results }}"
- name: Delete AWS Account
amazon.aws.iam_user:
name: "{{ colab_username }}"
state: absent
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
register: iam_user
ignore_errors: true
- name: Delete Success message for AWS
set_fact:
message: "{{ message + ' - AWS\n' + ' - ' + 'AWS Account Deleted! \n' }}"
check_flag: true
when: iam_user.changed == true and iam_user.failed == false # and did't fail
- name: Delete Fail messages for AWS
set_fact:
message: "{{ message + ' - AWS\n' + ' - ' + 'AWS Account Deletion Failed: ' + iam_user.msg + ' \n' }}"
when: iam_user.changed == false or iam_user.failed == false
ignore_errors: true
- hosts: localhost
connection: local
gather_facts: no
ignore_unreachable: yes
tasks:
- name: Delete user from vCenter
vcenter_user_admin:
user: "{{ colab_user_username }}"
vcenter_admin_username: "{{ vcenter_admin_username }}"
vcenter_admin_password: "{{ vcenter_admin_password }}"
vcenter_address: "{{ vcenter_address }}"
action: "delete"
no_log: False
register: vcenter_result
ignore_errors: true
- name: Create Success message for vCenter
set_fact:
message: "{{ hostvars[groups['login'][0]].message + ' - HX VMware\n' + ' - ' + vcenter_address + ' Account Deleted! \n' }}"
when: vcenter_result.changed == true
ignore_errors: true
- name: Create Fail message for vCenter
set_fact:
message: "{{ hostvars[groups['login'][0]].message + ' - HX VMware\n' + ' - ' + vcenter_address + ' Failed! \n' }}"
when: vcenter_result.failed == true or vcenter_result.changed == false
ignore_errors: true
- name: Get Meraki Users
uri:
url: "{{ 'https://api.meraki.com/api/v1/networks/' + networkId + '/merakiAuthUsers' }}"
method: GET
return_content: yes
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'X-Cisco-Meraki-API-Key': '{{ meraki_key }}'}"
validate_certs: no
register: users
ignore_errors: yes
- name: Get Meraki Account
set_fact:
meraki_account_id: "{{ item.id | default('') }}"
when: item.email == "{{ colab_user_username|lower + '@vpn.colab.ciscops.net' }}"
with_items: "{{ users.json }}"
ignore_errors: true
- name: Delete Meraki VPN Account
uri:
url: "{{ 'https://api.meraki.com/api/v1/networks/' + networkId + '/merakiAuthUsers/' + meraki_account_id|urlencode}}"
method: DELETE
follow_redirects: all
status_code: [ 204 ]
return_content: yes
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Cisco-Meraki-API-Key': '{{ meraki_key }}'}"
body_format: json
body: {}
validate_certs: no
register: meraki_delete_results
ignore_errors: yes
when: meraki_account_id != ""
- name: Delete Failure Message Due to no Account
set_fact:
message: "{{ message + ' - VPN\n' + ' - No account to delete \n' }}"
skip: true
when: meraki_account_id == ""
ignore_errors: yes
- name: Delete Success Message
set_fact:
message: "{{ message + ' - VPN\n' + ' - ' + 'VPN Account Deleted! \n' }}"
when: meraki_delete_results.status == 204 and skip is not defined
ignore_errors: yes
- name: Delete Failure Message
set_fact:
message: "{{ message + ' - VPN\n' + ' - Failed to delete \n' }}"
when: meraki_delete_results.status != 204 and skip is not defined
ignore_errors: yes
- name: Delete COLABOT Directory Record
update_dynamo_directory:
action: "delete"
aws_key: "{{ aws_prod_key_vault }}"
aws_secret: "{{ aws_prod_token_vault }}"
aws_region: "{{ aws_prod_region_vault }}"
dynamo_table: colab_directory
colab_email: "{{ colab_user_email }}"
ignore_errors: True
- name: Send WxT Message to User
uri:
url: "https://api.ciscospark.com/v1/messages"
method: POST
return_content: yes
body: "{{ {'toPersonEmail': colab_user_email, 'markdown': message } | to_json }}"
validate_certs: no
headers: "{{ {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + wxt_bearer } }}"
ignore_errors: True
- name: Add Username for WxT Space Message
set_fact:
message: "{{ 'Account deletion results for ' + colab_user_email + ': \n ' + message }}"
ignore_errors: yes
- name: Send WxT Message to WxT Space
uri:
url: "https://api.ciscospark.com/v1/messages"
method: POST
return_content: yes
body: "{{ {'roomId': colab_room_id, 'markdown': message } | to_json }}"
validate_certs: no
headers: "{{ {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + wxt_bearer } }}"
ignore_errors: true