Ansible is an automation, configuration, and infrastructure management tool. My previous articles introduce Ansible and explain system roles. In this article, I demonstrate configuring Logical Volume Manager (LVM) using Ansible. Configuring LVM with Ansible helps system administrators perform their tasks more efficiently and spend less time working manually. You can create an Ansible playbook that creates a partition and initializes LVM.
[ You might also enjoy: Introduction to Logical Volume Manager ]
LVM Overview
As you know, the primary partition can't be extended and reduced. Such a scenario can create problems when you need extra space on a disk. Using LVM, you can overcome this problem because LVM lets you add and remove a new disk to a logical volume. LVM is part of the default installation of Red Hat Enterprise Linux (RHEL) since RHEL 7.
Here are a few essential items you need to understand before using LVM:
- PV—Physical Volume initialized on disk, partition, or loopback file. When you initialize a PV, it creates a label at the start of the device.
- VG—Volume Group is a collection of logical volumes (LVs). You create a VG inside a PV.
- LV—Logical Volume is just like a standard partition on a disk. You can create multiple LVs inside a single VG. You can resize an LV according to the space required.
Creating a Partition
In RHEL 8, you create a partition using the Parted utility. Parted gives you the flexibility to create a partition using MS-DOS or a GUID Partition Table (GPT) partition. In Ansible, you can use the Parted module for partitioning.
In this example, consider /dev/vdb
as the disk name. Create two 1 GB partitions. Simply using the Parted module, you can create partitions of any size.
Example:
---
- name: playbook for simple 1 GB partition
hosts: vm1.example.com
become: true
tasks:
- name: create partition
parted:
device: /dev/vdb
number: 1
flags: [ lvm ]
state: present
part_end: 2GB
Run this playbook using:
$ ansible-playbook partition.yml
You can use the lsblk
command to check that the partition now exists.
Initializing LVM and creating LVs
After creating the partition, initialize the LV. To initialize requires you to first create both a PV and a VG. Use the Ansible lvg module to create a PV and a VG with a Physical Extents (PE) size. The PE divides the VG into a fixed size. By default, the size of the PE is 4MB. You can change the default size while creating the VG. Generally, a larger PE size provides better performance.
Example:
- name: task for creating volume group
lvg:
vg: sample-vg
pvs: /dev/vdb1
pesize: 16
- name: Install lvm2 dependency
package:
name: lvm2
state: present
The next task creates the LV. The lvol module creates LVs. When creating an LV, you give options like VG name, size, and an LV name.
In this example, a 2GB VG creates the first LV with a size of 1GB.
Example:
- name: task for creating logical volume
lvol:
vg: sample-vg
lv: sample-lv
size: 1g
force: yes
To use this created LV, you have to mount that partition within the filesystem. Only you can access this LV for storing data. In Ansible, use the filesystem modules for formatting any block device with filesystem
. After formatting, you mount the partition at any directory. You can use the mount module in Ansible. Make sure that the directory (/data1
in this example) exists before executing the playbook.
Example:
- name: Create directory data1 if does not exist
file:
path: /data1
state: directory
mode: '0755'
- name: format the xfs filesystem
filesystem:
fstype: xfs
dev: /dev/sample-vg/sample-lv
- name: mount the lv on /data1
mount:
path: /data1
src: /dev/sample-vg/sample-lv
fstype: xfs
state: mounted
Example: Whole playbook:
---
- name: playbook for simple 1 GB partition
hosts: localhost
become: true
tasks:
- name: create partition
parted:
device: /dev/nvme1n1
number: 1
flags: [ lvm ]
state: present
part_end: 2GB
- name: Install lvm2 dependency
package:
name: lvm2
state: present
- name: task for creating volume group
lvg:
vg: sample-vg
pvs: /dev/nvme1n1p1
pesize: 16
- name: task for creating logical volume
lvol:
vg: sample-vg
lv: sample-lv
size: 1g
force: yes
- name: Create directory data1 if does not exist
file:
path: /data1
state: directory
mode: '0755'
- name: format the xfs filesystem
filesystem:
fstype: xfs
dev: /dev/sample-vg/sample-lv
- name: mount the lv on /data1
mount:
path: /data1
src: /dev/sample-vg/sample-lv
fstype: xfs
state: mounted
Run this playbook and check whether your LV gets created or use the lvs
command.
Remember that one of the features of LVM is that you can extend the size of the LV. Manually, you can use the lvextend
command. In Ansible, you can use the lvol module to extend the size of sample-lv.
Example:
- name: Extend the logical volume to take all remaining space of the PVs and resize the underlying filesystem
lvol:
vg: sample-vg
lv: sample-lv
size: 2g
resizefs: true
force: yes
Using the lvextend
command or the lvol module helps you to extend your LV. In this lvol volume, use the resizefs
parameter to enlarge an unmounted file system located on the device.
In order to shrink the size of an LV, the syntax is as follows:
- name: Extend the logical volume to take all remaining space of the PVs and resize the underlying filesystem
lvol:
vg: sample-vg
lv: sample-lv
size: 700m
shrink: yes
force: yes
[ Need more on Ansible? Take a free technical overview course from Red Hat. Ansible Essentials: Simplicity in Automation Technical Overview. ]
Wrap Up
This article showed you how to configure LVM using a simple Ansible playbook. Rather than using a simple playbook, you could create a role, using the same process and modules as above. Ansible helps you reduce manual tasks as well as increase efficiency for LVM configuration. Using the methods described in this article simplifies operations on multiple managed nodes.
Über den Autor
Shiwani Biradar is an Associate Technical support Engineer in Red Hat. She loves contributing to open source projects and communities. Shiwani never stops exploring new technologies. If you don't find her exploring technologies then you will find her exploring food. She is familiar with Linux, Cloud, and DevOps tools and enjoys technical writing, watching TV series, and spending time with family.
Nach Thema durchsuchen
Automatisierung
Das Neueste zum Thema IT-Automatisierung für Technologien, Teams und Umgebungen
Künstliche Intelligenz
Erfahren Sie das Neueste von den Plattformen, die es Kunden ermöglichen, KI-Workloads beliebig auszuführen
Open Hybrid Cloud
Erfahren Sie, wie wir eine flexiblere Zukunft mit Hybrid Clouds schaffen.
Sicherheit
Erfahren Sie, wie wir Risiken in verschiedenen Umgebungen und Technologien reduzieren
Edge Computing
Erfahren Sie das Neueste von den Plattformen, die die Operations am Edge vereinfachen
Infrastruktur
Erfahren Sie das Neueste von der weltweit führenden Linux-Plattform für Unternehmen
Anwendungen
Entdecken Sie unsere Lösungen für komplexe Herausforderungen bei Anwendungen
Original Shows
Interessantes von den Experten, die die Technologien in Unternehmen mitgestalten
Produkte
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud-Services
- Alle Produkte anzeigen
Tools
- Training & Zertifizierung
- Eigenes Konto
- Kundensupport
- Für Entwickler
- Partner finden
- Red Hat Ecosystem Catalog
- Mehrwert von Red Hat berechnen
- Dokumentation
Testen, kaufen und verkaufen
Kommunizieren
Über Red Hat
Als weltweit größter Anbieter von Open-Source-Software-Lösungen für Unternehmen stellen wir Linux-, Cloud-, Container- und Kubernetes-Technologien bereit. Wir bieten robuste Lösungen, die es Unternehmen erleichtern, plattform- und umgebungsübergreifend zu arbeiten – vom Rechenzentrum bis zum Netzwerkrand.
Wählen Sie eine Sprache
Red Hat legal and privacy links
- Über Red Hat
- Jobs bei Red Hat
- Veranstaltungen
- Standorte
- Red Hat kontaktieren
- Red Hat Blog
- Diversität, Gleichberechtigung und Inklusion
- Cool Stuff Store
- Red Hat Summit