From 47ff87b618d506f2a6e07d4a09b148ccb7b7032b Mon Sep 17 00:00:00 2001 From: Ryan Hamilton Date: Tue, 27 May 2025 18:33:18 +0000 Subject: [PATCH] Add playbooks/check_unallocated_disk.yml --- playbooks/check_unallocated_disk.yml | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 playbooks/check_unallocated_disk.yml diff --git a/playbooks/check_unallocated_disk.yml b/playbooks/check_unallocated_disk.yml new file mode 100644 index 0000000..d28826c --- /dev/null +++ b/playbooks/check_unallocated_disk.yml @@ -0,0 +1,46 @@ +--- +- name: Check unallocated disk space + hosts: all + become: true + gather_facts: false + + tasks: + + - name: Get block device sizes + command: lsblk -b -o NAME,SIZE,TYPE -dn + register: lsblk_output + + - name: Parse lsblk output into structured facts + set_fact: + disks: "{{ disks | default({}) | combine({ item.split()[0]: item.split()[1]|int }) }}" + loop: "{{ lsblk_output.stdout_lines }}" + when: "'disk' in item" + + - name: Get partition sizes for each disk + command: lsblk -b -o NAME,SIZE,TYPE -n + register: partition_output + + - name: Initialize disk usage map + set_fact: + used_space: "{{ used_space | default({}) }}" + + - name: Sum partition sizes under each disk + set_fact: + used_space: >- + {{ + used_space | combine({ + (item.0): (used_space[item.0]|default(0)) + (item.1.split()[1]|int) + }) + }} + with_nested: + - "{{ disks.keys() | list }}" + - "{{ partition_output.stdout_lines }}" + when: item.1.startswith(item.0) and 'part' in item.1 + + - name: Show disk usage summary + debug: + msg: >- + Disk /dev/{{ item.key }}: Total {{ item.value|int // 1024**3 }} GB, + Used {{ used_space[item.key]|default(0)|int // 1024**3 }} GB, + Unallocated {{ (item.value|int - used_space[item.key]|default(0)|int) // 1024**3 }} GB + loop: "{{ disks|dict2items }}"