From 8eec5d34d4c7b27def2543515d18e91a2f925e17 Mon Sep 17 00:00:00 2001 From: Chris Hammer Date: Sat, 17 Feb 2024 22:44:30 -0500 Subject: [PATCH] Refactor boot and LV code --- ansible.cfg | 2 +- bigboot-v2.yml | 44 --------------------------- tasks/bigboot_manage_lv.yml | 29 ++++++++---------- tasks/capture_boot_device_details.yml | 17 +++-------- 4 files changed, 18 insertions(+), 74 deletions(-) delete mode 100644 bigboot-v2.yml diff --git a/ansible.cfg b/ansible.cfg index d58a63d..eabe676 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -7,7 +7,7 @@ gathering = smart gather_timeout = 600 fact_caching = jsonfile fact_caching_connection = /tmp/.ansible_facts -fact_caching_timeout = 300 +fact_caching_timeout = 10 retry_files_enabled = false forks = 40 timeout = 30 diff --git a/bigboot-v2.yml b/bigboot-v2.yml deleted file mode 100644 index c7741fa..0000000 --- a/bigboot-v2.yml +++ /dev/null @@ -1,44 +0,0 @@ ---- -- name: Expand /boot partition on target host - hosts: all - become: true - gather_facts: true - - tasks: - - name: Expand boot partition - block: - - name: Execute Bigboot role to resize /boot - ansible.builtin.import_role: - name: infra.lvm_snapshots.bigboot - - rescue: - - name: Get the target device information and calculate padding - ansible.builtin.set_fact: - bigboot_shrink_target_info: "{{ ansible_facts.mounts \ - | selectattr('device', 'equalto', bigboot_shrink_target) | first }}" - bigboot_size_padded: (bigboot_size | human_to_bytes * bigboot_shrink_padding) | int - - - name: Calculate the new logical volume size - ansible.builtin.set_fact: - shrink_lv_volume_size: "{{ (bigboot_shrink_target_info.size_total - bigboot_size_padded | int) \ - | ansible.builtin.human_readable | ansible.builtin.regex_replace(' ', '') }}" - - - name: Show resize information - ansible.builtin.debug: - msg: "Shrinking {{ bigboot_shrink_target }} from \ - {{ bigboot_shrink_target_info.size_total | ansible.builtin.human_readable }} to \ - {{ shrink_lv_volume_size[:-1] | ansible.builtin.regex_replace('^(\\d+)\\.\\d+(\\w).*', '\\1\\2') }} \ - to expand /boot partition by {{ bigboot_size }}." - - - name: Execute Shrink_LV role to resize target logical volume - ansible.builtin.import_role: - name: infra.lvm_snapshots.shrink_lv - vars: - shrink_lv_devices: - - device: "{{ bigboot_shrink_target }}" - size: "{{ shrink_lv_volume_size[:-1] \ - | ansible.builtin.regex_replace('^(\\d+)\\.\\d+(\\w).*', '\\1\\2') }}" - - - name: Execute Bigboot role to resize /boot - ansible.builtin.import_role: - name: infra.lvm_snapshots.bigboot diff --git a/tasks/bigboot_manage_lv.yml b/tasks/bigboot_manage_lv.yml index 4f94956..45dcb6b 100644 --- a/tasks/bigboot_manage_lv.yml +++ b/tasks/bigboot_manage_lv.yml @@ -17,9 +17,14 @@ bigboot_lv_info: "{{ ansible_facts.mounts \ | selectattr('device', 'equalto', bigboot_adjacent_lvm_device | trim) | first }}" +- name: Assert that there is space on the logical volume for shrinkage + ansible.builtin.assert: + that: bigboot_lv_info.size_available > bigboot_expansion_diff | int + fail_msg: There is not enough space available for LV shrinking. + - name: Capture shrink size for logical volume ansible.builtin.set_fact: - bigboot_lv_shrink_size: "{{ bigboot_lv_info.size_total - bigboot_expansion_diff | human_to_bytes }}" + bigboot_lv_shrink_size: "{{ bigboot_lv_info.size_total - bigboot_expansion_diff | int }}" - name: Capture logical volume name ansible.builtin.shell: @@ -52,31 +57,23 @@ # Free PE / Size 0 / 0 bigboot_lv_vg_free_pe: "{{ bigboot_lv_vg_free_pe.stdout | regex_replace('^.*/.*/\\s+[<]?(.*)', '\\1') }}" -- name: Get size only from the values +- name: Get size in MB for PE and ansible.builtin.set_fact: - bigboot_lv_size_in_mb: + bigboot_lv_pe_size_in_mb: "{{ bigboot_lv_vg_free_pe | regex_replace('i|\\s+|<', '') | human_to_bytes | human_readable(unit='M') }}" - bigboot_size_in_mb: "{{ bigboot_size_target[:-1] }}" -- name: Check if we can shrink the logical volume +- name: Verify if there's available PE or not and execute Shrink_LV block: - - name: Assert if we need to execute the shrink_lv role + - name: Assert if we need to execute the shrink_lv role to gain free PE ansible.builtin.assert: - that: - - bigboot_lv_vg_free_pe[:-4] != '' - - (bigboot_lv_size_in_mb[:-6] | int - bigboot_size_in_mb | int ) >= 0 - fail_msg: Not enough free PE available for /boot expansion. + that: (bigboot_lv_pe_size_in_mb[:-3] | int | round) | int > bigboot_size_target[:-1] | int + fail_msg: Not enough PE to expand /boot. rescue: - - name: Assert there is free space available for shrinking - ansible.builtin.assert: - that: (bigboot_lv_info.size_available - bigboot_size_target | human_to_bytes) >= 0 - fail_msg: "Not enough space available on {{ bigboot_adjacent_lvm_device | trim }} for shrinking." - - name: Execute Shrink_LV role to resize target logical volume ansible.builtin.import_role: name: infra.lvm_snapshots.shrink_lv vars: shrink_lv_devices: - device: "{{ bigboot_adjacent_lvm_device | trim }}" - size: "{{ (bigboot_lv_shrink_size | int / bigboot_shrink_padding) | regex_replace('\\..*$', '') }}" + size: "{{ bigboot_lv_shrink_size | int }}" diff --git a/tasks/capture_boot_device_details.yml b/tasks/capture_boot_device_details.yml index 14a9fd7..57d8b28 100644 --- a/tasks/capture_boot_device_details.yml +++ b/tasks/capture_boot_device_details.yml @@ -12,9 +12,9 @@ ansible.builtin.set_fact: bigboot_boot_partsize: "{{ ansible_devices[bigboot_boot_device[:-1]].partitions[bigboot_boot_device].size }}" -- name: Debug bigboot_boot_partsize +- name: Display current parition size for /boot ansible.builtin.debug: - var: bigboot_boot_partsize + msg: "/boot ({{ bigboot_boot_mount.device }}): {{ bigboot_boot_partsize }}" - name: Capture required expansion space ansible.builtin.set_fact: @@ -22,22 +22,13 @@ "{{ bigboot_size | human_to_bytes - bigboot_boot_partsize | human_to_bytes }}" no_log: false -- name: Debug bigboot_expansion_diff - ansible.builtin.debug: - var: bigboot_expansion_diff - -- name: Boo - ansible.builtin.meta: end_host - - name: Convert size difference to MB ansible.builtin.set_fact: bigboot_size_expansion_mb: "{{ bigboot_expansion_diff | int | human_readable(unit='M') }}" - name: Set bigboot size 4k aligned ansible.builtin.set_fact: - bigboot_size_target: "{{ bigboot_size_expansion_mb[:-2] \ - | regex_replace('\\.\\d+ ', '') \ - | int | get_block_size_up }}" + bigboot_size_target: "{{ bigboot_size_expansion_mb[:-2] | int | get_block_size_up }}" - name: Validate if we need to expand boot block: @@ -53,4 +44,4 @@ rescue: - name: Expansion of /boot required ansible.builtin.debug: - msg: "Expanding /boot by an additional {{ bigboot_size_target }}." + msg: "Will need to expand /boot by an additional {{ bigboot_size_target }}."