This guide will help you understand how to automate Logical Volume Manager (LVM) Management on Linux using Ansible.
Many times, system admins face a problem when trying to extend and reduce a primary partition. This can create a lot of problems especially if you are in need of extra space on the disk. To overcome this problem, Logical Volume Manager(LVM) was introduced. This allows for flexible and dynamic management of storage devices.
The other cool benefits associated with LVM are:
- Storage Pooling: It allows the pooling of physical storage devices into a single logical volume group. This can help to aggregate the storage capacity of multiple disks or partitions, providing a unified storage pool that can be allocated and managed as needed.
- Volume Management: it makes it easy to create and manage logical volumes, which are virtual partitions that span one or more physical storage devices. The logical volumes can be resized, extended, or shrunk without affecting the underlying data or file systems
- Snapshotting: it also makes it possible to create snapshots that are point-in-time copies of logical volumes. This allows for efficient and space-saving backups, as they only store the changes made since the snapshot was created.
- Striping and Mirroring: for improved performance and data redundancy, LVM supports data striping and mirroring. Striping distributes data across multiple physical devices, which allows parallel access thus increasing read/write speeds. Mirroring duplicates data across multiple devices to increase fault tolerance and offer protection against data loss.
- Online Data Relocation: It also offers online relocation of data within logical volumes. This is to say that data can be moved between physical devices or different areas of the storage system without disrupting the system operation or requiring downtime.
When dealing with LVM, there are some things you need to know.
- PV(Physical Volume): This is a physical storage device such as a hard disk or solid-state dive. It is initialized on disk, partition, or loopback files to be used by LVM. When you initialize a PV, it creates a label at the start of the device.
- VG (Volume Group): This is a collection of one or more PVs that are put together to form a pool. A VG is created inside a PV.
- LV (Logical Volume): is a virtual partition created in the Volume Group (VG). There can be multiple LVs created in a single VG and can be resized accordingly.
To automate the partitioning, you can use Ansible which provides several modules to work with disk management in Linux. This comes in handy when you have several systems that you need to create and manage LVM partitions.
1. Install Ansible on Your System
To make automated Logical Volume Manager (LVM) management, you need to have Ansible installed on your workstation. This can be done using the guide below:
You can also use PIP to install Ansible. First, ensure that Python and Python-PIP are installed:
Once installed, proceed with Ansible installation as shown:
sudo pip3 install ansible
Or on RHEL_based systems with EPEL repos:
##On RHEL 7/CentOS 7
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
##On RHEL 8/CentOS 8/Alma Linux 8/Rocky Linux 8
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Then install Ansible:
sudo yum install ansible ansible-core
Top verify if you have Ansible installed, use:
$ ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/thor/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
Create the Ansible Hosts Inventory file
After installing Ansible, you need to create the host inventory. This comprises the nodes to be managed by Ansible. Modify the file as desired specifying the host IP address and user.
$ sudo vim /etc/ansible/hosts
[Rocky9]
192.168.200.59 ansible_ssh_user=username
[Debian12]
192.168.200.56 ansible_ssh_user=username
In the above file, replace the IP address and username appropriately. You also need to ensure that the users provided have sudo access and are able to execute sudo without a password. This can be done on the managed nodes as shown:
$ sudo vim /etc/sudoers
.......
##Find/Add the line
username ALL=(ALL) NOPASSWD: ALL
Now we need to copy the SSH key of the workstation to the managed nodes. First, generate SSH keys on the Workstation
ssh-keygen -t rsa
Now copy the key to the nodes:
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
Verify that you are able to reach all the hosts from your workstation.
$ ansible -m ping all
192.168.200.56 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
192.168.200.59 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2. Install LVM Ansible Collection
To be able to manage the LVM partitions, we will install and use the community.general.lvol collection. This can be done by executing the command:
$ ansible-galaxy collection install community.general
Process install dependency map
Starting collection install process
Installing 'community.general:7.1.0' to '/home/thor/.ansible/collections/ansible_collections/community/general'
Once complete, the module will the available as community.general.lvol. This module can be used to create, remove and resize logical volumes. It takes several parameters such as:
- lv: this is the name of the logical volume
- opts: options that can be passed along with the
lvcreate
command. - pvs: a list separated by commas of the physical volumes for example
/dev/sdb, /dev/sdc
- resizefs(boolean): it is used to resize the underlying filesystem with the LV.
- shrink(boolean): it shrinks the current size if the size is higher than the requested.
- size: specifies the size of the LV
- snapshot: the name of the snapshot
- state: control if the LV exists.
- thinpool: the name of the thin pool volume
- vg: the name of the VG the logical volume is part of.
3. Create Logical Volume Manager (LVM) using Ansible
Once all the above activities have been done, you are set to automate Logical Volume Manager (LVM) Management on Linux using Ansible. In this guide, we have disks /dev/sdb
and /dev/sdc
available on our managed nodes. We can now proceed and learn together how to create, modify/expand/resize, and delete LVMs.
When creating LVM, we can have 3 steps below. These steps can be done individually or put into one playbook as shown in the below steps.
a. Creating a Physical Volume
We will start by creating a partition on the disks this can be done using the module community.general.parted
vim pv_creation-playbook.yaml
In the file, add the lines below:
- name: playbook for PV creation
hosts: all
become: true
tasks:
- name: Create a physical volume on disks /dev/sdb
community.general.parted:
device: /dev/sdb
number: 1
flags: [ lvm ]
state: present
part_end: 100%
- name: Create a physical volume on disks /dev/sdc
community.general.parted:
device: /dev/sdc
number: 1
flags: [ lvm ]
state: present
part_end: 100%
Apply the manifest:
ansible-playbook pv_creation-playbook.yaml
Sample Output:

Now we have partitions /dev/sdb1 and /dev/sdc1 created on the two devices.
b. Creating Volume Group(VG)
Once a partition has been created, we will use the community.general.lvg module to create a PV and VG. When creating a volume group, you need to specify the Physical Extents (PE) size(default value is 4MB). For example, we can use the below playbook:
vim vg_creation-playbook.yaml
Add the lines below replacing where required:
- name: playbook for VG creation
hosts: all
become: true
tasks:
- name: Install lvm2 dependency
package:
name: lvm2
state: present
- name: Create a volume group on disks /dev/sdb and /dev/sdc
community.general.lvg:
vg: sample-vg
pvs: /dev/sdb1,/dev/sdc1
pesize: 16
The above file ensures that the required LVM2 module has been installed, and then creates a volume group. To apply the playbook, use:
ansible-playbook vg_creation-playbook.yaml
Sample Output:

c. Creating Logical Volume(LV)
To create and manage logical volumes, we use the community.general.lvol collection.The LV can be created on the above VG using the below playbook:
vim lv_creation-playbook.yaml
In the file add:
- name: playbook for LV creation
hosts: all
become: true
tasks:
- name: Create a logical volume of 512m with disks /dev/sdb and /dev/sdc
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 512
pvs: /dev/sdb1,/dev/sdc1
Apply the changes:
ansible-playbook lv_creation-playbook.yaml
That will create a logical volume with the name sample-lv in the VG.

Verify that changes:
$ sudo vgs sample-vg
VG #PV #LV #SN Attr VSize VFree
sample-vg 2 1 0 wz--n- <19.97g <19.47g
$ sudo lvs -a -o +devices sample-vg
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert Devices
sample-lv sample-vg -wi-a----- 512.00m /dev/sdb1(0)
There are several other ways to create a logical volume. These are:
- Create a logical volume of 10G
- name: playbook for 10g LV creation
hosts: all
become: true
tasks:
- name: Create a logical volume of 10G with disks /dev/sdb and /dev/sdc
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 10g
- Create a logical volume the size of all remaining space in the volume group
- name: playbook for 100% size LV creation
hosts: all
become: true
tasks:
- name: Create a logical volume of all the remaining space with disks /dev/sdb and /dev/sdc
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 100%FREE
- Create a logical volume with special options
- name: playbook for simple logical volume with special options
hosts: all
become: true
tasks:
- name: Create a logical volume with special options
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 10g
opts: -r 16
- Create cache pool logical volume
- name: playbook for simple cache pool logical volume
hosts: all
become: true
tasks:
- name: Create a cache pool logical volume
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 10g
opts: --type cache-pool
Remember all the above tasks for creating PV, VG and LV can be put in one playbook as shown:
$ vim lvm_creation_playbook.yml
- name: playbook for LVM creation
hosts: all
become: true
tasks:
- name: Create a physical volume on disks /dev/sdb
community.general.parted:
device: /dev/sdb
number: 1
flags: [ lvm ]
state: present
part_end: 100%
- name: Create a physical volume on disks /dev/sdc
community.general.parted:
device: /dev/sdc
number: 1
flags: [ lvm ]
state: present
part_end: 100%
- name: Install lvm2 dependency
package:
name: lvm2
state: present
- name: Create a volume group on disks /dev/sdb and /dev/sdc
community.general.lvg:
vg: sample-vg
pvs: /dev/sdb1,/dev/sdc1
pesize: 16
- name: Create a logical volume of 512m with disks /dev/sdb and /dev/sdc
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 512
pvs: /dev/sdb1,/dev/sdc1
Now apply the playbook:
ansible-playbook lvm_creation_playbook.yml
4. Automate LVM Management using Ansible
Now once the LVM has been created, we can also automate other tasks such as modifying/expanding/resizing, and deleting LVMs. This can be done using the community.general.lvol module as shown in the detailed examples below:
a. Extending Logical Volume
There are times you need to increase the size of your logical volume to fit your needs. This can be done as shown
vim lv_extend_playbook.yml
In the file, specify the desired variables. Here are some examples:
- Extend the logical volume to specific size (say 1g)
- name: playbook for LVM extension
hosts: all
become: true
tasks:
- name: Extend the logical volume to 1024m
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 1024
- Extend the logical volume by given space
- name: playbook for LVM extension
hosts: all
become: true
tasks:
- name: Extend the logical volume by given space
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: +512M
- Extend the logical volume to consume all remaining space in the volume group
- name: playbook for LVM extension
hosts: all
become: true
tasks:
- name: Extend the logical volume to consume all remaining space in the volume group
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: +100%FREE
- Extend the logical volume to take all remaining space of the PVs and resize the underlying filesystem
- name: playbook for LVM extension
hosts: all
become: true
tasks:
- name: Extend the logical volume to take all remaining space of the PVs and resize the underlying filesystem
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 100%PVS
resizefs: true
To apply the playbook use:
ansible-playbook lv_extend_playbook.yml
Sample Output:

b. Shrink Logical Volume
You can also shrink the logical volume to a desired size. First, create the playbook.
vim lv_shrink_playbook.yml
Choose one of the desired options below to shrink the LV:
- Resize the logical volume to % of VG
- name: playbook for LVM shrinking
hosts: all
become: true
tasks:
- name: Resize the logical volume to % of VG
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 80%VG
force: true
- Reduce the logical volume to a specific size(say 512m)
- name: playbook for LVM shrinking
hosts: all
become: true
tasks:
- name: Resize the logical volume to 512m
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 512
force: true
- Reduce the logical volume by given space
- name: playbook for LVM shrinking
hosts: all
become: true
tasks:
- name: Resize the logical volume by given space
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: -512m
force: true
- Set the logical volume to 512m and do not try to shrink if the size is lower than the current one
- name: playbook for LVM shrinking
hosts: all
become: true
tasks:
- name: Set the logical volume to 512m and do not try to shrink if size is lower than current one
community.general.lvol:
vg: sample-vg
lv: sample-lv
size: 512
shrink: false
Save the content and apply the manifest:
ansible-playbook lv_shrink_playbook.yml
c. Create a Snapshot of a Logical Volume
LVM allows users to take a point-in-time snapshot of a volume. This can also be done using Ansible:
$ vim lv_snapshot_playbook.yml
- name: playbook for LVM snapshot
hosts: all
become: true
tasks:
- name: Create a snapshot volume of the test logical volume.
community.general.lvol:
vg: sample-vg
lv: sample-lv
snapshot: snap1
size: 100m
Apply the playbook:
ansible-playbook lv_snapshot_playbook.yml
d. Deactivate a Logical Volume
It is also possible to deactivate a logical volume. To achieve that, use a playbook with the below content
- name: playbook for LVM deactivation
hosts: all
become: true
tasks:
- name: Deactivate a logical volume
community.general.lvol:
vg: sample-vg
lv: sample-lv
active: false
e. Delete a Logical Volume
To delete a logical volume, use a playbook with the below content. Replace values where required.
- name: playbook for LVM Deletion
hosts: all
become: true
tasks:
- name: Remove the logical volume
community.general.lvol:
vg: sample-vg
lv: sample-lv
state: absent
force: true
Once executed, you will have the LV deleted on the managed hosts.

Conclusion
That marks the end of this detailed guide on the automated Logical Volume Manager (LVM) Management on Linux using Ansible. I hope you benefitted from it.
See more:
- Automate Linux Systems with Ansible System Roles
- Automate Windows Server 2019 & Windows 10 Administration with Ansible
- How to automate simple tasks on Linux using Ansible
- Automate App Deployment updates on Kubernetes using Keel