-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathESXi_unprovisioning.yml
165 lines (145 loc) · 5.37 KB
/
ESXi_unprovisioning.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
# Playbook for server unprovisioning:
# - Put the ESXi server into maintenance mode
# - Do some ESXi server cleanup
# - Remove the server from vCenter
# - Power off the server
# - Delete the HPE OneView Server Profile
# - Remove the DNS record
# - Remove the host SSH key from .ssh/known_hosts on the Ansible control node
#
# Commands that can be used to run this playbook:
# $ ansible-playbook -i "RHEL-1," ESXi_unprovisioning.yml --ask-vault-password
# $ ansible-playbook -i "RHEL-1,ESX7-2" ESXi_unprovisioning.yml --ask-vault-password
# $ ansible-playbook -i hosts ESXi_unprovisioning.yml --ask-vault-password
---
- name: Deleting provisioned compute module(s)
hosts: all
collections:
- hpe.oneview
gather_facts: no
vars_files:
- vars/ESXi7.0.u3_vars.yml
- vars/VMware_vCenter_vars_encrypted.yml
vars:
# HPE Synergy Composer configuration
config: "{{ playbook_dir }}/oneview_config.json"
inventory_fqdn: "{{ inventory_hostname | lower }}.{{ domain }}"
ansible_forks: 5
tasks:
- name: Taking "{{ inventory_fqdn }}" to maintenance mode
vmware_maintenancemode:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
timeout: 3600
state: present
validate_certs: false
delegate_to: localhost
- name: Removing vmkernel mk1 port from "{{ vcenter_switch_name }}" distributed Switch
community.vmware.vmware_vmkernel:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
dvswitch_name: "{{ vcenter_switch_name }}"
portgroup_name: "{{ dportgroup_name }}"
state: absent
device: vmk1
validate_certs: False
delegate_to: localhost
- name: Gathering facts about vmnics
community.vmware.vmware_host_vmnic_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: "{{ inventory_fqdn }}"
validate_certs: false
delegate_to: localhost
register: host_vmnics
#- debug: var=host_vmnics
- name: Capturing available vmnics for the distributed switch creation
set_fact:
vmnics: "{{ host_vmnics.hosts_vmnics_info | json_query('\"' + inventory_fqdn + '\".dvswitch') }}"
- name: Removing host from "{{ vcenter_switch_name }}" distributed Switch
vmware_dvs_host:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: "{{ inventory_fqdn }}"
switch_name: "{{ vcenter_switch_name }}"
vmnics: "{{ vmnics | json_query('\"' + vcenter_switch_name + '\"') }} "
state: absent
validate_certs: False
delegate_to: localhost
- name: Removing ESXi host "{{ inventory_hostname }}" from vCenter "{{ vcenter_hostname }}"
vmware_host:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter_name: "{{ datacenter_name }}"
cluster_name: "{{ cluster_name }}"
esxi_hostname: "{{ inventory_fqdn }}"
state: absent
validate_certs: false
delegate_to: localhost
- name: Checking if server profile "{{ inventory_hostname }}" exists
oneview_server_profile_facts:
config: "{{ config }}"
name: "{{ inventory_hostname }}"
delegate_to: localhost
# - debug: var=server_profiles
- name : Getting server profile "{{ inventory_hostname }}" information
oneview_server_profile:
config: "{{ config }}"
state: "present"
data:
name: "{{ inventory_hostname }}"
delegate_to: localhost
when: server_profiles
# - debug: var=server_hardware
- name: Powering off server hardware "{{ inventory_hostname }}"
oneview_server_hardware:
config: "{{ config }}"
state: power_state_set
data:
name : "{{ server_hardware.name }}"
powerStateData:
powerState: "Off"
powerControl: "PressAndHold"
delegate_to: localhost
when: server_profiles
- name: Deleting server profile "{{ inventory_hostname }}"
oneview_server_profile:
config: "{{ config }}"
state: "absent"
data:
name: "{{ inventory_hostname }}"
delegate_to: localhost
when: server_profiles
register: result
- name: Result of the task to delete the server profile
debug:
msg: "{{ result.msg }}"
when: server_profiles
- name: Removing {{ inventory_hostname }} SSH key
known_hosts:
name: '{{ inventory_hostname }}'
path: ~/.ssh/known_hosts
state: absent
delegate_to: localhost
- name: Removing the DNS record from DNS server
hosts: all
gather_facts: no
vars:
ansible_forks: 5
vars_files:
vars/Windows_DNS_vars_encrypted.yml
tasks:
- name: Removing "{{ inventory_hostname }}" from "{{ dns_server }}"
community.windows.win_dns_record:
name: "{{ inventory_hostname }}"
type: "A"
zone: "{{ domain }}"
state: absent
delegate_to: "{{ dns_server }}"