RHCE 9.0 Practice Exam: Creating and Managing Logical Volumes (LVM) Using Ansible

RHCE 9.0 Practice Exam: Creating and Managing Logical Volumes (LVM) Using Ansible
Photo by Taylor Vick / Unsplash

πŸ“Œ 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

  1. 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.
  2. Error Handling:
  3. 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! πŸ”₯

Read more