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!