forked from IBMSpectrumComputing/lsf-hybrid-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cleanup.yml
335 lines (298 loc) · 9.63 KB
/
Cleanup.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
#
#-----------------------------------
# Copyright IBM Corp. 1992, 2017. All rights reserved.
# US Government Users Restricted Rights - Use, duplication or disclosure
# restricted by GSA ADP Schedule Contract with IBM Corp.
#-----------------------------------
#
---
- name: Delete the EC2 Instances
hosts: localhost
gather_facts: False
vars_prompt:
- name: user_del_vpc
prompt: "\n**************************** Input Needed ****************************\n\nDo you want to delete the VPC [y/n] ?"
default: "n"
private: no
tasks:
- include_vars: "AWS/AWS-config.yml"
- set_fact:
del_vpc: "{{ user_del_vpc }}"
- name: Check for inventory file
stat:
path: "{{ RC_PLAY_TOP }}/inventory_ec2servers.yml"
register: have_inv
- name: Reload EC2 server inventory
include_vars: "{{ RC_PLAY_TOP }}/inventory_ec2servers.yml"
when: have_inv.stat.exists == True
- name: Make a hostgroup from EC2 Servers
add_host:
hostname: "{{ item.pub_ip }}"
groupname: ec2servers
with_items: "{{ ec2servers }}"
when: have_inv.stat.exists == True
- name: Check for inventory file
stat:
path: "{{ RC_PLAY_TOP }}/inventory_ec2.yml"
register: have_vpninv
- name: Reload EC2 VPN inventory
include_vars: "{{ RC_PLAY_TOP }}/inventory_ec2.yml"
when: have_vpninv.stat.exists == True
- name: Make a hostgroup from the EC2 VPN node
add_host:
hostname: "{{ item.pub_ip }}"
groupname: ec2vpn
with_items: "{{ ec2vpn }}"
when: have_vpninv.stat.exists == True
- name: Delete Server Instances
ec2:
state: absent
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
key_name: "{{ AWS_Key_Name }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
instance_ids: "{{ item.awsid }}"
with_items: "{{ ec2servers }}"
ignore_errors: True
when: have_inv.stat.exists == True
- name: Delete VPN Instance (Okay to fail)
ec2:
state: absent
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
key_name: "{{ AWS_Key_Name }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
instance_ids: "{{ item.awsid }}"
with_items: "{{ ec2vpn }}"
ignore_errors: True
when: have_vpninv.stat.exists == True
- name: Wait for instances to terminate before deleting more
pause:
seconds: 180
when: have_inv.stat.exists == True or have_vpninv.stat.exists == True
- name: Delete Server volumes
ec2_vol:
id: "{{ item.awsvol }}"
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
region: "{{ AWS_Region }}"
ec2_url: "{{ AWS_EC2_URL }}"
state: absent
with_items: "{{ ec2servers }}"
ignore_errors: True
when: have_inv.stat.exists == True
- name: Delete VPN volumes (Okay to fail)
ec2_vol:
id: "{{ item.awsvol }}"
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
region: "{{ AWS_Region }}"
ec2_url: "{{ AWS_EC2_URL }}"
state: absent
with_items: "{{ ec2vpn }}"
ignore_errors: True
when: have_vpninv.stat.exists == True
- name: Remove the inventory files
file:
path: "{{ RC_PLAY_TOP }}/{{ item }}"
state: absent
with_items:
- "inventory_ec2.yml"
- "inventory_ec2servers.yml"
- "deploy-lsf-server.tar"
- name: Remove the support files
file:
path: "{{ RC_PLAY_TOP }}/files/{{ item }}"
state: absent
with_items:
- "group"
- "hosts"
- "hosts.ec2"
- "passwd"
- "shadow"
- "shadow.tmp"
- "fstab.append"
- "hosts-lsf"
- name: Clean LSF hostcache
lineinfile:
dest: "/opt/ibm/lsfsuite/lsf/work/{{ CLOUD_LSF_CLUSTER_NAME }}/ego/lim/hostcache"
regexp: '.*awshost.*'
create: no
state: absent
- name: Clean LSF shared file
lineinfile:
dest: /opt/ibm/lsfsuite/lsf/conf/lsf.shared
regexp: "^{{ CLOUD_LSF_CLUSTER_NAME }}.*"
create: no
state: absent
- name: Reconfigure LSF
shell: echo "y" | lsadmin reconfig
ignore_errors: True
- name: Reset AWS_VPN_PRV_IP value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPN_PRV_IP:.*'
line: 'AWS_VPN_PRV_IP: none'
when:
- AWS_VPN_PRV_IP != "none"
# ---------- Delete VPC -------------
- name: Delete VPC private subnet
ec2_vpc_subnet:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
state: absent
vpc_id: "{{ AWS_VPC }}"
cidr: "{{ AWS_VPC_PRV_CIDR }}"
when:
- AWS_VPC_PRV_Subnet != "none"
- del_vpc == "y"
- name: Reset AWS_VPC_PRV_Subnet value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPC_PRV_Subnet:.*'
line: 'AWS_VPC_PRV_Subnet: none'
when:
- AWS_VPC_PRV_Subnet != "none"
- del_vpc == "y"
- name: Delete VPC subnet routes
ec2_vpc_route_table:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
state: absent
vpc_id: "{{ AWS_VPC }}"
lookup: id
route_table_id: "{{ AWS_VPC_Routes }}"
when:
- AWS_VPC_Routes != "none"
- del_vpc == "y"
- name: Reset AWS_VPC_Routes value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPC_Routes:.*'
line: 'AWS_VPC_Routes: none'
when:
- AWS_VPC_Routes != "none"
- del_vpc == "y"
- name: "Delete the Security Group"
ec2_group:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
state: absent
vpc_id: "{{ AWS_VPC }}"
name: My_LSF_VPC_SEC_GRP
description: Allow SSH and OpenVPN
# group_id: "{{ AWS_VPC_Security_Group }}"
when:
- AWS_VPC_Security_Group != "none"
- del_vpc == "y"
- name: Reset AWS_VPC_Security_Group value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPC_Security_Group:.*'
line: 'AWS_VPC_Security_Group: none'
when:
- AWS_VPC_Security_Group != "none"
- del_vpc == "y"
- name: Delete Internet Gateway
ec2_vpc_igw:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
vpc_id: "{{ AWS_VPC }}"
state: absent
when:
- AWS_VPC_IGW != "none"
- del_vpc == "y"
- name: Reset AWS_VPC_IGW value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPC_IGW:.*'
line: 'AWS_VPC_IGW: none'
when:
- AWS_VPC_IGW != "none"
- del_vpc == "y"
- name: Delete Network ACL
ec2_vpc_nacl:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
region: "{{ AWS_Region }}"
state: absent
name: My_LSF_VPC_NACL
vpc_id: "{{ AWS_VPC }}"
when:
- AWS_VPC_NACLs != "none"
- del_vpc == "y"
- name: Reset AWS_VPC_NACLs value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPC_NACLs:.*'
line: 'AWS_VPC_NACLs: none'
when:
- AWS_VPC_NACLs != "none"
- del_vpc == "y"
- name: Delete VPC
ec2_vpc_net:
name: My_LSF_VPC
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
cidr_block: "{{ AWS_VPC_CIDR }}"
state: absent
when:
- AWS_VPC != "none"
- del_vpc == "y"
- name: Reset AWS_VPC value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_VPC:.*'
line: 'AWS_VPC: none'
when:
- AWS_VPC != "none"
- del_vpc == "y"
- name: Reset files directory contents
shell: rm -rf "{{ RC_PLAY_TOP }}/files/rpms"
when:
- del_vpc == "y"
- name: Delete the IAM Policy
iam_policy:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
iam_type: role
iam_name: LSFRCRole
policy_name: "RC-Access"
state: absent
when:
- AWS_IAM_Role_Name != "none"
- del_vpc == "y"
- name: Delete the IAM Role
iam:
aws_access_key: "{{ AWS_Access_Key }}"
aws_secret_key: "{{ AWS_Secret_Key }}"
ec2_url: "{{ AWS_EC2_URL }}"
region: "{{ AWS_Region }}"
iam_type: role
name: LSFRCRole
state: absent
when:
- AWS_IAM_Role_Name != "none"
- del_vpc == "y"
- name: Reset AWS_IAM_Role_Name value in file
lineinfile:
dest: "{{ RC_PLAY_TOP }}/AWS/AWS-config.yml"
regexp: '^AWS_IAM_Role_Name:.*'
line: 'AWS_IAM_Role_Name: none'
when:
- AWS_IAM_Role_Name != "none"
- del_vpc == "y"