-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcloudcat-destroy.yml
121 lines (106 loc) · 3.46 KB
/
cloudcat-destroy.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
---
- hosts: localhost
connection: local
vars:
access: redactedxxx
secret: redactedyyy
instance_name: cloudcat-cracker
image: ami-0ee246e709782b1be
region: regionxxx
type: typexxx
ssh_key: sshxxx
identity: identityxxx
wordsnap: snapxxx
#group:
vars_files:
- external_vars.yml
tasks:
- name: Gathering EC2 instance information
ec2_instance_facts:
aws_access_key: "{{ access }}"
aws_secret_key: "{{ secret }}"
region: "{{ region }}"
filters:
"tag:name": cloudcat-instance
register: ec2
- debug:
msg: "EC2 instance {{ ec2.instances.0.instance_id }} at {{ ec2.instances.0.public_ip_address }} will be destroyed."
ignore_errors: true
# Looping block sample
# - name: Destroying EC2 instance
# ec2_instance:
# aws_access_key: "{{ access }}"
# aws_secret_key: "{{ secret }}"
# state: absent
# instance_ids: "{{ item }}"
# region: "{{ region }}"
# wait_timeout: "300"
# wait: true
# register: ec2
# ignore_errors: true
# loop:
# - {{ ec2.instances.0.instance_id }}
# - {{ ec2.instances.1.instance_id }}
# - {{ ec2.instances.2.instance_id }}
- name: Destroying EC2 instance
ec2_instance:
aws_access_key: "{{ access }}"
aws_secret_key: "{{ secret }}"
state: absent
instance_ids: "{{ ec2.instances.0.instance_id }}"
region: "{{ region }}"
wait_timeout: "300"
wait: true
register: ec2
ignore_errors: true
- debug:
msg: "EC2 instance destroyed."
when: ec2.instances.0.instance_id is defined
- debug:
msg: "No instances to destroy!"
when: ec2.instances.0.instance_id is not defined
- name: Gathering volume facts
ec2_vol_facts:
aws_access_key: "{{ access }}"
aws_secret_key: "{{ secret }}"
region: "{{ region }}"
filters:
"tag:name": "cloudcat-wordlist"
register: ec2vol
- name: Destroying wordlist volume
ec2_vol:
aws_access_key: "{{ access }}"
aws_secret_key: "{{ secret }}"
state: absent
region: "{{ region }}"
id: "{{ ec2vol.volumes.0.id }}"
ignore_errors: true
- debug:
msg: "Wordlist volume {{ ec2vol.volumes.0.id }} destroyed."
when: ec2vol.volumes.0.id is defined
- debug:
msg: "No wordlist volumes to destroy!"
when: ec2vol.volumes.0.id is not defined
- debug:
msg: "All instances and wordlists destroyed."
when: ec2vol.volumes.0.id is defined and
ec2.instances.0.instance_id is defined
- debug:
msg: "Nothing destroyed since nothing was there."
when: ec2vol.volumes.0.id is not defined and
ec2.instances.0.instance_id is not defined
- name: Checking for remaining instances
ec2_instance_facts:
aws_access_key: "{{ access }}"
aws_secret_key: "{{ secret }}"
region: "{{ region }}"
filters:
"tag:name": cloudcat-instance
instance-state-name: [ "shutting-down", "stopping", "stopped", "terminated" ]
register: remaining
- debug:
msg: "{{ remaining.instances.0.instance_id }} instance(s) remaining."
when: remaining.instances.0.instance_id is defined
- debug:
msg: "No instances remaining relating to CloudCat."
when: remaining.instances.0.instance_id is not defined