9
This commit is contained in:
@@ -4,17 +4,21 @@
|
||||
gather_facts: no
|
||||
|
||||
vars:
|
||||
task_path: "\" # e.g. "\" or "\Microsoft\Windows\WindowsUpdate\"
|
||||
task_name: "Patching-windows-task"
|
||||
# Root task folder: use '\\'
|
||||
# If your task is in a folder, e.g. \Microsoft\Windows\WindowsUpdate\
|
||||
# then set: '\\Microsoft\\Windows\\WindowsUpdate\\'
|
||||
task_path: '\\'
|
||||
task_name: 'Patching-windows-task'
|
||||
|
||||
tasks:
|
||||
- name: Start the SYSTEM patch task
|
||||
ansible.windows.win_command: >
|
||||
schtasks /Run /TN "{{ task_path }}{{ task_name }}"
|
||||
register: start_task
|
||||
changed_when: start_task.rc == 0
|
||||
changed_when: start_task.rc == 0 or
|
||||
('SUCCESS' in (start_task.stdout | default('')))
|
||||
failed_when: start_task.rc not in [0] and
|
||||
("SUCCESS" not in (start_task.stdout | default('')))
|
||||
('SUCCESS' not in (start_task.stdout | default('')))
|
||||
|
||||
- name: Poll task until it is Ready or Disabled
|
||||
ansible.windows.win_powershell:
|
||||
@@ -23,23 +27,15 @@
|
||||
Import-Module ScheduledTasks
|
||||
$tp = '{{ task_path }}'
|
||||
$tn = '{{ task_name }}'
|
||||
# Confirm it exists (throws if not)
|
||||
$null = Get-ScheduledTask -TaskPath $tp -TaskName $tn
|
||||
$state = (Get-ScheduledTaskInfo -TaskPath $tp -TaskName $tn).State
|
||||
# Return plain text state for Ansible to parse
|
||||
$state
|
||||
(Get-ScheduledTaskInfo -TaskPath $tp -TaskName $tn).State
|
||||
register: task_state
|
||||
failed_when: false
|
||||
retries: 3 # up to 3 hours
|
||||
retries: 180 # up to 3 hours
|
||||
delay: 60
|
||||
until: task_state.stdout | trim in ['Ready','Disabled']
|
||||
|
||||
- name: Show last observed task state (debug)
|
||||
ansible.builtin.debug:
|
||||
msg:
|
||||
- "Task '{{ task_path }}{{ task_name }}' final state: {{ task_state.stdout | trim }}"
|
||||
until: (task_state.stdout | trim) in ['Ready','Disabled']
|
||||
|
||||
- name: Reboot if needed
|
||||
ansible.windows.win_reboot:
|
||||
reboot_timeout: 3600
|
||||
when: task_state.stdout | trim == 'Ready'
|
||||
when: (task_state.stdout | trim) == 'Ready'
|
||||
Reference in New Issue
Block a user