Skip to content

Commit

Permalink
split file windows_update_install.yml
Browse files Browse the repository at this point in the history
Signed-off-by: Yanan Shen <yanans@vmware.com>
  • Loading branch information
123lzxm committed Jan 3, 2024
1 parent c044967 commit ebeae2a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 108 deletions.
67 changes: 67 additions & 0 deletions windows/windows_update_install/install_msu_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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: "{{ msu_file_dest_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 }} /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 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 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_kb_number }} is not installed in guest OS."
success_msg: "The msu file {{ msu_kb_number }} is installed in guest OS."
48 changes: 48 additions & 0 deletions windows/windows_update_install/prepare_msu_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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_update_msu_path }}\\*"
msu_file_dest_path: "C:\\msu"
msu_file_name: ""

- name: "Check if folder {{ msu_file_dest_path }} exists on guest OS"
include_tasks: ../utils/win_is_folder.yml
vars:
win_is_folder_path: "{{ msu_file_dest_path }}"

- name: "Create folder {{ msu_file_dest_path }} on guest OS"
include_tasks: ../utils/win_execute_cmd.yml
vars:
win_powershell_cmd: "mkdir {{ msu_file_dest_path }}"
when: not win_is_folder_result

- name: "Copy the msu file to local disk of guest OS"
include_tasks: ../utils/win_execute_cmd.yml
vars:
win_powershell_cmd: >-
net use {{ drive_letter_new }}: {{ windows_msu_share_point }} {{ windows_nfs_pwd }} /user:{{ windows_nfs_username }};
Copy-Item -Path {{ msu_file_src_path }} -Include *.msu -Destination {{ msu_file_dest_path }} -ErrorAction Stop;
net use {{ drive_letter_new }}: /delete
- name: "Get the msu file name"
include_tasks: ../utils/win_execute_cmd.yml
vars:
win_powershell_cmd: >-
Get-ChildItem -Path {{ msu_file_dest_path }} -Include *.msu -Name -ErrorAction Stop;
- name: "Check if the msu file is copied to {{ msu_file_dest_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_file_dest_path }} in guest OS."
success_msg: "The msu file is copied to {{ msu_file_dest_path }} in guest OS."

- name: "Set the msu file name"
ansible.builtin.set_fact:
msu_file_name: "{{ win_powershell_cmd_output.stdout_lines[0] }}"
111 changes: 3 additions & 108 deletions windows/windows_update_install/windows_update_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,116 +16,11 @@
vars:
create_current_test_folder: true

- 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_update_msu_path }}\\*"
msu_file_dest_path: "C:\\msu"
msu_file_name: ""

- name: "Check if folder {{ msu_file_dest_path }} exists on guest OS"
include_tasks: ../utils/win_is_folder.yml
vars:
win_is_folder_path: "{{ msu_file_dest_path }}"

- name: "Create folder {{ msu_file_dest_path }} on guest OS"
include_tasks: ../utils/win_execute_cmd.yml
vars:
win_powershell_cmd: "mkdir {{ msu_file_dest_path }}"
when: not win_is_folder_result

- name: "Copy the msu file to local disk of guest OS"
include_tasks: ../utils/win_execute_cmd.yml
vars:
win_powershell_cmd: >-
net use {{ drive_letter_new }}: {{ windows_msu_share_point }} {{ windows_nfs_pwd }} /user:{{ windows_nfs_username }};
Copy-Item -Path {{ msu_file_src_path }} -Include *.msu -Destination {{ msu_file_dest_path }} -ErrorAction Stop;
net use {{ drive_letter_new }}: /delete
- name: "Get the msu file name"
include_tasks: ../utils/win_execute_cmd.yml
vars:
win_powershell_cmd: >-
Get-ChildItem -Path {{ msu_file_dest_path }} -Include *.msu -Name -ErrorAction Stop;
- name: "Check if the msu file is copied to {{ msu_file_dest_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_file_dest_path }} in guest OS."
success_msg: "The msu file is copied to {{ msu_file_dest_path }} in guest OS."

- name: "Set the msu file name"
ansible.builtin.set_fact:
msu_file_name: "{{ win_powershell_cmd_output.stdout_lines[0] }}"

- 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: "{{ msu_file_dest_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: "Prepare the msu file"
include_tasks: prepare_msu_file.yml

- name: "Install the msu file"
ansible.windows.win_shell: >-
wusa.exe {{ msu_file_dest }} /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 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 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_kb_number }} is not installed in guest OS."
success_msg: "The msu file {{ msu_kb_number }} is installed in guest OS."
include_tasks: install_msu_file.yml

- name: "Reset base snapshot after installing the msu file"
include_tasks: ../../common/reset_base_snapshot.yml
Expand Down

0 comments on commit ebeae2a

Please sign in to comment.