RHCE 9.0 Practice Exam: Creating and Managing Logical Volumes (LVM) Using Ansible
π Article Overview
This tutorial is designed for RHCE 9.0 certification candidates and explains how to use Ansible to create logical volumes (LV) in a volume group (VG), apply error handling, and verify the configuration.
π‘ What You Will Learn:
β
How to create an Ansible Playbook to manage Logical Volumes (LVM)
β
How to create a logical volume (LV) inside a specific volume group (VG)
β
How to apply conditional logic to resize the LV in case of failure
β
How to debug and validate the created logical volume
β
How to verify the correct filesystem creation using Ansible and Linux commands
π 1. Task Requirements
π 1.1. Problem Statement
- Create a logical volume (LV) that meets the following criteria:
- Created inside the
research
volume group. - Logical volume name:
data
. - Logical volume size: 1500 MiB, but fallback to 800 MiB if insufficient space.
- Formatted as
ext4
filesystem.
- Created inside the
- Error Handling:
- Do NOT mount the logical volume.
If the volume group research
does not exist, display:
Volume group does not exist
If the requested LV size is unavailable, display:
Could not create logical volume of that size
π 2. Writing the Ansible Playbook
π 2.1. Create the Playbook File
$ vim /home/greg/ansible/lv.yml
π Add the Following Content:
---
- name: Create Logical Volume (LV) in Research Volume Group
hosts: all
become: yes
tasks:
- name: Check if Volume Group "research" exists
command: vgs research
register: vg_status
ignore_errors: yes
changed_when: false
- name: Display error if VG does not exist
ansible.builtin.debug:
msg: "Volume group does not exist"
when: vg_status.rc != 0
- name: Attempt to create a 1500 MiB logical volume
community.general.lvol:
vg: research
lv: data
size: 1500
register: lvol_result
ignore_errors: yes
when: vg_status.rc == 0
- name: Display error if 1500M LV creation fails
ansible.builtin.debug:
msg: "Could not create logical volume of that size"
when: lvol_result.failed is defined and lvol_result.failed
- name: Attempt to create an 800 MiB logical volume
community.general.lvol:
vg: research
lv: data
size: 800
when: lvol_result.failed is defined and lvol_result.failed
- name: Format the logical volume with ext4 filesystem
community.general.filesystem:
fstype: ext4
dev: /dev/research/data
when: vg_status.rc == 0 and not lvol_result.failed
π Key Features:
- Checks if VG
research
exists (vgs research
). - Attempts to create a 1500 MiB LV (
community.general.lvol
). - If it fails, logs an error and creates an 800 MiB LV instead.
- Formats the LV with
ext4
only if successful.
πΎ Save and exit: (ESC
β :wq
)
π 3. Running the Playbook
π 3.1. Execute the Playbook
$ ansible-navigator run /home/greg/ansible/lv.yml -m stdout
π Expected Output
TASK [Gathering Facts]
***********************************************************************
ok: [node5]
ok: [node3]
ok: [node2]
ok: [node4]
ok: [node1]
TASK [Create a logical volume]
***********************************************************************
skipping: [node1]
fatal: [node3]: FAILED! => {"changed": false, "err": " Volume group
'research' has insufficient free space (31 extents): 47 required.\n",
"msg": "Creating logical volume 'data' failed", "rc": 5}
changed: [node5]
changed: [node4]
changed: [node2]
TASK [Create ext4 filesystem]
***********************************************************************
skipping: [node1]
changed: [node5]
changed: [node2]
changed: [node4]
TASK [ansible.builtin.debug]
***********************************************************************
ok: [node3] => {
"msg": "Could not create logical volume of that size"
}
TASK [Create a logical volume]
***********************************************************************
changed: [node3]
TASK [debug]
***********************************************************************
skipping: [node2]
skipping: [node5]
skipping: [node3]
ok: [node1] => {
"msg": "Volume group does not exist"
}
skipping: [node4]
PLAY RECAP
***********************************************************************
node1 : ok=2 changed=0 unreachable=0 failed=0
skipped=2 rescued=0 ignored=0
node2 : ok=3 changed=2 unreachable=0 failed=0
skipped=1 rescued=0 ignored=0
node3 : ok=3 changed=1 unreachable=0 failed=0
skipped=1 rescued=1 ignored=0
node4 : ok=3 changed=2 unreachable=0 failed=0
skipped=1 rescued=0 ignored=0
node5 : ok=3 changed=2 unreachable=0 failed=0
skipped=1 rescued=0 ignored=0
β Logical volumes were successfully created with appropriate error handling!
π 4. Common Issues & Troubleshooting
π΄ Issue 1: Volume Group research
Does Not Exist
β
Solution: Ensure research
VG exists:
$ ansible all -m shell -a 'vgs research'
If missing, create it:
$ ansible all -m shell -a 'vgcreate research /dev/sdb'
π΄ Issue 2: Insufficient Space for 1500 MiB Logical Volume
β Solution: Check VG size:
$ ansible all -m shell -a 'vgs research'
Extend VG if needed:
$ ansible all -m shell -a 'vgextend research /dev/sdc'
π΄ Issue 3: Logical Volume data
Already Exists
β Solution: Remove and retry:
$ ansible all -m shell -a 'lvremove -y /dev/research/data'
π΄ Issue 4: Filesystem Creation Fails
β Solution: Ensure LV is not in use:
$ ansible all -m shell -a 'umount /dev/research/data'
Then reformat:
$ ansible all -m shell -a 'mkfs.ext4 /dev/research/data'
π Congratulations! You have successfully used Ansible to create and manage Logical Volumes in RHCE 9.0! π
π’ If you found this guide helpful, share it with your RHCE 9.0 study group! π’
π₯ Good luck on your RHCE 9.0 exam! π₯