---
- name: Capture logical volume adjacent to /boot
  ansible.builtin.shell:
    cmd: |
      set -o pipefail
      lsblk -p -o name,type|grep lvm|head -1
    executable: /bin/bash
  changed_when: false
  register: bigboot_adjacent_lvm

- name: Set adjacent LVM device name
  ansible.builtin.set_fact:
    bigboot_adjacent_lvm_device: "{{ bigboot_adjacent_lvm.stdout | regex_replace('.*(/dev.*)\\s+.*$', '\\1') }}"

- name: Get logical volume mount information
  ansible.builtin.set_fact:
    bigboot_lv_info: "{{ ansible_facts.mounts \
      | selectattr('device', 'equalto', bigboot_adjacent_lvm_device | trim) | first }}"

- 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 }}"

- name: Capture logical volume name
  ansible.builtin.shell:
    cmd: |
      set -o pipefail
      lvdisplay {{ bigboot_adjacent_lvm_device }} | grep -i 'vg name'
    executable: /bin/bash
  changed_when: false
  register: bigboot_lv_vg_name

- name: Format logical volume name
  ansible.builtin.set_fact:
    bigboot_lv_vg_name: "{{ bigboot_lv_vg_name.stdout | regex_replace('VG\\s+Name\\s+(.*)$', '\\1') }}"

- name: Capture volume group free PE
  ansible.builtin.shell:
    cmd: |
      set -o pipefail
      vgdisplay {{ bigboot_lv_vg_name | trim }} | grep -i 'free'
    executable: /bin/bash
  changed_when: false
  register: bigboot_lv_vg_free_pe

- name: Format logical volume free PE
  ansible.builtin.set_fact:
    # Ex:
    #   Free  PE / Size       320 / 1.25 GiB"
    #   Free  PE / Size       189 / 756.00 MiB"
    #   Free  PE / Size       414 / <1.62 GiB
    #   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
  ansible.builtin.set_fact:
    bigboot_lv_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
  block:
    - name: Assert if we need to execute the shrink_lv role
      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.

  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('\\..*$', '') }}"
