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
      community.general.lvg:
        vg: vg.storage
        pvs: /dev/sdb1
        pesize: 32
    
  2. Create a logical volume in your logical volume group (docs)

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

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

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

    - name: mount the logical volume to /data
      ansible.posix.mount:
        path: /data
        src: /dev/vg.storage/data
        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.