forked from hornjason/ansible-ocp-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy.yml
148 lines (125 loc) · 5.32 KB
/
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
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
---
- hosts: localhost
vars:
skip: False
tasks:
- name: Destroy Azure | Check for ResourceGroup
shell: az group exists --name "{{ rg }}"
register: rg_exist
tags: always
- block:
- debug: var=rg_exist
- name: Destroy Azure | Gather list of VMs
shell: az vm list -g "{{ rg }}" --query "[].name" -o tsv
register: vm_list
# The bastion is not part of the Ansible inventory, but we
# need to unregister it with RHSM.
- name: Destroy Azure | Add bastion host
add_host:
name: bastion
groups: bastions
ansible_user: "{{ admin_user }}"
ansible_become: True
- name: Destroy Azure | RHSM unregister
redhat_subscription:
state: absent
delegate_to: "{{ item }}"
loop: "{{ vm_list.stdout_lines }}"
# We use the CLI option "--no-wait" to delete all VMs in
# parallel, and then wait in an Ansible loop until all of them
# are gone.
- name: Destroy Azure | Schedule VM deletion
command: az vm delete --resource-group {{ rg }} --name {{ item }} --no-wait --yes
loop: "{{ vm_list.stdout_lines }}"
# Wait up to 20 minutes until all VMs have been deleted.
- name: Destroy Azure | Wait for VM deletion
command: az vm list -g "{{ rg }}" --query "[].name" -o tsv
register: vm_list2
retries: 20
delay: 60
until: vm_list2.stdout_lines | length == 0
- name: Destroy Azure | Gather list of StorageAccounts
shell: az storage account list -g "{{ rg }}" --query "[].name" -o tsv
register: sa_list
- name: Destroy Azure | Delete Storage Accounts
azure_rm_storageaccount:
resource_group: "{{ rg }}"
name: "{{ item }}"
state: absent
loop: "{{ sa_list.stdout_lines }}"
- name: Destroy Azure | Gather list of disks
shell: az disk list -g "{{ rg }}" --query "[].name" -o tsv
register: disk_list
# We use the CLI option "--no-wait" to delete all disks in
# parallel.
- name: Destroy Azure | Schedule managed disk deletion
command: az disk delete --resource-group {{ rg }} --name {{ item }} --no-wait --yes
loop: "{{ disk_list.stdout_lines }}"
# Wait up to 20 minutes until all disks have been deleted.
- name: Destroy Azure | Wait for disk deletion
command: az disk list -g "{{ rg }}" --query "[].name" -o tsv
register: disk_list2
retries: 20
delay: 60
until: disk_list2.stdout_lines | length == 0
- name: Destroy Azure | Gather list of AvailabilitySets
shell: az vm availability-set list -g "{{ rg }}" --query "[].name" -o tsv
register: as_list
- name: Destroy Azure | Delete Availability Sets
azure_rm_availabilityset:
name: "{{ item }}"
location: "{{ location }}"
resource_group: "{{ rg }}"
state: absent
loop: "{{ as_list.stdout_lines }}"
- name: Destroy Azure | Gather list of LoadBalancers
shell: az network lb list -g "{{ rg }}" --query "[].name" -o tsv
register: lb_list
- name: Destroy Azure | Delete LoadBalancers
azure_rm_loadbalancer:
name: "{{ item }}"
location: "{{ location }}"
resource_group: "{{ rg }}"
state: absent
loop: "{{ lb_list.stdout_lines }}"
- name: Destroy Azure | Gather list of NICs
shell: az network nic list -g "{{ rg }}" --query "[].name" -o tsv
register: nic_list
- name: Destroy Azure | Delete NICs
azure_rm_networkinterface:
resource_group: "{{ rg }}"
name: "{{ item }}"
state: absent
loop: "{{ nic_list.stdout_lines }}"
- name: Destroy Azure | Gather list of PublicIPs
shell: az network public-ip list -g "{{ rg }}" --query "[].name" -o tsv
register: pip_list
- name: Destroy Azure | Delete PublicIPs
azure_rm_publicipaddress:
resource_group: "{{ rg }}"
name: "{{ item }}"
state: absent
loop: "{{ pip_list.stdout_lines }}"
- name: Destroy Azure | Gather list of Network Security Groups
shell: az network nsg list -g "{{ rg }}" --query "[].name" -o tsv
register: nsg_list
- name: Destroy Azure | Delete Network Security Groups
azure_rm_securitygroup:
resource_group: "{{ rg }}"
name: "{{ item }}"
state: absent
loop: "{{ nsg_list.stdout_lines }}"
- name: Destroy Azure | Gather list of vNets
shell: az network vnet list -g "{{ rg }}" --query '[].name' -o tsv
register: vnet_list
tags: vnet
when: not vnet_preserve
- name: Destroy Azure | Delete vNet
azure_rm_virtualnetwork:
resource_group: "{{ rg }}"
name: "{{ item }}"
state: absent
tags: vnet
loop: "{{ vnet_list.stdout_lines }}"
when: not vnet_preserve
when: rg_exist.stdout == 'true'