diff --git a/win-updates-troubleshooting.yaml b/win-updates-troubleshooting.yaml index 7f24470..ff9fdf6 100644 --- a/win-updates-troubleshooting.yaml +++ b/win-updates-troubleshooting.yaml @@ -3,13 +3,6 @@ hosts: windows gather_facts: no - vars: - # ===== Toggle these to TEST failure handling ===== - simulate_failure: false # set to true to mark ALL hosts as failed - simulate_failure_hosts: [] # or list specific hosts, e.g. ['SERVER01','SERVER02'] - simulate_failed_update_count: 0 # optional: pretend N failed updates on simulated hosts - # ===========================AWX===================== - tasks: - name: Get current timestamp set_fact: @@ -20,11 +13,19 @@ path: 'C:\Temp\windows_updates_with_kb.txt' register: kb_updates_file - - name: Fail if updates report file is missing - fail: - msg: 'KB updates report file not found at C:\Temp\windows_updates_with_kb.txt. Please run the assessment playbook first.' + # ---- DO NOT hard-fail; flag host and stop further tasks on this host ---- + - name: Mark host failed if report is missing (but continue overall run) + set_fact: + patch_failed_host: true + patch_failed_count: "{{ (patch_failed_count | default(0) | int) + 1 }}" + patch_fail_reason: "KB report missing at C:\\Temp\\windows_updates_with_kb.txt" when: not kb_updates_file.stat.exists + - name: Stop further tasks on this host (report missing) + meta: end_host + when: not kb_updates_file.stat.exists + # ------------------------------------------------------------------------ + - name: Read KB updates report content win_shell: Get-Content -Path 'C:\Temp\windows_updates_with_kb.txt' register: updates_content @@ -84,17 +85,6 @@ patch_failed_host: "{{ patch_failed_host | default(false) }}" patch_failed_count: "{{ patch_failed_count | default(0) | int }}" - # ---- FAILURE SIMULATION (enable via vars above) ---- - - name: Simulate patch failure (all hosts or specific hosts) - set_fact: - patch_failed_host: true - patch_failed_count: >- - {{ (simulate_failed_update_count | int) - if (simulate_failed_update_count | int) > 0 - else ((patch_failed_count | default(0) | int) + 1) }} - when: - - simulate_failure | bool or (inventory_hostname in simulate_failure_hosts) - - name: Display installation summary debug: msg: @@ -155,29 +145,9 @@ - kb_updates_file.stat.exists - (kb_numbers is not defined or kb_numbers | length == 0) - # ---- Aggregate & publish facts to localhost for next play ---- - - name: Collect failed hosts (no extract filter; safe default) - run_once: true - delegate_to: localhost - delegate_facts: true - set_fact: - failed_hosts_list: "{{ (failed_hosts_list | default([])) + [item] }}" - loop: "{{ ansible_play_hosts_all }}" - when: hostvars[item].patch_failed_host | default(false) - - - name: Publish aggregate flags to localhost (safe even if none failed) - run_once: true - delegate_to: localhost - delegate_facts: true - vars: - _fails: "{{ hostvars['localhost'].failed_hosts_list | default([]) }}" - set_fact: - any_patch_failed: "{{ (_fails | length) > 0 }}" - failed_hosts_csv: "{{ _fails | join(', ') if (_fails | length) > 0 else 'None' }}" - # ------------------------------------------------------------------------------ -- name: Post patching results to SharePoint (Graph) +- name: Aggregate results and post to SharePoint (always runs) hosts: localhost connection: local gather_facts: false @@ -202,9 +172,28 @@ URL={{ job_url }}. tasks: - - name: Build final status from published facts + - name: Init failed hosts list set_fact: - status_final: "{{ 'failed' if (hostvars['localhost'].any_patch_failed | default(false)) else 'successful' }}" + failed_hosts_list: [] + + - name: Collect hosts that flagged patch failure + set_fact: + failed_hosts_list: "{{ failed_hosts_list + [item] }}" + loop: "{{ groups['windows'] | default([]) }}" + when: hostvars[item].patch_failed_host | default(false) + + - name: Compute final status and CSV + set_fact: + any_patch_failed: "{{ (failed_hosts_list | length) > 0 }}" + failed_hosts_csv: "{{ failed_hosts_list | join(', ') if failed_hosts_list | length > 0 else 'None' }}" + status_final: "{{ 'failed' if (failed_hosts_list | length) > 0 else 'successful' }}" + + - name: Sanity — status to post + debug: + msg: + status_final: "{{ status_final }}" + failed_hosts_csv: "{{ failed_hosts_csv }}" + failed_hosts_list: "{{ failed_hosts_list }}" - name: Acquire Graph token (client credentials) uri: @@ -239,7 +228,7 @@ RunEnd: "{{ run_end }}" Notes: |- {{ summary_text }} - Failed hosts: {{ hostvars['localhost'].failed_hosts_csv | default('None') }} + Failed hosts: {{ failed_hosts_csv }} register: sp_create ignore_errors: true no_log: true