-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup-johnston
executable file
·129 lines (117 loc) · 4.35 KB
/
cleanup-johnston
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
#!/usr/bin/env ansible-playbook
# (c) 2016 DataNexus Inc. All Rights Reserved.
#
# remove johnston instances
---
- hosts: localhost
connection: local
vars_files:
- "{{ configuration }}"
gather_facts: true
tasks:
- block:
- name: gathering {{ region }} running instances
ec2_instance_facts:
region: "{{ region }}"
filters:
instance-state-name: running
"tag:Tenant": "{{ tenant }}"
"tag:Project": "{{ project }}"
"tag:Domain": "{{ domain }}"
"tag:Application": "{{ application }}"
"tag:Cluster": "{{ cluster | default ('a') }}"
"tag:Dataflow": "{{ dataflow | default ('none') }}"
register: instance_facts
- name: 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: gathering {{ region }} available volumes
ec2_vol_facts:
region: "{{ region }}"
filters:
status: available
register: volume_facts
- name: 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: gathering {{ region }} available interfaces
ec2_eni_facts:
region: "{{ region }}"
filters:
status: available
register: eni_facts
- name: 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: removing {{ application }} key
ec2_key:
state: absent
region: "{{ region }}"
name: "{{ region }}-{{ application }}"
- name: 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: removing jumphost security groups
ec2_group:
name: "{{ item }}"
vpc_id: "{{ (vpc_facts.vpcs | selectattr('state', 'equalto', 'available') | map(attribute='id') | list).0 }}"
region: "{{ region }}"
state: absent
with_items:
- jumphost_ssh
- jumphost_open
- demo_ssh
when: cloud == 'aws'
- block:
- include_role:
name: azure
tasks_from: discover-resourcegroup
- name: JOHNSTON CLEANUP | removing {{ application }} VM in {{ specified_resourcegroup.name }}
azure_rm_virtualmachine:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ item }}"
state: absent
remove_on_absent:
- network_interfaces
- virtual_storage
with_items:
- "{{ project }}-{{ application }}"
- name: JOHNSTON CLEANUP | NIC removal if we haven't booted a machine
azure_rm_networkinterface:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ item }}"
state: absent
with_items:
- "{{ project }}_{{ application }}_internal"
- "{{ project }}_{{ application }}_external"
- name: JOHNSTON OVERLAY | deleting all security groups
azure_rm_securitygroup:
resource_group: "{{ specified_resourcegroup.name }}"
name: "{{ item }}"
state: absent
with_items:
- "{{ project }}_{{ application }}_internal"
- "{{ project }}_{{ application }}_external"
- "dnsg_{{ project }}_{{ application }}_private_internal"
- "dnsg_{{ project }}_{{ application }}_public_internal"
when: cloud == 'azure'