Ansible

Ansible LVOL

Ansible is a free, open-source software provisioning, configuration management, and application deployment tool. It is a fantastic tool that connects to the defined remote hosts and runs the granular programs called Ansible modules. These modules are run over SSH by default and ensure that Ansible performs the defined tasks without additional configuration on the remote host.

Logical Volumes (LV) are a Logical Volume Manager (LVM) component which is a device mapper framework provided by Linux to manage the disk drives. It abstracts the physical storage which allows the users to create logical storage volumes.

Some of the common uses of the LVM include:

  • Resize the volume groups online by absorbing new physical volumes (PV) or ejecting the existing ones
  • Resize the logical volumes online
  • Create snapshot volumes
  • Move the volumes across physical volumes
  • Split or merge the volume groups without data loss

Components of LVM

The following are some standard components of the LVM in Linux:

  • Physical Volumes – It represents storage devices.
  • Volume Groups – It acts as a container for one or more PVs.
  • Logical Volumes – It resides within VG and represent the block devices that are analogous to partitions in a disk.

This tutorial explores how we can work with the LVOL module in Ansible to configure the LVM logical volumes.

Ansible LVOL Module

The ansible LVOL module is part of the “community.general” collections. It allows us to create, remote, or resize the logical volumes in a Linux system.

If you do not have this collection installed, you can use the ansible-galaxy command as follows:

$ ansible-galaxy collection <strong>install</strong> community.general

Once installed, you can use the module that we will provide in the following examples.

Parameters of LVOL Module

The following are some of the parameters for the LVOL module which allows you to customize the functionality of the module:

  • vg – It specifies the name of the volume group that the logical volume is a part of.
  • lv – It specifies the name of the logical volume.
  • size – It defines the size of the logical volume, by default, in megabytes or optionally with one of [bBsSkKmMgGtTpPeE].
  • resizefs – It resizes the underlying filesystem with the logical volume (true or false).
  • state – It controls if the LV should be present, absent, or resized.
  • thinpool ­– It specifies the thin pool volume name.

Examples:

Let us explore some basic examples on how we can use this module to perform various tasks with LVMs.

Example 1: Create a New Logical Volume

The most basic task is setting up a new logical volume. For that, we can use the playbook as shown in the following:

---
- name: Create LV
  hosts: localhost
  tasks:
    - name: Create a logical volume of 1G
      lvol:
        vg: vg_1
        lv: logical_v
        size: 1G
        state: present

This should create a new logical volume with the specified name and size in the defined volume group.

Example 2: Resize a Logical Volume

The second use case is resizing an existing logical volume. An example playbook is as follows:

---
- name: Resize LV
  hosts: localhost
  tasks:
    - name: Resize logical_v to 2G
      lvol:
        vg: vg_1
        lv: logical_v
        size: 2G
        state: resized
        resizefs: true

This should resize the logical volume from 1G to 2G. Keep in mind that we use the “resizefs: true” parameter to ensure that we resize the filesystem alongside the logical volume.

Example 3: Remove a Logical Volume

To remove an existing logical volume, we can use the state parameter as shown in the following playbook example:

---
- name: Remove LV
  hosts: localhost
  tasks:
    - name: Remove Volume
      lvol:
        vg: vg_1
        lv: logical_v
        state: absent

Example 4: Create a Thin-Provisioned Logical Volume

We can use the thinpool parameter to create the thin-provisioned LVs as provided in the following sample playbook:

---
- name: Create thin provisioned LV
  hosts: localhost
  tasks:
    - name: Create a thin pool named th_pool of size 5G
      lvol:
        vg: vg_1
        lv: th_pool
        size: 5G
        state: present
    - name: Create a thin LV named 'thinlv' using th_pool
      lvol:
        vg: vg_1
        lv: thinlv
        size: 2G
        thinpool: th_pool
        state: present

As you can see, the previous playbook starts by creating a thin pool and then creating a thin logical volume in the resulting thin pool.

Conclusion

We explored the workings of the LVOL module in Ansible to manage the logical volumes in remote hosts. Learning how to work with the LVOL module can quickly help you to solve a lot of complexities that are involved in managing the LVM devices. This is because it abstracts the complexities of direct LVM command-line management into simpler, human-readable YAML definitions.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list