How To Extend A Partition And Logical Volume Using Ansible On RHEL/CentOS 8.x

As the number of Linux servers grows, so does the need for efficient and automated systems management. In this context, Ansible emerges as a powerful tool that allows administrators to manage large numbers of servers in an organized and standardized way. If you’re looking to extend a partition and logical volume on a Red Hat Enterprise Linux (RHEL) or CentOS server, Ansible can make the task much easier. In this blog post, we’ll show you how to use Ansible to extend a partition and logical volume in a consistent and repeatable manner, ensuring that your servers remain healthy and functioning. Whether you’re new to Ansible or an experienced user, this guide will provide you with the knowledge and skills to manage your storage needs with confidence.

A typical scenario in which this method could be used is if a managed virtual machine is rapidly running out of free space, you’ve already increased the size of the disk device and now you need to make all of that additional space available to the operating system.

Extend A Partition And Logical Volume
Example of listed block devices with standard RHEL partitioning

In the example image above, the output from listing block devices shows that /dev/sda is 40GB with currently only 20GB being used (root lv is 17GB, /boot is 1GB and 2GB of swap). I will be assigning all of the remaining 20GB of free space to the root logical volume.

1. Multiple methods exist for the initial tasks. Do I want to extend the partition /dev/sda2 or create a new partition /dev/sda3? Either option will work for us and I will go with extending /dev/sda2. Also, do I want to perform the partitioning operation using parted (and the ansible parted module) or fdisk?
Personally I prefer to use fdisk where possible, I just find it more user friendly and don’t have to worry about figuring out at which block a new partition starts. The downside is there is no module for fdisk in ansible. Instead we will use the ‘expect’ module to pass the interactions fdisk requires. In turn the expect module requires the expect package to be installed on the managed machine, and we achieve this using pip.
Important: fdisk prompts do sometimes vary between distros and versions, so your regex responses defined in the task could vary from what I’ve used below. I usually account for this by using multiple tasks with conditional when statements.

---
# Install pexpect package via pip3
- name: install pexpect
  command: pip3 install pexpect

# Delete and create new partition on /dev/sda
- name: create new partition
  expect:
    command: /sbin/fdisk /dev/sda
    responses:
      'Command.*$':
        - 'd'            
        - 'n'
        - 't'
        - 'w'
      'Partition number.*$': '\n' ## \n selects fdisk default choice ##
      'Select.*$': '\n'
      'First sector.*$': '\n'
      'Last sector.*$': '\n'
      'Do you want.*$': 'n' ## selects 'no' on removing LVM signature ##
      'Hex code.*$': '08e' ## selects LVM partition type ##
    echo: yes
    timeout: 30
  when: (ansible_os_family == "RedHat" and ansible_distribution_version|float >= 8)

2. The final tasks deal with configuring the changes required to LVM but before that we also need to inform the kernel of the change we just made to /dev/sda using the partx command.

# Update kernel partition table
- name: update kernel partition table
  command: partx -u /dev/sda

# Resize VG rhel on top of PV /dev/sda2 to utilize additional free space
- name: resize volume group
  lvg:
    vg: rhel
    pvs: /dev/sda2

# Extend logical Volume root to include additional free space
- name: extend logical volume
  lvol:
    vg: rhel
    lv: root
    resizefs: yes
    pvs: /dev/sda2

Leave a Reply

Your email address will not be published. Required fields are marked *