-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup-generic
executable file
·134 lines (120 loc) · 4.91 KB
/
cleanup-generic
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
#!/usr/bin/env ansible-playbook
# (c) 2016 DataNexus Inc. All Rights Reserved.
#
# remove application instances and associated infrastructure
---
- hosts: localhost
connection: local
vars_files:
- "{{ configuration }}"
gather_facts: true
tasks:
# the math is overkill, but it's useful if we want to get more complicated
- name: GENERIC CLEANUP OVERLAY | setting count to {{ count }}
set_fact:
count: "{{ count | default(1) | int * 1 }}"
- block:
- name: APPLICATION CLEANUP | gathering {{ region }} running instances
ec2_instance_facts:
region: "{{ region }}"
filters:
instance-state-name: running
"tag:Tenant": "{{ tenant }}"
"tag:Role": "{{ role | default('none') }}"
"tag:Project": "{{ project }}"
"tag:Domain": "{{ domain }}"
"tag:Application": "{{ application }}"
"tag:Cluster": "{{ cluster | default ('a') }}"
"tag:Dataflow": "{{ dataflow | default ('none') }}"
register: instance_facts
- name: APPLICATION CLEANUP | gathering {{ region }} available interfaces
ec2_eni_facts:
region: "{{ region }}"
filters:
status: in-use
group-name: "{{ project }}_{{ application }}"
description: 'public internal'
register: eni_facts
- name: APPLICATION CLEANUP | releasing EIP in {{ region }}
ec2_eip:
region: "{{ region }}"
in_vpc: yes
state: absent
release_on_disassociation: yes
device_id: "{{ item }}"
with_items: "{{ eni_facts.interfaces | selectattr('status', 'equalto', 'in-use') | map(attribute='id') | list }}"
when:
- eni_facts.interfaces | length > 0
- name: APPLICATION CLEANUP | terminating instances in {{ region }}
ec2:
state: absent
region: "{{ region }}"
instance_ids: "{{ item }}"
wait: true
with_items: "{{ instance_facts.instances | selectattr('state', 'equalto', 'running') | map(attribute='id') | list }}"
when:
- instance_facts.instances | length > 0
- name: APPLICATION CLEANUP | gathering {{ region }} available volumes
ec2_vol_facts:
region: "{{ region }}"
filters:
status: available
register: volume_facts
- name: APPLICATION CLEANUP | terminating volumes in {{ region }}
ec2_vol:
state: absent
region: "{{ region }}"
id: "{{ item }}"
with_items: "{{ volume_facts.volumes | selectattr('status', 'equalto', 'available') | map(attribute='id') | list }}"
when:
- volume_facts.volumes | length > 0
- name: APPLICATION CLEANUP | terminating interfaces in {{ region }}
ec2_eni:
state: absent
region: "{{ region }}"
eni_id: "{{ item }}"
with_items: "{{ eni_facts.interfaces | selectattr('status', 'equalto', 'available') | map(attribute='id') | list }}"
when:
- eni_facts.interfaces | length > 0
- name: APPLICATION CLEANUP | removing {{ application }} key
ec2_key:
state: absent
region: "{{ region }}"
name: "{{ region }}-{{ application }}"
- name: APPLICATION CLEANUP | retrieving VPC ID for {{ network }} in {{ region }}
ec2_vpc_net_facts:
region: "{{ region }}"
# This filter must map exactly to what we created in the e2_vpc module
filters:
cidr_block: "{{ cidr_block }}"
register: vpc_facts
- name: APPLICATION CLEANUP | removing application security groups
local_action:
module: ec2_group
name: "{{ project }}_application"
vpc_id: "{{ (vpc_facts.vpcs|selectattr('state', 'equalto', 'available')|map(attribute='id')|list).0 }}"
region: "{{ region }}"
state: absent
when: cloud == 'aws'
- block:
- include_role:
name: azure
tasks_from: discover-resourcegroup
- name: APPLICATION CLEANUP | removing {{ count }} {{ application }} VM in {{ specified_resourcegroup.name }}
azure_rm_virtualmachine:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ project }}-{{ application }}-{{item }}"
state: absent
remove_on_absent:
- network_interfaces
- virtual_storage
with_sequence: count={{ count }}
- name: APPLICATION OVERLAY | deleting remaining security groups
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ item }}"
state: absent
with_items:
- "dnsg_{{ project }}_ssh_private_internal"
- "dnsg_{{ project }}_{{ application }}_public_internal"
when: cloud == 'azure'