forked from vmware/ansible-vsphere-gos-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Windows] Install windows update patch by msu file (vmware#525)
Signed-off-by: Yanan Shen <[email protected]>
- Loading branch information
Showing
11 changed files
with
269 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2023 VMware, Inc. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
--- | ||
- name: "Set the .msu file installation related parameters" | ||
ansible.builtin.set_fact: | ||
msu_kb_number: "{{ msu_file_name | regex_search('(?i)KB\\d+') | upper }}" | ||
msu_file_dest_path: "{{ msu_dir_path }}\\{{ msu_file_name }}" | ||
msu_install_timeout: 7200 | ||
msu_become_user: "{{ 'SYSTEM' if guest_os_ansible_architecture == '32-bit' else 'Administrator' }}" | ||
|
||
- name: "Enable the user Administrator" | ||
include_tasks: ../utils/win_execute_cmd.yml | ||
vars: | ||
win_powershell_cmd: "net user Administrator /active:yes" | ||
when: vm_username | lower != "administrator" | ||
|
||
- name: "Install the .msu file" | ||
ansible.windows.win_shell: >- | ||
wusa.exe {{ msu_file_dest_path }} /quiet /norestart | ||
delegate_to: "{{ vm_guest_ip }}" | ||
register: install_msu_result | ||
become: true | ||
become_method: runas | ||
become_user: "{{ msu_become_user }}" | ||
async: "{{ msu_install_timeout }}" | ||
poll: 0 | ||
environment: | ||
ANSIBLE_WIN_ASYNC_STARTUP_TIMEOUT: 10 | ||
|
||
- name: "Check if the .msu file installation task started" | ||
ansible.builtin.assert: | ||
that: | ||
- install_msu_result is defined | ||
- install_msu_result.ansible_job_id is defined | ||
fail_msg: "The result of above .msu file installation task executed in guest OS is not returned." | ||
|
||
- name: "Check if the .msu file is installed before restart" | ||
ansible.windows.win_shell: "Get-HotFix | Where-Object HotFixID -eq {{ msu_kb_number }}" | ||
register: win_get_hotfix_result | ||
ignore_errors: true | ||
delegate_to: "{{ vm_guest_ip }}" | ||
ignore_unreachable: true | ||
until: | ||
- win_get_hotfix_result.rc is defined | ||
- win_get_hotfix_result.rc == 0 | ||
- win_get_hotfix_result.stdout_lines | length != 0 | ||
retries: "{{ msu_install_timeout // 300 }}" | ||
delay: 300 | ||
|
||
- name: "The .msu file installation failed" | ||
ansible.builtin.fail: | ||
msg: "Failed to install the .msu file {{ msu_file_name }} in {{ msu_install_timeout }} seconds." | ||
when: | ||
- win_get_hotfix_result.failed is defined | ||
- win_get_hotfix_result.failed | ||
|
||
- name: "Restart guest OS after installing the .msu file" | ||
include_tasks: ../utils/win_shutdown_restart.yml | ||
vars: | ||
set_win_power_state: "restart" | ||
win_reboot_timeout: 1800 | ||
|
||
- name: "Check if the .msu file is installed after restart" | ||
include_tasks: ../utils/win_execute_cmd.yml | ||
vars: | ||
win_powershell_cmd: "Get-HotFix | Where-Object HotFixID -eq {{ msu_kb_number }}" | ||
|
||
- name: "Check the msu installation result after restart" | ||
ansible.builtin.assert: | ||
that: | ||
- win_powershell_cmd_output.stdout_lines is defined | ||
- win_powershell_cmd_output.stdout_lines | length != 0 | ||
fail_msg: "The .msu file {{ msu_file_name }} is not installed in guest OS." | ||
success_msg: "The .msu file {{ msu_file_name }} is installed in guest OS." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2023 VMware, Inc. | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
--- | ||
- name: "Get unused driver letter" | ||
include_tasks: ../utils/win_get_unused_drive_letter.yml | ||
|
||
- name: "Initialize the .msu file path" | ||
ansible.builtin.set_fact: | ||
msu_file_src_path: "{{ drive_letter_new }}:\\{{ windows_nfs_msu_path }}\\*" | ||
msu_dir_path: "C:\\msu" | ||
msu_file_name: "" | ||
|
||
- name: "Check if folder {{ msu_dir_path }} exists on guest OS" | ||
include_tasks: ../utils/win_is_folder.yml | ||
vars: | ||
win_is_folder_path: "{{ msu_dir_path }}" | ||
|
||
- name: "Create folder {{ msu_dir_path }} on guest OS" | ||
include_tasks: ../utils/win_execute_cmd.yml | ||
vars: | ||
win_powershell_cmd: "mkdir {{ msu_dir_path }}" | ||
when: not win_is_folder_result | ||
|
||
- name: "Set mount command for accessing the shared folder" | ||
ansible.builtin.set_fact: | ||
win_nfs_mount_cmd: |- | ||
{%- if windows_nfs_username is defined and windows_nfs_username and | ||
windows_nfs_password is defined and windows_nfs_password -%} | ||
{{ 'net use ' ~ drive_letter_new ~ ': ' ~ windows_nfs_share ~ ' ' ~ windows_nfs_password ~ ' /user:' ~ windows_nfs_username }} | ||
{%- else -%} | ||
{{ 'net use ' ~ drive_letter_new ~ ': ' ~ windows_nfs_share }} | ||
{%- endif -%} | ||
no_log: "{{ hide_secrets | default(false) }}" | ||
|
||
- name: "Copy the .msu file to local disk of guest OS" | ||
include_tasks: ../utils/win_execute_cmd.yml | ||
vars: | ||
win_powershell_cmd: >- | ||
{{ win_nfs_mount_cmd }}; | ||
Copy-Item -Path {{ msu_file_src_path }} -Include *.msu -Destination {{ msu_dir_path }} -ErrorAction Stop; | ||
net use {{ drive_letter_new }}: /delete | ||
win_execute_cmd_no_log: "{{ hide_secrets | default(false) }}" | ||
|
||
- name: "Get the .msu file name" | ||
include_tasks: ../utils/win_execute_cmd.yml | ||
vars: | ||
win_powershell_cmd: >- | ||
Get-ChildItem -Path {{ msu_dir_path }} -Include *.msu -Name -ErrorAction Stop; | ||
- name: "Check if the .msu file is copied to {{ msu_dir_path }}" | ||
ansible.builtin.assert: | ||
that: | ||
- win_powershell_cmd_output.stdout_lines is defined | ||
- win_powershell_cmd_output.stdout_lines | length != 0 | ||
fail_msg: "The .msu file is not found in {{ msu_dir_path }} in guest OS." | ||
success_msg: "The .msu file is copied to {{ msu_dir_path }} in guest OS." | ||
|
||
- name: "Set the .msu file name" | ||
ansible.builtin.set_fact: | ||
msu_file_name: "{{ win_powershell_cmd_output.stdout_lines[0] }}" |
Oops, something went wrong.