Ansible

Ansible Parted

Ansible is a powerful, free, open-source automation tool that automates various DevOps tasks such as software provisioning, configuration management, and application deployment.

Ansible works through the use of modules, a set of predefined codes that perform specific operations on the target hosts.

One functional module available in the Ansible ecosystem is the parted module which plays a crucial role in disk partitioning in Linux systems.

In this tutorial, we will learn about the parted module to perform various disk partitioning operations on the target hosts.

Ansible Parted Module

The parted module is part of the “ansible.community.general” collection. It allows us to manage the partitions on different storage devices using the parted command-line utility in the background.

Requirements:

This module requires the following tools and utilities installed:

  1. Parted version 1.8.3 and above
  2. Option align requires parted 2.1 or above
  3. If the version of parted is below 3.1, it requires a Linux version that runs the sysfs file system /sys/
  4. The resizepart command when using the resize parameter

Installing the Module

To ensure that you have the parted module installed, use the following command:

ansible-galaxy collection install community.general

Parted Module Parameters

The following are some standard and useful parameters provided by the parted module:

  • Device – It defines the target device to manage.
  • Number – It specifies the number of the partition to manipulate.
  • Label – It sets the label type like gpt, msdos, etc.
  • part_type – It defines the partition type: primary, extended, or logical.
  • part_start – It sets the start of the partition.
  • part_end – It is used to determine the end of the partition.
  • State – It configures whether the partition should be present or absent.
  • Name – It sets the name for the partition.
  • Flags – It configures the flags to set on the partition.
  • Align – It sets the alignment for the partition.

Examples:

The following are some example playbooks that we can use to perform the partition operations using the parted module.

Example 1: Create New Partition

The following playbook creates a primary partition on /dev/sdb from 0% to 100% using the parted module as follows:

---
- name: Create a new primary partition on /dev/sdb
  hosts: all
  tasks:
    - name: Create partition
      parted:
        device: /dev/sdb
        number: 1
        state: present
        part_start: 0%
        part_end: 100%

Example 2: Remove a Partition

To remove a partition, we can set the state to absent as demonstrated in the following playbook:

---
- name: Remove the first partition from /dev/sdb
  hosts: all
  tasks:
    - name: Remove partition
      parted:
        device: /dev/sdb
        number: 1
        state: absent

Example 3: Create a Named Partition with a Specific Size

We can also define a playbook that uses the parted module to create a primary partition named data_partition that is 50GB in size on /dev/sdb.

---
- name: Create a named partition on /dev/sdb
  hosts: all
  tasks:
    - name: Create named partition
      parted:
        device: /dev/sdb
        number: 1
        state: present
        part_start: 0GB
        part_end: 50GB
        name: data_partition

Example 4: Set a Flag on a Partition

We can also set specific flags to a partition as demonstrated in the following playbook example:

---
- name: Set boot flag on the first partition of /dev/sdb
  hosts: all
  tasks:
    - name: Set boot flag
      parted:
        device: /dev/sdb
        number: 1
        state: present
        flags: [boot]

This playbook should set the boot flag on partition number 1 of /dev/sdb.

Conclusion

We learned about the parted module in Ansible which provides the easy to configure and use features to manage the partitions using the parted utility.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list