Files
homelab/ansible/roles/expand_root_lv/tasks/main.yml
JARVIS 9ed7466fd8 feat(expand_root_lv): new role to grow root LV to fill VG + resize fs
Reclaims the half-disk LV left by the Ubuntu Server autoinstall
template default. Idempotent — no-ops cleanly when there are no free PE
in the VG, and exits the play cleanly on hosts without LVM.

Supports ext4 and xfs. Does not handle partition resize (cloud-utils
growpart) — out of scope for the template fix-up case.

Wired into day1_deploy_honcho.yml ahead of the honcho role so newly
provisioned VMs get the fix-up automatically. Suitable to add to any
day1 playbook by simply listing it before the application role.
2026-05-30 23:03:10 -05:00

71 lines
2.4 KiB
YAML

---
# ============================================================================
# expand-root-lv / main
# ----------------------------------------------------------------------------
# 1. Confirm the target VG exists (skip role cleanly on non-LVM hosts).
# 2. Read free physical-extent count for the VG.
# 3. Extend the LV to +100%FREE only when free_pe > 0.
# 4. Grow the filesystem on the mountpoint (ext4 -> resize2fs, xfs -> xfs_growfs).
# Each step is idempotent and skips when there's nothing to do.
# ============================================================================
- name: Gather LVM facts
ansible.builtin.command:
cmd: "vgs --noheadings --nosuffix --units b -o vg_name,vg_free_count {{ expand_root_lv_vg_name }}"
register: vg_info
changed_when: false
failed_when: false
- name: Skip role when target VG is absent
ansible.builtin.meta: end_play
when: vg_info.rc != 0
- name: Parse free PE count
ansible.builtin.set_fact:
expand_root_lv_free_pe: "{{ (vg_info.stdout.split() | last | int) if vg_info.stdout | length > 0 else 0 }}"
- name: Extend LV to fill VG (only if free PE > 0)
ansible.builtin.command:
cmd: "lvextend -l +100%FREE /dev/{{ expand_root_lv_vg_name }}/{{ expand_root_lv_lv_name }}"
register: lvextend_result
when: expand_root_lv_free_pe | int > 0
changed_when: lvextend_result.rc == 0
- name: Detect filesystem type at mountpoint
ansible.builtin.command:
cmd: "findmnt {{ expand_root_lv_mountpoint }} -no FSTYPE"
register: fstype_result
changed_when: false
- name: Set filesystem type fact
ansible.builtin.set_fact:
expand_root_lv_fstype: "{{ fstype_result.stdout | trim }}"
- name: Grow ext4 filesystem
ansible.builtin.command:
cmd: "resize2fs /dev/{{ expand_root_lv_vg_name }}/{{ expand_root_lv_lv_name }}"
register: resize_result
when:
- expand_root_lv_fstype == "ext4"
- lvextend_result.changed | default(false)
changed_when: resize_result.rc == 0
- name: Grow xfs filesystem
ansible.builtin.command:
cmd: "xfs_growfs {{ expand_root_lv_mountpoint }}"
register: xfs_result
when:
- expand_root_lv_fstype == "xfs"
- lvextend_result.changed | default(false)
changed_when: xfs_result.rc == 0
- name: Report current root size
ansible.builtin.command:
cmd: "df -h {{ expand_root_lv_mountpoint }}"
register: df_result
changed_when: false
- name: Show post-resize disk usage
ansible.builtin.debug:
msg: "{{ df_result.stdout_lines }}"