-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws_in-place-upgrade.yml
213 lines (171 loc) · 7.34 KB
/
ws_in-place-upgrade.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
# Ansible playbook Windows in-place upgrade!
- name: Retrieve VM info and mount ISO via guest
hosts: localhost
gather_facts: no
vars_files:
- vars.yml # Configure your variables in vars.yml
- serverlist.yml # Provide your server list in serverlist.yml
##### Step 1: Verify if the Windows machines exist and mount the ISO file #####
tasks:
- name: Search for windows machine
vmware.vmware_rest.vcenter_vm_info:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: "{{ vcenter_validate_certs }}"
filter_names: "{{ server_name }}"
register: search_result
- name: Windows Server information
debug:
var: search_result
- name: Mount windows 2016 ISO image via guest
vmware.vmware_rest.vcenter_vm_hardware_cdrom:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: "{{ vcenter_validate_certs }}"
vm: '{{ search_result.value[0].vm }}'
type: SATA
sata:
bus: 0
unit: 2
start_connected: true
backing:
iso_file: '[yourdisk] ISO/win2016.ISO' # Update [yourdisk] and path to ISO
type: ISO_FILE
register: _result
##### Step 2: Turn off Windows machines and take snapshot #####
- name: Soft Shutdown 1min
hosts: your hosts # Ensure your hosts is the correct host group in your inventory
gather_facts: no
tasks:
- name: Notify users and execute soft shutdown
win_shell: shutdown.exe /s /t 60 /c "Server is shutting down in 1 minute. Please save your work."
- name: vcenter info
hosts: localhost
gather_facts: no
vars_files:
- vars.yml
- serverlist.yml
tasks:
- name: Wait until the server is fully down
vmware.vmware_rest.vcenter_vm_info:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: "{{ vcenter_validate_certs }}"
filter_names: "{{ server_name }}"
register: vm_status
until: vm_status.value[0].power_state == "POWERED_OFF"
retries: 60 # Number of times we check the status before giving up.
delay: 120 # Waiting time between each check (in seconds).
- name: Windows Server information
debug:
var: vm_status
- name: Create Snapshot of Windows VM
community.vmware.vmware_guest_snapshot:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
datacenter: "{{ datacenter }}"
folder: "{{ folder }}"
name: "{{ server_name }}"
state: present
snapshot_name: "ansible_snapshot_{{ lookup('pipe', 'date +%Y-%m-%d') }}"
description: "Taken by Ansible on {{ lookup('pipe', 'date +%Y-%m-%d') }}"
register: snapshot_result
when: vm_status.value | length > 0
##### Step 3: Turn on Windows machines, find CD drive letter and start in-place upgrade #####
- name: Power on the virtual machine via vmware_guest
vmware_guest_powerstate:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
datacenter: "{{ datacenter }}"
name: "{{ server_name }}"
state: powered-on
delegate_to: localhost
when: snapshot_result is defined and snapshot_result.failed | default(false) == false
- name: Find drive letter based on volume label via win_shell
hosts: your hosts
gather_facts: no
tasks:
- name: Find drive letter based on volume label via win_shell
pause:
minutes: 5
- name: Use PowerShell to retrieve disk info
win_shell: |
Get-Volume | Where-Object { $_.FileSystemLabel -eq "SSS_X64FREV_EN-US_DV9" } | Select-Object -ExpandProperty DriveLetter
register: disk_info
retries: 60
delay: 120
until: disk_info.stdout | trim != ""
- name: Show drive letter for 'SSS_X64FREV_EN-US_DV9' #You need to change this to your actual ISO name
debug:
msg: "{{ disk_info.stdout | trim }}"
- name: Run setup.exe for upgrade
win_command: "{{ disk_info.stdout | trim }}:\\setup.exe /auto upgrade /dynamicupdate disable /migratedrivers all /showoobe none /imageindex 4 /compat ignorewarning"
register: result
failed_when: result.rc != 0
ignore_unreachable: true
# This command starts the Windows setup process from the specified drive.
# - "/auto upgrade": Automates the Windows upgrade process.
# - "/dynamicupdate disable": Disables the use of Dynamic Update for setup.
# - "/migratedrivers all": Migrates all drivers during the upgrade.
# - "/showoobe none": Skips Out Of Box Experience (OOBE) post-install screens.
# - "/imageindex 4": Specifies the index of the image to install from the .wim file on the ISO.
# - "/compat ignorewarning": Ignores compatibility warnings during setup.
- name: Wait a while after the upgrade
pause:
minutes: 30
- name: Wait until the system is in place
win_ping:
register: ping_result
until: ping_result is succeeded
retries: 80 # Try a total of 80 times (including the first attempt)
delay: 900 # Wait 900 seconds (15 minutes) between attempts
ignore_errors: true
- name: Show the result
debug:
msg: "{{ result.stdout_lines }}"
##### Step 4: Retrieve current OS and wait until 2016 is in place before starting with windows updates #####
- name: Retrieve current OS name
win_shell: (Get-WmiObject -Class Win32_OperatingSystem).Caption
register: current_os_name
retries: 15
delay: 60
- name: Show current OS name
debug:
msg: "Current operating system is: {{ current_os_name.stdout | trim }}"
- name: Correct OS should be 2016
win_shell: (Get-WmiObject -Class Win32_OperatingSystem).Caption
register: current_os_name
until: "'Microsoft Windows Server 2016' in current_os_name.stdout"
retries: 15
delay: 60
- name: Show current OS name
debug:
msg: "Current operating system is: {{ current_os_name.stdout | trim }}"
- name: Ensure Ansible_windows_updates directory exists
win_file:
path: 'C:\\Windows\\Logs\\Ansible_windows_updates'
state: directory
- name: Get current date
set_fact:
current_date: "{{ lookup('pipe', 'date +%Y-%m-%d') }}"
- name: Install updates
win_updates:
reboot: yes
log_path: "C:\\Windows\\Logs\\Ansible_windows_updates\\patch_{{ current_date }}.log"
register: update_result
- name: Display installed updates
debug:
var: update_result
- name: Gather installed updates
win_shell: 'Get-WmiObject -Class "Win32_QuickFixEngineering" | Select-Object -Property "Description", "InstalledOn" | Out-File -FilePath "C:\\Windows\\Logs\\Ansible_windows_updates\\InstalledUpdates.txt" -Encoding ASCII'
register: win_updates
- name: Display installed updates
debug:
var: win_updates.stdout_lines