Create a New LVM Partition - Ansible module parted
How to automate the creation of a new partition /dev/sdb1 on an additional disk initialized for LVM on Linux using Ansible and parted module.


How to Create a New LVM Partition with Ansible?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot
Ansible Create a New LVM Partition
community.general.parted
- Configure block device partitions
Today we’re talking about the Ansible module parted.
The full name is community.general.parted
, which means that is part of the collection of modules “community.general” maintained by the Ansible Community.
The purpose of the module is to Configure block device partitions.
Parameters
device
string - The block device (disk) where to operatelabel
string - msdos/gpt/aix/amiga/bsd/dvh/loop/mac/pc98/sunnumber
integer - 1state
string - info/present/absent - partition information / create / deletefs_type
string - If specified and the partition does not exist, will set filesystem type to the given partition.flags
list - A list of the flags that have to be set on the partition.
The parameters of module parted
for the creation of a New LVM Partition use case.
The only required parameter is device
, the block device (disk) where to operate.
The system default partition table is msdos
but you could specify a different one, such as gpt
.
The parameter number
allows you to specify the partition number to work, required for almost any operations
The parameter state
specify the status of the specified partition. The default option info
only gives you the partition information, the option present
means to create the partition, and the option absent
means that the partition must be deleted.
You could specify the file system type via the fs_type
or flags
for the partition. For the LVM use case, we need to specify the lvm
flag.
Please note that fs_type
or flags
doesn’t initialize the file system or the partition, only sets this attribute in the partition table.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
How to create a New LVM Partition with Ansible Playbook. I’m going to automate the initialization of the additional disk /dev/sdb creating a new partition /dev/sdb1 ready to be used by LVM on Linux.
code
---
- name: disk demo
hosts: all
become: true
tasks:
- name: create partition
community.general.parted:
device: /dev/sdb
number: 1
flags: [ lvm ]
state: present
execution
$ ansible-playbook -i virtualmachines/disk/inventory disk_management/partition_create.yml
PLAY [disk demo] **********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [disk.example.com]
TASK [create partition] ***************************************************************************
changed: [disk.example.com]
PLAY RECAP ****************************************************************************************
disk.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
idempotency
$ ansible-playbook -i virtualmachines/disk/inventory disk_management/partition_create.yml
PLAY [disk demo] **********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [disk.example.com]
TASK [create partition] ***************************************************************************
ok: [disk.example.com]
PLAY RECAP ****************************************************************************************
disk.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
before execution
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 128G 0 disk
|-sda1 8:1 0 1G 0 part /boot
`-sda2 8:2 0 127G 0 part
|-rhel_rhel8-root 253:0 0 70G 0 lvm /
`-rhel_rhel8-swap 253:1 0 2.1G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
[[email protected] devops]# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Error: /dev/sdb: unrecognised disk label
Model: VBOX HARDDISK (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted)
after execution
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 128G 0 disk
|-sda1 8:1 0 1G 0 part /boot
`-sda2 8:2 0 127G 0 part
|-rhel_rhel8-root 253:0 0 70G 0 lvm /
`-rhel_rhel8-swap 253:1 0 2.1G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
`-sdb1 8:17 0 1023M 0 part
# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VBOX HARDDISK (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1074MB 1073MB primary lvm
(parted)
Recap
Now you know how to Create a New LVM Partition with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate