-
Notifications
You must be signed in to change notification settings - Fork 40
/
aws_terminate_instances.yml
207 lines (184 loc) · 5.88 KB
/
aws_terminate_instances.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
---
- name: Terminate Instances
hosts: all
gather_facts: yes
vars_files:
- aws_vars.yml
tasks:
- name: Gather ec2 facts
ec2_facts:
- set_fact:
tower_host: "localhost"
tower_username: "{{ ec2_tag_student_id }}"
tower_password: "{{ tower_password }}"
- set_fact:
master_hostname: "{{ item }}"
with_items: "{{ groups.all }}"
when: "'master' in item"
- debug:
var: hostvars
verbosity: 2
- name: Query for EBS Volumes
shell: >
oc get pv -l failure-domain.beta.kubernetes.io/region --template '{% raw %}{{ range .items }}{{ index .spec.awsElasticBlockStore.volumeID }}{{ printf "\n" }}{{end}}{% endraw %}' |cut -d/ -f4
register: aws_pv
run_once: yes
when: "'master' in inventory_hostname"
- debug:
var: aws_pv
verbosity: 2
- name: Remove Instance from Tower Inventory
local_action:
module: tower_host
tower_host: "{{ tower_host }}"
tower_username: "{{ tower_username }}"
tower_password: "{{ tower_password }}"
name: "{{ inventory_hostname }}"
inventory: "{{ tower_inventory }}"
state: absent
delegate_to: "ec2-user@{{ tower_host }}"
when: "not 'tower' in inventory_hostname"
ignore_errors: yes
- name: Retrieve Instance Public DNS Record
local_action:
module: route53
command: get
zone: "{{ domain_name }}"
record: "{{ ec2_tag_Name }}"
type: A
register: dns_public_record
- debug:
var: dns_public_record
verbosity: 2
- name: Delete Instance Public DNS Record
local_action:
module: route53
command: delete
zone: "{{ domain_name }}"
type: "{{ dns_public_record.set.type }}"
ttl: "{{ dns_public_record.set.ttl }}"
record: "{{ dns_public_record.set.record }}"
value: "{{ dns_public_record.set.value }}"
when: ('tower' in inventory_hostname and terminate_tower|default(false)|bool == true) or ('node' in inventory_hostname or 'master' in inventory_hostname)
ignore_errors: yes
- name: Retrieve Internal Public DNS Record
local_action:
module: route53
command: get
zone: "{{ domain_name }}"
record: "{{ openshift_master_internal_dns_prefix }}-{{ ec2_tag_student_id }}.{{ domain_name }}"
type: A
register: dns_internal_record
when: "'master' in inventory_hostname"
- debug:
var: dns_internal_record
verbosity: 2
when: "'master' in inventory_hostname"
- name: Delete Internal Public DNS Record
local_action:
module: route53
command: delete
zone: "{{ domain_name }}"
type: "{{ dns_internal_record.set.type }}"
ttl: "{{ dns_internal_record.set.ttl }}"
record: "{{ dns_internal_record.set.record }}"
value: "{{ dns_internal_record.set.value }}"
when: "'master' in inventory_hostname"
ignore_errors: yes
- name: Retrieve Wildcard Public DNS Record
local_action:
module: route53
command: get
zone: "{{ domain_name }}"
record: "*.apps-{{ ec2_tag_student_id }}.{{ domain_name }}"
type: A
register: dns_wildcard_record
when: "'master' in inventory_hostname"
- debug:
var: dns_wildcard_record
verbosity: 2
when: "'master' in inventory_hostname"
- name: Delete Wildcard Public DNS Record
local_action:
module: route53
command: delete
zone: "{{ domain_name }}"
type: "{{ dns_wildcard_record.set.type }}"
ttl: "{{ dns_wildcard_record.set.ttl }}"
record: "{{ dns_wildcard_record.set.record }}"
value: "{{ dns_wildcard_record.set.value }}"
when: "'master' in inventory_hostname"
ignore_errors: yes
- name: Terminate OCP AWS Instances
local_action:
module: ec2
state: 'absent'
region: "{{ aws_region }}"
instance_ids: "{{ ansible_ec2_instance_id }}"
wait: yes
async: 300
poll: 0
when: "not 'tower' in inventory_hostname"
delegate_to: "ec2-user@{{ tower_host }}"
ignore_errors: yes
- name: Pause to allow OCP instances time to terminate and unbind any volumes
pause:
prompt: Waiting for OCP instances to delete ...
seconds: 15
- name: List EBS Volumes
local_action:
module: ec2_vol
id: "{{ item }}"
region: "{{ aws_region }}"
state: 'list'
with_items: "{{ hostvars[master_hostname].aws_pv.stdout_lines }}"
async: 120
poll: 5
when:
- "'tower' in inventory_hostname"
- master_hostname is defined
register: aws_pv_status
- debug:
var: aws_pv_status
verbosity: 2
when:
- "'tower' in inventory_hostname"
- master_hostname is defined
- name: Remove EBS Volumes
local_action:
module: ec2_vol
id: "{{ item }}"
instance: None
region: "{{ aws_region }}"
state: 'absent'
with_items: "{{ hostvars[master_hostname].aws_pv.stdout_lines }}"
async: 120
poll: 5
register: aws_pv_remove
until: not aws_pv_remove | failed
retries: 10
delay: 10
ignore_errors: yes
when:
- "'tower' in inventory_hostname"
- master_hostname is defined
- debug:
var: aws_pv_remove
verbosity: 2
when:
- "'tower' in inventory_hostname"
- master_hostname is defined
- name: Terminate Tower AWS Instance
local_action:
module: ec2
state: 'absent'
region: "{{ aws_region }}"
instance_ids: "{{ ansible_ec2_instance_id }}"
wait: no
async: 300
poll: 0
when:
- "'tower' in inventory_hostname"
- terminate_tower|default(false)|bool == true
ignore_errors: yes
...