Windows: report from a post-install search, even on failure

- Re-search after installing; win_updates can list reboot-applied updates as not installed
- Wrap install in block/always so the pending report runs when an update fails
- Read reboot status from the post-install search

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
JustinandClaude Opus 5.5 committed 2026-11-17 15:32:00 -06:00
1 parent 8de2386aa1
commit abdca77860
1 file changed
+35 -22
+35 -22
View File
@@ -34,27 +34,40 @@
msg: "{{ search.updates | dict2items | map(attribute='value.title') | list }}" msg: "{{ search.updates | dict2items | map(attribute='value.title') | list }}"
when: search_only | bool when: search_only | bool
- name: Install updates - name: Install updates and report
ansible.windows.win_updates:
category_names: "{{ update_categories }}"
state: installed
reboot: "{{ reboot | bool }}"
reboot_timeout: 3600
register: result
when: not search_only | bool when: not search_only | bool
# With reboot=false, updates blocked by the pending restart (WU_E_INSTALL_NOT_ALLOWED) block:
# are expected; only fail on other errors. - name: Install updates
failed_when: >- ansible.windows.win_updates:
result.failed_update_count | default(0) | int > 0 and category_names: "{{ update_categories }}"
(reboot | bool or state: installed
result.updates | dict2items | map(attribute='value') reboot: "{{ reboot | bool }}"
| selectattr('failure_hresult_code', 'defined') reboot_timeout: 3600
| rejectattr('failure_hresult_code', 'equalto', 2149842966) | list | length > 0) register: result
# With reboot=false, updates blocked by the pending restart (WU_E_INSTALL_NOT_ALLOWED)
# are expected; only fail on other errors.
failed_when: >-
result.failed_update_count | default(0) | int > 0 and
(reboot | bool or
result.updates | dict2items | map(attribute='value')
| selectattr('failure_hresult_code', 'defined')
| rejectattr('failure_hresult_code', 'equalto', 2149842966) | list | length > 0)
- name: Report # Runs even if the install failed, so a failed run still shows what is pending.
ansible.builtin.debug: always:
msg: # Re-search afterwards: win_updates marks updates that were staged for a reboot as
- "Installed: {{ result.updates | dict2items | map(attribute='value') | selectattr('installed') | map(attribute='title') | list }}" # "not installed" even after the reboot applies them, so this is the reliable view.
- "Not installed: {{ result.updates | dict2items | map(attribute='value') | rejectattr('installed') | map(attribute='title') | list }}" - name: Check what is still pending
- "Reboot still required: {{ result.reboot_required }}{{ ' - reboot and re-run to finish' if result.reboot_required else '' }}" ansible.windows.win_updates:
when: not search_only | bool category_names: "{{ update_categories }}"
state: searched
register: after
- name: Report
vars:
reboot_pending: "{{ after.reboot_required | default(result.reboot_required | default(false)) | bool }}"
ansible.builtin.debug:
msg:
- "Processed this run: {{ result.updates | default({}) | dict2items | map(attribute='value.title') | list }}"
- "Still pending: {{ after.updates | default({}) | dict2items | map(attribute='value.title') | list }}"
- "Reboot still required: {{ reboot_pending }}{{ ' - reboot and re-run to finish' if reboot_pending | bool else '' }}"