Ansible - Extend an LVM logical volumes using the lvol module
by
Jeremy Canfield |
Updated: January 16 2024
| 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 extend a Logical Volume, like this.
---
- hosts: all
tasks:
- name: extend Logical Volume lv001 in Volume Group vg001 to 8 GB
lvol:
vg: vg001
lv: lv001
size: 8g
...
However, you probably want to first determine if the Volume Group has enough available extents, which means you'll want to
- Determine the current size of the Logical Volume
- Determine the available extents in the Volume Group
Something like this.
---
- hosts: all
tasks:
- name: use lvdisplay to get the size of the /dev/mapper/rootvg-lv_var Logical Volume
shell: lvdisplay /dev/mapper/rootvg-lv_var | grep "LV Size" | awk '{print $3}'
register: lvsize
- debug:
var: lvsize
# ---------------------------------------------------------------
# This should return a string, almost always 'resizeable'
# ---------------------------------------------------------------
- name: use vgdisplay to get the available free extents in the rootvg volume group
become: yes
become_user: root
shell: vgdisplay rootvg | grep 'VG Status' | awk '{print $3}'
register: rootvg_status
- debug:
var: rootvg_status.stdout
# ---------------------------------------------------------------
# This should return something like 128 or 256 or 512, et cetera
# ---------------------------------------------------------------
- name: use vgdisplay to get the available free extents in the rootvg volume group
shell: vgdisplay rootvg | grep 'Free PE / Size' | awk '{print $5}'
register: rootvg_free_extents
- debug:
var: rootvg_free_extents
# ---------------------------------------------------------------------------
# If rootvg_free_extents is 128, then rootvg_free_extents_float will be 1.0
# If rootvg_free_extents is 256, then rootvg_free_extents_float will be 2.0
# et cetera
# ---------------------------------------------------------------------------
- name: set_fact rootvg_available_extents_float
set_fact:
rootvg_available_extents_float: "{{ rootvg_free_extents.stdout|int / 128 }}"
- debug:
var: rootvg_available_extents_float
# --------------------------------------------------------------------------------------
# If lvsize.stdout is 7.00, then needed_extents will be 1.00 because 8.00 - 7.00 = 1.00
# If lvsize.stdout is 6.00, then needed_extents will be 2.00 because 8.00 - 6.00 = 2.00
# If lvsize.stdout is 5.00, then needed_extents will be 3.00 because 8.00 - 5.00 = 3.00
# et cetera
# --------------------------------------------------------------------------------------
- name: set_fact needed_extents
set_fact:
needed_extents: "{{ 8.00 - lvsize.stdout|int }}"
- name: fail if the rootvg Volume Group does not have enough available extents and is not resizeable
fail:
msg: The rootvg Volume Group has {{ rootvg_available_extents_float|int }} available extents thus the /dev/mapper/rootvg-lv_var Logical Volume cannot be extended to 8.00
when: rootvg_available_extents_float|int < needed_extents|int and rootvg_status.stdout != 'resizeable'
- name: extend Logical Volume lv001 in Volume Group rootvg to 8 GB
lvol:
vg: rootvg
lv: lv001
size: 8g
...
Did you find this article helpful?
If so, consider buying me a coffee over at