-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathteardown_servers_engine.yml
63 lines (52 loc) · 1.75 KB
/
teardown_servers_engine.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
---
- name: Teardown AWS Instances
hosts: localhost
gather_facts: no
vars_files:
- ./vars/default-vars.yml
vars:
ec2_instance_ids: []
tasks:
# # VPC or Virtual Private Cloud. The instance will be tied to a subnet that belongs to this particular VPC
# - name: "Get the VPC ID for {{ ec2_prefix }}-vpc"
# ec2_vpc_net_info:
# filters:
# "tag:Name": "{{ ec2_prefix }}-vpc"
# region: "{{ ec2_region }}"
# register: vpc_net_facts
#
# - name: debug vpc_net_facts
# debug:
# var: vpc_net_facts
- name: "Gather {{ application }} instance info"
ec2_instance_info:
filters:
"tag:application": "{{ application }}"
"tag:provisioner": "mford"
instance-state-name: [ "running", "shutting-down", "stopping", "stopped" ]
register: instance_info
- debug:
var: instance_info
- debug:
var: instance_info.instances | length
- debug:
var: instance_info | json_query('instances[*].instance_id')
- name: "Create a list of the {{ application }} Instances"
set_fact:
ec2_instance_ids: "{{ ec2_instance_ids }} + [ '{{ item }}']"
loop: "{{ instance_info | json_query('instances[*].instance_id') }}"
- name: "Terminate Instances for Application {{ application }}"
ec2:
state: absent
instance_ids: "{{ ec2_instance_ids }}"
wait: "{{ ec2_wait }}"
region: "{{ ec2_region }}"
when: ec2_instance_ids | length > 0
- name: Delete AWS SSH Public Key
ec2_key:
name: "{{ ec2_prefix }}-key"
state: absent
- name: Delete Local Private Key
file:
path: "{{ working_dir }}/{{ ec2_prefix }}-key-private.pem"
state: absent