Ansible Pilot

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.

June 1, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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

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

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.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

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 
[root@demo 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)

code with ❤️ in GitHub

Recap

Now you know how to Create a New LVM Partition with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) 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

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases