Update win-updates-troubleshooting.yaml

This commit is contained in:
2025-08-14 09:24:20 +00:00
parent 99db5e2aca
commit d91792dc47

View File

@@ -3,13 +3,6 @@
hosts: windows hosts: windows
gather_facts: no 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: tasks:
- name: Get current timestamp - name: Get current timestamp
set_fact: set_fact:
@@ -20,11 +13,19 @@
path: 'C:\Temp\windows_updates_with_kb.txt' path: 'C:\Temp\windows_updates_with_kb.txt'
register: kb_updates_file register: kb_updates_file
- name: Fail if updates report file is missing # ---- DO NOT hard-fail; flag host and stop further tasks on this host ----
fail: - name: Mark host failed if report is missing (but continue overall run)
msg: 'KB updates report file not found at C:\Temp\windows_updates_with_kb.txt. Please run the assessment playbook first.' 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 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 - name: Read KB updates report content
win_shell: Get-Content -Path 'C:\Temp\windows_updates_with_kb.txt' win_shell: Get-Content -Path 'C:\Temp\windows_updates_with_kb.txt'
register: updates_content register: updates_content
@@ -84,17 +85,6 @@
patch_failed_host: "{{ patch_failed_host | default(false) }}" patch_failed_host: "{{ patch_failed_host | default(false) }}"
patch_failed_count: "{{ patch_failed_count | default(0) | int }}" 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 - name: Display installation summary
debug: debug:
msg: msg:
@@ -155,29 +145,9 @@
- kb_updates_file.stat.exists - kb_updates_file.stat.exists
- (kb_numbers is not defined or kb_numbers | length == 0) - (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 hosts: localhost
connection: local connection: local
gather_facts: false gather_facts: false
@@ -202,9 +172,28 @@
URL={{ job_url }}. URL={{ job_url }}.
tasks: tasks:
- name: Build final status from published facts - name: Init failed hosts list
set_fact: 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) - name: Acquire Graph token (client credentials)
uri: uri:
@@ -239,7 +228,7 @@
RunEnd: "{{ run_end }}" RunEnd: "{{ run_end }}"
Notes: |- Notes: |-
{{ summary_text }} {{ summary_text }}
Failed hosts: {{ hostvars['localhost'].failed_hosts_csv | default('None') }} Failed hosts: {{ failed_hosts_csv }}
register: sp_create register: sp_create
ignore_errors: true ignore_errors: true
no_log: true no_log: true