AnsiblePilot — Ansible Automation Platform and Enterprise Ansible

AnsiblePilot covers Red Hat Ansible Automation Platform (AAP) and enterprise Ansible: install and upgrade paths, Event-Driven Ansible, security and day-2 operations, plus Luca Berton's books, videos and training.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 9 technical books published by Apress and Leanpub, 4 of them on Ansible, including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible parted Module: Create LVM Partitions on Linux (Guide)

By Luca Berton · Published 2022-06-01 · Category: troubleshooting

Discover how to automate the creation of LVM partitions on Linux systems using Ansible. This guide walks you through using the community.general.parted module.

Ansible parted Module: Create LVM Partitions on Linux (Guide)

How to Create a New LVM Partition with Ansible?

See also: Ansible Change User Password: Secure Password Management Guide

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 operate
  • label string - msdos/gpt/aix/amiga/bsd/dvh/loop/mac/pc98/sun
  • number integer - 1
  • state string - info/present/absent - partition information / create / delete
  • fs_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.

See also: Edit single-line text - Ansible Playbook for Changing IP Address of Remote Hosts

## Playbook 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 Playbook
  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 Playbook] **********************************************************************************
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 Playbook] **********************************************************************************
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

Conclusion

Now you know how to Create a New LVM Partition with Ansible.

See also: Ansible Linux Users and Groups: Complete Management Guide (Examples)

Category: troubleshooting

Watch the video: Ansible parted Module: Create LVM Partitions on Linux (Guide) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home