--- - name: Check Free Disk Space hosts: windows gather_facts: false tasks: - name: "Check Free Disk Space in C:" win_shell: | $freeSpace = [math]::Round((Get-PSDrive C | Select-Object Free).Free / 1GB, 2) Write-Output $freeSpace register: freediskspace - name: Report Free Disk Space debug: msg: "Free disk space on C: drive: {{ freediskspace.stdout | trim }} GB" when: freediskspace is defined and freediskspace.stdout is defined - name: Fail if there is not Enough Space fail: msg: "the node {{ inventory_hostname }} has insufficient disk space: {{ freediskspace.stdout | trim }} GB (minimum required: 20 GB)" when: - freediskspace.stdout | trim | float < 20 - name: Find Recovery partitions (PowerShell → JSON) ansible.windows.win_powershell: script: | $ErrorActionPreference = 'Stop' $guid = '{DE94BBA4-06D1-4D40-A16A-BFD50179D6AC}' $parts = @( Get-Partition | Where-Object { $_.GptType -eq $guid -or $_.Type -eq 'Recovery' -or $_.Type -eq 'Unknown' } | Select-Object DiskNumber, PartitionNumber, @{n='SizeBytes';e={$_.Size}}, @{n='SizeMB';e={[math]::Round($_.Size/1MB,2)}}, GptType, Type ) $parts = @($parts) $parts | ConvertTo-Json -Compress -Depth 4 register: winre_raw changed_when: false - name: Parse JSON (string → object) ansible.builtin.set_fact: winre_parts: "{{ (winre_raw.output | join('') | trim | default('[]')) | from_json }}" - name: Normalize to list ansible.builtin.set_fact: winre_parts: "{{ [winre_parts] }}" when: winre_parts is mapping # --- threshold (MiB) -> bytes - name: Set Recovery size threshold ansible.builtin.set_fact: recovery_threshold_mib: 500 recovery_threshold_bytes: "{{ 500 * 1024 * 1024 }}" changed_when: false # Smallest Recovery partition on this host (if any) - name: Compute smallest Recovery partition size (bytes) ansible.builtin.set_fact: recovery_min_bytes: "{{ (winre_parts | map(attribute='SizeBytes') | list) | min }}" when: winre_parts | length > 0 changed_when: false # Fail this host if it has a Recovery partition smaller than 500 MiB - name: Enforce minimum Recovery partition size ansible.builtin.fail: msg: >- Recovery partition too small on {{ inventory_hostname }}: smallest={{ (recovery_min_bytes / 1024 / 1024) | round(2) }} MiB, required >= {{ recovery_threshold_mib }} MiB. Details: {{ winre_parts }} when: - winre_parts | length > 0 - recovery_min_bytes < recovery_threshold_bytes - name: Show Recovery partition sizes ansible.builtin.debug: msg: "Disk {{ item.DiskNumber }}, Part {{ item.PartitionNumber }}, Size: {{ item.SizeMB }} MB ({{ item.SizeBytes }} bytes)" loop: "{{ winre_parts }}"