Bootstrap FreeKB - Ansible - Manage LVM logical volumes using the lvol module
Ansible - Manage LVM logical volumes using the lvol module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

AVOID TROUBLE

Creating and removing logical volumes can only be done by root or by users or groups that have been granted sudo access, thus you'll want to either run this playbook as root, use the remote_user: root directive or use become.

The lvol module can be used to create and remove LVM logical volumes. In this example, a 1024 MB logical volume named "lv001" will be created from the available space in volume group vg001.

  • kibibyte (k or KiB) - 1024 bytes
  • kilobyte (K or KB) - 1000 bytes
  • mebibyte (m or MiB) - 1024 kibibytes
  • megabyte (M or MB) - 1000 kilobytes
  • gibibyte (g or GiB) - 1024 mebibytes
  • gigabyte (G or GB) - 1000 megabytes
  • tebibyte (t or TiB) - 1024 gibibytes
  • terabyte (T or TB) - 1000 gigabytes
  • pibibyte (p or PiB) - 1024 tebibytes
  • petabyte (P or PB) - 1000 gigabytes
  • exbibyte (e or EiB) - 1024 pibibytes
  • exabyte (E or EX) - 1000 petabytes

The ansible_lvm fact is ued to ensure volume group vg001 exists. If the volume group does not exist, the lvg module can be used to create the volume group.

---
- hosts: all
  tasks:
    - name: create a 1024 MiB logical volume named 'lv001' from volume group vg001
      lvol:
        vg: vg001
        lv: lv001
        size: '1024m'
      when: ansible_lvm.vgs.vg001 is defined
...

 

Or, the size option can accept one of the following formats. Refer to what are LVM extents to understand what LVM extents are.

  • Percentage of available extents in the volume group (e.g. 100%FREE)
  • Percentage of total extents in the volume group (e.g. 50%VG)
  • Percentage of the available extents in the physical volume (e.g. 25%PVS)
  • Percentage of the total extents in the origin logical volume (e.g. 100%ORIGIN)
---
- hosts: all
  tasks:
    - name: create logical volume named 'lv001' using 50% of the available extents from volume group vg001
      lvol:
        vg: vg001
        lv: lv001
        size: 50%FREE
...

 

Be aware that if the volume group does not exist, something like this wil be returned.

fatal: [server1.example.com]: FAILED! => 
{
 "changed": false,
 "err": "  Volume group \"vg001\" not found\n  Cannot process volume group bogus\n",
 "msg": "Volume group vg001 does not exist.",
 "rc": 5
}

 

There are a few ways to handle the situation where the volume group does not exist. Probably the most reasonable option is to use the lvg module to create the volume group.

---
- hosts: all
  tasks:
    - name: create LVM volume group vg001
      lvg:
        vg: vg001
        pvs: /dev/sda1
        pesize: 32
...

 

Or the ignore_errors parameter can be used, but this probably isn't a great option, since the ultimate object here is to create the logical volume, and this doesn't really help in getting the logical volume created.

---
- hosts: all
  tasks:
    - name: create a 1024 MB logical volume named 'lv001' from volume group vg001
      lvol:
        vg: vg001
        lv: lv001
        size: '1024'
      ignore_errors: true
...

 

If the creation of the logical volume is successful, something like this should be returned.

TASK [create a 1024 MB logical volume named 'lv001' from volume group vg001]
ok: [server1.example.com] => {
    "out": {
        "changed": true,
        "failed": false,
        "msg": ""
    }
}

 

And here is how to remove the logical volume.

---
- hosts: all
  tasks:
    - name: remove the logical volume named 'lv001' from volume group vg001
      lvol:
        vg: vg001
        lv: lv001
        state: absent
        force: true
...

 

If the removal is successful, something like this should be returned.

TASK [remove the logical volume named 'lv001' from volume group vg001]
ok: [server1.example.com] => {
    "out": {
        "changed": true,
        "failed": false
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter cad4de in the box below so that we can be sure you are a human.