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