In our last guide, we have seen how to manage user accounts using ansible. By following in this guide let us see how to create a new filesystem using ansible.

To read about more topics on “Ansible” you can refer to the below links.

  1. Install and configure Ansible Automation IT Tool
  2. Install Ansible using Python installation manager pip
  3. How to create a host’s Inventory using Ansible

Strictly advice to use this playbook in a new build server.

What I’m trying to perform here is to create a new filesystem using /dev/sdb. Server Operating System installed in “/dev/sda” by following we need to create a mount point for “/datas” in this case we have 100 GB of disk “/dev/sdb” and it needs to be created using logical volume. Create the filesystem and mount under /datas.

What does this playbook do?

  • Defined in which host this playbook need to be run.
  • Defined with variables prompt which will take user input.
  • Defined with tasks to create file systems, mount, fstab entry.
  • Partitions using fdisk
  • Create Volume group with each 8 MB in PE size.
  • Create a logical volume from the volume group by using 100% of the space in VG.
  • Create a filesystem in the newly created logical volume. (ext3, ext4, xfs)
  • Mount the newly created filesystem with required mount options.

You may ask how I’m using the fdisk to create a partition. It simple by using an echo.

# /bin/echo -e "n\np\n1\n\n\nt\n8e\nw" | sudo fdisk "{{ disk_name }}"

new line represent here by Pressing an “Enter Key”.

n\ = new partition, new line
np\ = primary, new line
n1\ = 1 st primary, 1 and new line
n\ = new line
n\ = new line
nt\ = t and new line
n8e\ = LVM type 8e and new line
nw” = write and exit, new line

|” = forwarding all inputs to fdisk command on a disk.

This is how we are using fdisk to create a partition using the echo command.

Warning: Never use in an existing production environment. If you know what you doing kindly point to right disk to avoid data loss.

Here the playbook starts.

- hosts: "{{ hosts_prompt }}" ## This will get from "hosts_promt" as an input of any Hostname or Host group
  gather_facts: no
  connection: ssh

   - name: "hosts_prompt" ## Host where playbook about to play
     prompt: "Enter the Host or Host group need to run with this Playbook"
     private: no

   - name: "disk_name" ## Which disk we are using to create a partition for a logical volume. eg(/dev/sdb,/dev/sdc)
     prompt: "Disk used for creating partition"
     private: no

   - name: "pvs_name" ## Disk after partition
     prompt: "Disk name after creating with partition"
     private: no

   - name: "vg_name" ## Name of Volume group about to create.
     prompt: "Enter VG name to be created"
     private: no

   - name: "lv_name" ## Name of Logical Volume about to create.
     prompt: "Enter LV name to be created"
     private: no

   - name: "file_system" ## What type of filesystem it can be ext3, ext4 or xfs etc.
     prompt: "Type of file system required?"
     private: no

   - name: "mount_point" ## In what name mount point should be created.
     prompt: "Where need to mount?"
     private: no
   - name: Create Partition for "{{ mount_point }}" file system
     remote_user: ansible
     become: yes
     become_method: sudo
       /bin/echo -e "n\np\n1\n\n\nt\n8e\nw" | sudo fdisk "{{ disk_name }}" ## Create the partition on a disk.

   - name: Create VG for vg_u01
     remote_user: ansible
     become: yes
     become_method: sudo
       vg: "{{ vg_name }}" ## Name of Volume group.
       pvs: "{{ pvs_name }}" ## Physical Volume created using partition eg (/dev/sdb1 or /dev/sdc1)
       pesize: 8

   - name: Create LVM lv_u01 for "{{ mount_point }}" file system.
     remote_user: ansible
     become: yes
     become_method: sudo
       vg: "{{ vg_name }}"
       lv: "{{ lv_name }}" ## Name of Logical volume need to be created
       size: 100%FREE ## All the PE's in Volume group will be used for creating the logical volume.

   - name: Create the file system on newly created Logical volume for "{{ mount_point }}".
     remote_user: ansible
     become: yes
     become_method: sudo
       fstype: "{{ file_system }}" ## What type of filesystem required eg:(ext3, ext4, xfs etc.)
       dev: "/dev/mapper/{{ vg_name }}-{{ lv_name }}" ## Full related path of device mapper to be created with creating FS.

   - name: Mount the created filesystem ## This is to create the FSTAB entry and mount the Filesystem.
     remote_user: ansible
     become: yes
     become_method: sudo
       path: "{{ mount_point }}" ## Mount point where to mount the FS.
       src: "/dev/mapper/{{ vg_name }}-{{ lv_name }}" ## Full related path of device mapper to be mounted under mount point.
       fstype: "{{ file_system }}" ## Filesystem type
       opts: rw,noatime,noexec,nodev,nosuid ## Mount options
       state: mounted

That’s it we have successfully created with a new Filesystem on a remote server. This will help in case we are deploying with the number of new servers. Make sure to not use in an existing production environment.

Babin Lonston
Overall 14+ Years of experience in IT field, 7+ years of experience in Linux administration with Virtualization & Cloud technologies. Love documentation and being Numismatics for a long time.

The solution for “touch: cannot touch `test.txt’: No space left on device”

Previous article

Linux Interview Q&A Level 1

Next article

You may also like


Leave a reply

Your email address will not be published.

More in Automation