If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The parted module can be used to create a remove a partition on a storage device. Let's say the /dev/sdb storage device has not yet been configured with a parition. The following playbook would create the /dev/sdb1 partition on the storage device, using all of the available storage on the disk for the new partition.
AVOID TROUBLE
The modification of a partition can only be done by root or a user or group that has been granted sudo access. Refer to beome / sudo to understand how to run a playbook as root or with become (sudo) permission.
---
- hosts: all
tasks:
- name: create the /dev/sdb1 partition
parted:
device: /dev/sdb
number: 1
part_start: "0%"
part_end: "100%"
state: present
...
Instead of using part_start: 0% and part_end: 100%, which uses all of the available space on the disk for the partition, you could specify the amount of space you want allocated for the partition, such as 1 GB.
---
- hosts: all
tasks:
- name: create a 1 GB /dev/sdb1 partition
parted:
device: /dev/sdb
number: 1
part_start: 1GiB
state: present
...
The flags option can be used to set a partition type, such as setting a partition type to swap or LVM.
---
- hosts: all
tasks:
- name: create a 1 GB /dev/sdb2 swap partition
parted:
device: /dev/sdb
number: 2
part_start: 1GiB
flags: [ swap ]
state: present
...
Did you find this article helpful?
If so, consider buying me a coffee over at