AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub 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) — Video Tutorial
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.
What You'll Learn
- How to Create a New LVM Partition with Ansible?
- Ansible Create a New LVM Partition
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
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 ************************************
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: troubleshooting
Read the full written article: Ansible parted Module: Create LVM Partitions on Linux (Guide)