Trouble Creating Partition

Hi Community,
I need help to make a rescue block. I thought I was doing everything right, but I guess I am not. I am trying to create a partition using multiple plays. The first one is to check if the disk is available. If not, then throw an error. The second one is to create a 1G partition. If the space is unavailable, create a partition for whatever space is left on the desk. Rescue will make it work by creating a partition from the leftover space. The sdb disk is available; earlier, I created a partition with 800MB. The disk still has 224 spaces left. For some reason, it is not using the rescue part at all. My brain is not working anymore. How can we achieve that? The goal is to create a specific partition size; if there isn’t enough space, make one, whatever is available, instead of failing.

---
- name: Partition playbook
  hosts: webserver
  become: yes
  tasks:
  - name: check disk avaialbility
    block:
      - name: if sdb doesn't exist then throw an error
        debug:
          msg: "sdb disk doesn't exist"
        when: '"sdb" not in ansible_devices'
      - name: create 1 GB partiton
        parted:
          device: /dev/sdb
          number: 1
          part_end: 1GiB
          state: present
        when: '"sdb" in ansible_devices'
    rescue:
      - name: Display a message about space
        debug:
          msg: "There isn't enought space in in the disk"
        when: '"sdb2" not in ansible_devices'
      - name: create partition whatever space available
        parted:
          device: /dev/sdb
          number: 2
          part_end: "100%"
          state: present
        when: '"sdb" in ansible_devices'
~                                       
[ansible@ansible-server ansible]$ ansible-playbook storage.yml

PLAY [Partition playbook] ***************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [node1]

TASK [if sdb doesn't exist then throw an error] *****************************************************************************************************************************************************************
skipping: [node1]

TASK [create 1 GB partiton] *************************************************************************************************************************************************************************************
ok: [node1]

PLAY RECAP ******************************************************************************************************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
[ansible@node1 ~]$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda           8:0    0   10G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0    9G  0 part
  ├─rl-root 253:0    0    8G  0 lvm  /
  └─rl-swap 253:1    0    1G  0 lvm  [SWAP]
sdb           8:16   0    1G  0 disk
└─sdb1        8:17   0  799M  0 part
sdc           8:32   0    1G  0 disk
sr0          11:0    1 1024M  0 rom
[ansible@node1 ~]$

Thank you for your help!

---
- name: Partition playbook
  hosts: webserver
  become: yes
  tasks:
    - name: Gather disk facts
      ansible.builtin.setup:
        gather_subset: '!all,!min,disks'
      register: facts_result

    - name: Debug disk facts
      ansible.builtin.debug:
        var: ansible_devices

    - name: Manage sdb partition
      block:
        - name: Fail if sdb disk doesn’t exist
          ansible.builtin.assert:
            that: "'sdb' in ansible_devices"
            fail_msg: "sdb disk doesn’t exist"
            success_msg: "sdb disk found"

        - name: Check available space on sdb
          ansible.builtin.shell: |
            parted /dev/sdb unit MiB print free | grep 'Free Space' | tail -n 1 | awk '{print $3}' | sed 's/MiB//'
          register: free_space
          changed_when: false

        - name: Debug free space
          ansible.builtin.debug:
            msg: "Free space on sdb: {{ free_space.stdout }} MiB"

        - name: Ensure enough space for 1GB partition
          ansible.builtin.assert:
            that: "free_space.stdout | float >= 1024"
            fail_msg: "Insufficient space for 1GB partition ({{ free_space.stdout }} MiB available)"
            success_msg: "Enough space for 1GB partition"

        - name: Create 1GB partition
          community.general.parted:
            device: /dev/sdb
            number: 2  # Use next partition number after sdb1
            part_start: "799MiB"  # Start after existing 799MB partition
            part_end: "1823MiB"   # 799 + 1024 = 1823
            state: present
          register: parted_result

      rescue:
        - name: Display space warning
          ansible.builtin.debug:
            msg: "Insufficient space for 1GB; creating partition with remaining space"

        - name: Create partition with remaining space
          community.general.parted:
            device: /dev/sdb
            number: 2
            part_start: "799MiB"  # After existing partition
            part_end: "100%"      # Use all remaining space
            state: present
          register: rescue_parted_result

        - name: Debug rescue partition result
          ansible.builtin.debug:
            var: rescue_parted_result

    - name: Verify partitions
      ansible.builtin.shell: lsblk /dev/sdb
      register: lsblk_result
    - name: Show partition layout
      ansible.builtin.debug:
        msg: "{{ lsblk_result.stdout_lines }}"