From abdca77860643c84e3eee7b822c0da7031419ba7 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 17 Nov 2026 15:31:31 -0600 Subject: [PATCH] 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 --- windows/update.yaml | 57 ++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/windows/update.yaml b/windows/update.yaml index fd8ec9c..654695c 100644 --- a/windows/update.yaml +++ b/windows/update.yaml @@ -34,27 +34,40 @@ msg: "{{ search.updates | dict2items | map(attribute='value.title') | list }}" when: search_only | bool - - name: Install updates - ansible.windows.win_updates: - category_names: "{{ update_categories }}" - state: installed - reboot: "{{ reboot | bool }}" - reboot_timeout: 3600 - register: result + - name: Install updates and report when: not search_only | bool - # 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) + block: + - name: Install updates + ansible.windows.win_updates: + category_names: "{{ update_categories }}" + state: installed + reboot: "{{ reboot | bool }}" + reboot_timeout: 3600 + 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 - ansible.builtin.debug: - msg: - - "Installed: {{ result.updates | dict2items | map(attribute='value') | selectattr('installed') | map(attribute='title') | list }}" - - "Not installed: {{ result.updates | dict2items | map(attribute='value') | rejectattr('installed') | map(attribute='title') | list }}" - - "Reboot still required: {{ result.reboot_required }}{{ ' - reboot and re-run to finish' if result.reboot_required else '' }}" - when: not search_only | bool + # Runs even if the install failed, so a failed run still shows what is pending. + always: + # Re-search afterwards: win_updates marks updates that were staged for a reboot as + # "not installed" even after the reboot applies them, so this is the reliable view. + - name: Check what is still pending + ansible.windows.win_updates: + 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 '' }}"