---
- name: Get /boot mount information
  ansible.builtin.set_fact:
    bigboot_boot_mount: "{{ ansible_facts.mounts \
      | selectattr('mount', 'equalto', '/boot') | first }}"

- name: Capture device for /boot
  ansible.builtin.set_fact:
    bigboot_boot_device: "{{ bigboot_boot_mount.device | regex_replace('^.*/(\\w+$)', '\\1') }}"

- name: Capture partition size for /boot parition
  ansible.builtin.set_fact:
    bigboot_boot_partsize: "{{ ansible_devices[bigboot_boot_device[:-1]].partitions[bigboot_boot_device].size }}"

- name: Display current parition size for /boot
  ansible.builtin.debug:
    msg: "/boot ({{ bigboot_boot_mount.device }}): {{ bigboot_boot_partsize }}"

- name: Capture required expansion space
  ansible.builtin.set_fact:
    bigboot_expansion_diff:
      "{{ bigboot_size_target | human_to_bytes - bigboot_boot_partsize | human_to_bytes }}"

- name: Validate if we need to expand boot
  block:
    - name: Set flag for Bigboot execution
      ansible.builtin.set_fact:
        bigboot_execute_bigboot: false

    - name: Assert that /boot requires expansion
      ansible.builtin.assert:
        that: bigboot_expansion_diff | int <= 0
        fail_msg: The /boot partition will need to be resized
        success_msg: The /boot partition is already at the desired size

  rescue:
    - name: "Check if /boot is already at or above {{ bigboot_size_min }}"
      block:
        - name: "Assert that /boot is already at or above {{ bigboot_size_min }}"
          ansible.builtin.assert:
            that: bigboot_boot_partsize | human_to_bytes >= bigboot_size_min | human_to_bytes
            success_msg: >
              /boot partition size is already at least {{ bigboot_size_min }}
              or greater. Nothing to do."
            fail_msg: >
              /boot partition size is less than {{ bigboot_size_min }}.
              Expansion of /boot is required.

      rescue:
        - name: Set flag for Bigboot execution
          ansible.builtin.set_fact:
            bigboot_execute_bigboot: true

        - name: Expansion of /boot required
          ansible.builtin.debug:
            msg: >
              Will attempt to increase to {{ bigboot_size_target }}, or
              {{ bigboot_size_min }} at the minimum."
