How to Create a New LVM Partition with Ansible?

I'm going to show you a live Playbook 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 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.

Links

  • [community.general.parted](https://docs.ansible.com/ansible/latest/collections/community/general/parted_module.html)

## 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

``yaml

---

  • name: disk Playbook

hosts: all

become: true

tasks:

- name: create partition

community.general.parted:

device: /dev/sdb

number: 1

flags: [ lvm ]

state: present

`

execution

``bash

$ 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