Ansible playbook how to view and expand multiple existing lvms in multiple lvgs in rhel 8

Hi, i’m looking to add a step that checks and prints the current sizes of existing lv’s in a volume group before expanding them. Currently, I have a play that will extend the size of multiple lvm’s assuming the vg has enough space. I’m not sure whats the best way to determine the current sizes of the existing lvms. My first thought is to maybe create a role that uses the shell command that then runs vgs and lvs to capture and print current size and free space of lv’s and vg but not sure if thats the best way. Here is my current code:


  • name: Extend the logical volume to take all remaining space of the PVs and resize the underlying filesystem
    vars:
    lvextendAudit: False
    lvextendAuditSize: 4096
    #lvextendAuditSize: 4096
    ##------------------------
    lvextendOpt: True
    lvextendOptSize: 40g
    #lvextendOptSize: 5120
    block:
    • name: Extend the Audit Logical Volume
      lvol:
      vg: rhel
      lv: audit
      size: ‘{{ lvextendAuditSize }}’
      resizefs: true
      shrink: no
      when: lvextendAudit
    • name: Extend the OPT Logical Volume
      lvol:
      vg: rhel
      lv: opt
      size: ‘{{ lvextendOptSize }}’
      resizefs: true
      shrink: no
      when: lvextendOpt

If you look at community/general/plugins/modules/lvol.py (and lvg.py and lvg_rename.py in that same directory) you can see that those modules are python wrappers around the respective commands. You could go so far as to model a vgs and lvs module plugin after them, or you could (probably more easily if not as robustly) do the same thing with ansible.builtin.shell.

Since you’re writing this for your own use, you can probably be less strict in specifying inputs. Even if you do decide to use ansible.builtin.shell, it’s worth looking over those other plugin source files to see what sort of issues they anticipate and deal with.


By the way, if you’re posting YAML, put it between “tripple-tick” lines so it displays properly, like this:

```yaml
---
- name: My pretty YAML
  hosts: localhost
  […]
```

It’ll make your posts easier for everyone to read, and greatly increase your chances of getting feedback.

Cheers!