-
Notifications
You must be signed in to change notification settings - Fork 0
/
Undeploy.yml
304 lines (265 loc) · 11 KB
/
Undeploy.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
##
## Project: SDDC.Lab
## Authors: Luis Chanu & Rutger Blom
## Filename: UnDeploy.yml
##
---
- hosts: localhost
name: UnDeploy.yml
tasks:
- name: Display error message if Pod-XXX-Config file is not valid or provided
ansible.builtin.pause:
seconds: 5
prompt: |
*****************************************************************************************************
****************************************** ERROR MESSAGE ********************************************
*****************************************************************************************************
A valid "Pod-XXX-Config.yml" file is required in order for this playbook to run.
Please verify:
==============
1) You supplied a valid Pod-XXX-Config.yml file via the ansible-playbook -e "@Pod-XXX-Config.yml"
command-line option. Here is an example of a how to load a Pod-XXX-Config.yml file that is
located in your home directory:
ansible-playbook -e "@~/Pod-XXX-Config.yml" UnDeploy.yml
2) The Pod-XXX-Config.yml file provided was created using the playbooks/CreatePodConfig.yml script.
All Pod configuration files used to deploy labs MUST be generated using that script.
3) You included the proper path with the "-e" option to the Pod-XXX-Config.yml file.
4) You prefaced the file name in the "-e" option with a '@', as shown in the example above.
*****************************************************************************************************
when:
- Valid_Pod_Config_File is not defined
- name: Exit Ansible playbook if Pod-XXX-Config.yml file is not valid or provided
ansible.builtin.meta: end_play
when: Valid_Pod_Config_File is not defined
- name: DEBUG -- Display Target Variables (Pause)
ansible.builtin.pause:
seconds: "{{ DEBUG.DisplayDelayInSeconds }}"
prompt: |
================================ Display Variables For Pod {{ '%03d' | format(Pod.Number | int) }} ==================================
Ansible Playbook: {{ ansible_play_name }}
Target.Deployment: {{ Target.Deployment }}
Target.FQDN: {{ Target.FQDN }}
Target.User: {{ Target.User }}
Target.Password: {{ Target.Password }}
Target.Cluster: {{ Target.Cluster }}
Target.DataCenter: {{ Target.DataCenter }}
Target.Datastore: {{ Target.Datastore }}
Target.vSwitch: {{ Target.vSwitch }}
Target.PortGroup.Trunk.Name: {{ Target.PortGroup.Trunk.Name }}
Target.PortGroup.Trunk.VLAN: {{ Target.PortGroup.Trunk.VLAN }}
Target.PortGroup.Management.Name: {{ Target.PortGroup.Management.Name }}
Target.PortGroup.Management.VLAN: {{ Target.PortGroup.Management.VLAN }}
Nested_Router.Name: {{ Nested_Router.Name }}
Nested_vCenter.VMName: {{ Nested_vCenter.VMName }}
Nested_NSXT.Component.LocalManager.VMName: {{ Nested_NSXT.Component.LocalManager.VMName }}
=================================================================================================
when:
- DEBUG.DisplayVariables
- name: Remove VyOS router VM
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ Nested_Router.Name }}"
force: true
state: absent
ignore_errors: true
- name: Remove vRLI VM
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ Nested_vRLI.Component.Node.VMName }}"
force: true
state: absent
ignore_errors: true
- name: Remove ALB VM
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ Nested_ALB.Component.Controller.VMName }}"
force: true
state: absent
ignore_errors: true
- name: Remove ESXi VMs
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ item.value.VMName }}"
force: true
state: absent
async: 7200
poll: 0
loop: "{{ Nested_ESXi.Host | dict2items }}"
ignore_errors: true
- name: Remove vCenter VM
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ Nested_vCenter.VMName }}"
force: true
state: absent
ignore_errors: true
- name: Remove NSX Local Manager VM
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ Nested_NSXT.Component.LocalManager.VMName }}"
force: true
state: absent
ignore_errors: true
- name: Remove NSX Global Manager VM
community.vmware.vmware_guest:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
name: "{{ Nested_NSXT.Component.GlobalManager.VMName }}"
force: true
state: absent
ignore_errors: true
##
## Remove Networking - Host Deployment
##
- name: Remove trunk port group for the nested environment
community.vmware.vmware_portgroup:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: false
esxi_hostname: "{{ Target.FQDN }}"
switch_name: "{{ Target.vSwitch }}"
portgroup_name: "{{ Target.PortGroup.Trunk.Name }}"
state: absent
ignore_errors: true
when:
- Target.Deployment == "Host"
- name: Remove management port group for the nested environment
community.vmware.vmware_portgroup:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: false
esxi_hostname: "{{ Target.FQDN }}"
switch_name: "{{ Target.vSwitch }}"
portgroup_name: "{{ Target.PortGroup.Management.Name }}"
state: absent
ignore_errors: true
when:
- Target.Deployment == "Host"
- name: Remove VMware vSwitch on the ESXi host for the nested environment
community.vmware.vmware_vswitch:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: false
esxi_hostname: "{{ Target.FQDN }}"
switch_name: "{{ Target.vSwitch }}"
state: absent
async: 7200
poll: 0
ignore_errors: true
when:
- Target.Deployment == "Host"
##
## Remove Networking - vCenter Deployment
##
- name: Remove a Management Distributed Port-Group in vCenter
community.vmware.vmware_dvs_portgroup:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
switch_name: "{{ Target.vSwitch }}"
portgroup_name: "{{ Target.PortGroup.Management.Name }}"
port_binding: static
vlan_id: "{{ Target.PortGroup.Management.VLAN }}"
vlan_trunk: false
num_ports: 48
state: absent
network_policy:
promiscuous: true
forged_transmits: true
mac_changes: false
ignore_errors: true
when: Target.Deployment == "vCenter"
- name: Remove a Trunk Distributed Port-Group in vCenter
community.vmware.vmware_dvs_portgroup:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
switch_name: "{{ Target.vSwitch }}"
portgroup_name: "{{ Target.PortGroup.Trunk.Name }}"
port_binding: static
vlan_id: "{{ Target.PortGroup.Trunk.VLAN }}"
vlan_trunk: true
num_ports: 48
state: absent
network_policy:
promiscuous: true
forged_transmits: true
mac_changes: false
ignore_errors: true
when: Target.Deployment == "vCenter"
##
## Remove vCenter Child VM Folder - vCenter Deployment
##
# REMINDER: Can not remove parent folder because other Pods are using it
- name: Remove Child VM Folder
community.vmware.vcenter_folder:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
validate_certs: "{{ Common.PKI.ValidateCerts }}"
datacenter: "{{ Target.DataCenter }}"
folder_type: vm
parent_folder: "{{ Target.VMFolder | regex_replace('^(.*)/.*$', '\\1') }}" # Extract parent folder from VMFolder full path
folder_name: "{{ Target.VMFolder | regex_replace('^.*/(.*)$', '\\1') }}" # Extract remaining folder name
state: absent
ignore_errors: true
when:
- Target.Deployment == "vCenter"
##
## Delete DRS VM-VM Affinity Rule
##
- name: Delete DRS VM-VM affinity rule
community.vmware.vmware_vm_vm_drs_rule:
hostname: "{{ Target.FQDN }}"
username: "{{ Target.User }}"
password: "{{ Target.Password }}"
cluster_name: "{{ Target.Cluster }}"
drs_rule_name: "{{ Deploy.DRS.VMRules.Name }}"
validate_certs: false
state: absent
when:
- Target.Deployment == "vCenter"
##
## Cleanup Ansible Control Station
##
- name: Remove static route on the Ansible Control Station
ansible.builtin.command: ./ip route del "{{ Net.Management.IPv4.Network }}.0/{{ Net.Management.IPv4.Prefix }}"
args:
chdir: /bin/
register: iproute
ignore_errors: true
changed_when: false
when:
- Deploy.Product.Router.Deploy
- Deploy.Setting.IPv4
- Nested_Router.Protocol == "Static"
##
## Remove Dynamic DNS Entries
##
- name: Remove DNS records for the Pod
import_playbook: playbooks/CleanupDNS.yml