Ansible is often overlooked for managing the more physical aspects of the devices it is used to configure, but it has some powerful tools for handling lower level duties, such as disks. Recently I was working on some old-fashioned VMWare virtual machines and discovered that a lot of the guidance for managing logical volumes in Ansible is dated so here are my modernized notes on taking a disk from device to volume. This is NOT a primer on LVM itself, an understanding of LVM is recommended before trying to implement these steps.

  1. Create a new logical volume group (docs)

    - name: Create a volume group on top of /dev/sdb with physical extent size = 32MB
        pvs: /dev/sdb1
        pesize: 32
  2. Create a logical volume in your logical volume group (docs)

    - name: create logical volume
        lv: data
        size: 10g
  3. Create a filesystem on your new volume (docs)

    - name: Create a ext4 filesystem on the data volume
        fstype: ext4
        dev: /dev/
  4. Create a folder to mount our new volume to (docs)

    - name: Create directory /data if does not exist
        path: /data
        state: directory
        mode: '0755'
  5. Mount our new volume to /data (docs)

    - name: mount the logical volume to /data
        path: /data
        src: /dev/
        fstype: ext4
        state: mounted

These instructions were tested on Ubuntu 20.04 servers with Ansible 2.11 but should work on any Linux flavor that supports LVM.