-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_base_accounts_and_reset_passwords.yaml
303 lines (300 loc) · 12.1 KB
/
add_base_accounts_and_reset_passwords.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
---
- 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
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
colabot_cipher: "{{ cipher_vault }}" # From Vault
iam_group: "{{ aws_iam_group_vault }}" # From Vault
wxt_bearer: "{{ wxt_bearer_vault }}" # From Vault
vcenter_user_group: "{{ vcenter_sso_group_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
cml_results: []
check_flag: false
aws_potential_reset_flag: false
api_cml_authenticate: '/api/v0/authenticate'
api_cml_create_account: '/api/v0/users/'
message: ''
meraki_account_id: ""
tasks:
- name: Generate Initial Password
generate_password:
register: results
- name: Save initial password
set_fact:
colab_user_password_init: "{{ results.meta }}"
- name: Generate Final Password
generate_password:
register: results
- name: Save final password
set_fact:
colab_user_password_final: "{{ results.meta }}"
- name: Get 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: Add failures to CML results
set_fact:
cml_results: "{{ cml_results }} + [ { 'server': '{{ item.item }}', 'status_code': '{{ item.status }}' }]"
when: item.failed == true
ignore_errors: yes
with_items: "{{ token_results.results }}"
- name: Create CML Accounts
uri:
url: "{{ item.item + api_cml_create_account }}"
method: POST
return_content: yes
body: "{{ {'username': colab_username, 'fullname': colab_email, 'description': '', 'email': colab_email, 'admin':false, 'groups': [], 'password': colab_user_password_init } | to_json }}"
validate_certs: no
status_code: [ 200, 201 ]
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'cache-control': 'no-cache',
'Authorization': 'Bearer {{ item.json }}'}"
when: item.failed == false
register: create_results
ignore_errors: yes
with_items: "{{ token_results.results }}"
- 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: id_results
ignore_errors: yes
with_items: "{{ token_results.results }}"
- name: Reset CML Passwords
uri:
url: "{{ item.item + '/api/v0/users/' + id_results.results[0].json }}"
method: PATCH
return_content: yes
body_format: json
body: "{{ {'username': colab_username, 'fullname': colab_email, 'description': '', 'email': colab_email, 'admin':false, 'groups': [], 'password': { 'old_password': colab_user_password_init, 'new_password': colab_user_password_final }} | to_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: create_results
ignore_errors: yes
with_items: "{{ token_results.results }}"
- name: Add CML Change Password failures to CML results
vars:
temp_list: [{ 'server': '{{ item.item.item }}', 'status_code': '{{ item.status }}' }]
set_fact:
cml_results: "{{ cml_results + temp_list }}"
when: item.get('failed') == true
ignore_errors: yes
with_items: "{{ create_results.results }}"
- name: Add CML Change Password Success to CML results
vars:
temp_list: [{ 'server': '{{ item.item.item }}', 'status_code': '{{ item.status }}' }]
set_fact:
cml_results: "{{ cml_results + temp_list }}"
when: item.get('failed') == false
ignore_errors: yes
with_items: "{{ create_results.results }}"
- name: Generate CML Message Header
set_fact:
message: "{{ ' - CML username is ' + colab_user_username + '\n' }}"
- name: Create Success messages for CML
set_fact:
message: "{{ message + ' - ' + item.server.replace('https://', '') + ' ' + 'Success! \n' }}"
check_flag: true
when: item.status_code == '200'
with_items: "{{ cml_results }}"
- name: Create Fail messages for CML
set_fact:
message: "{{ message + ' - ' + item.server.replace('https://', '') + ' ' + 'Failed: ' + item.status_code + ' \n' }}"
when: item.status_code != '200'
with_items: "{{ cml_results }}"
- name: Check for AWS IAM user
aws_check_for_user:
aws_key: "{{ aws_access_key }}"
aws_secret: "{{ aws_secret_key }}"
iam_user: "{{ colab_username }}"
register: check_iam_result
ignore_errors: true
- ansible.builtin.debug:
msg:
- "{{ check_iam_result }}"
- name: Reset AWS Password
amazon.aws.iam_user:
name: "{{ colab_username }}"
state: present
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
password: "{{ colab_user_password_final }}"
update_password: always
purge_tags: false
tags:
email: "{{ colab_user_email }}"
register: iam_user_reset
ignore_errors: true
when: check_iam_result.failed == false and check_iam_result.user_present == true
- name: Get AWS Account number
set_fact:
aws_account_number: "{{ iam_user_reset.iam_user.user.arn.replace('arn:aws:iam::', '').replace(':user/' + colab_username, '') }}"
when: iam_user_reset.failed == false
ignore_errors: true
- name: Create Reset Success message for AWS
set_fact:
message: "{{ message + ' - AWS username is ' + colab_user_username + '\n' + ' - https://' + aws_account_number + '.signin.aws.amazon.com/console Success! \n' }}"
when: iam_user_reset.failed == false and iam_user_reset.changed == true
ignore_errors: true
- hosts: localhost
connection: local
gather_facts: no
ignore_unreachable: yes
tasks:
- name: Add user and/or reset passord in vCenter 6.7
vcenter_user_admin:
user: "{{ colab_user_username }}"
user_password_init: "{{ colab_user_password_init }}"
user_password_final: "{{ colab_user_password_final }}"
vcenter_user_group: "{{ vcenter_user_group }}"
vcenter_admin_username: "{{ vcenter_admin_username }}"
vcenter_admin_password: "{{ vcenter_admin_password }}"
vcenter_address: "{{ vcenter_address }}"
action: "create_or_reset"
no_log: False
ignore_errors: True
- pause:
seconds: 5
- hosts: localhost
connection: local
gather_facts: no
ignore_unreachable: yes
tasks:
- name: Add user and/or reset passord in vCenter 6.7
vcenter_user_admin:
user: "{{ colab_user_username }}"
user_password_init: "{{ colab_user_password_init }}"
user_password_final: "{{ colab_user_password_final }}"
vcenter_user_group: "{{ vcenter_user_group }}"
vcenter_admin_username: "{{ vcenter_admin_username }}"
vcenter_admin_password: "{{ vcenter_admin_password }}"
vcenter_address: "{{ vcenter_address }}"
action: "create_or_reset"
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 username is ' + colab_user_username + '@ciscops.net\n' + ' - ' + vcenter_address + ' Success! \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 username is ' + colab_user_username + '@ciscops.net\n' + ' - ' + vcenter_address + ' Failed! \n' }}"
when: 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 }}"
when: item.email == "{{ colab_user_username|lower + '@vpn.colab.ciscops.net' }}"
with_items: "{{ users.json }}"
ignore_errors: true
- name: Reset Meraki VPN Password
uri:
url: "{{ 'https://api.meraki.com/api/v1/networks/' + networkId + '/merakiAuthUsers/' + meraki_account_id|urlencode}}"
method: PUT
follow_redirects: all
status_code: [ 200, 201 ]
return_content: yes
headers: "{
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Cisco-Meraki-API-Key': '{{ meraki_key }}'}"
body_format: json
body: "{{ {'password': colab_user_password_final} | to_json }}"
validate_certs: no
register: meraki_reset_password_results
ignore_errors: yes
when: meraki_account_id != ""
- name: Reset Success Message
set_fact:
message: "{{ message + ' - VPN username is ' + colab_user_username + '@vpn.colab.ciscops.net\n' + ' - VPN server: cpn-vpn-mkdqgqzprv.dynamic-m.com ' + 'Success! \n' }}"
when: meraki_reset_password_results.status == 200
ignore_errors: yes
- name: Encrypt string
encrypt_or_decrypt_string:
action: "encrypt"
message: "{{ colab_user_password_final }}"
key: "{{ cipher_vault }}"
register: encrypted_string
- name: Update COLABOT Directory
update_dynamo_directory:
action: "update"
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 }}"
colab_username: "{{ colab_user_username }}"
colab_password: "{{ encrypted_string.meta }}"
ignore_errors: True
- name: Send WxT Message
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 } }}"
- name: Send Password
when: check_flag == true
uri:
url: "https://api.ciscospark.com/v1/messages"
method: POST
return_content: yes
body: "{{ {'toPersonEmail': colab_user_email, 'markdown': 'Your COLAB password: ' + colab_user_password_final } | to_json }}"
validate_certs: no
headers: "{{ {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + wxt_bearer } }}"