Find the LVM and VG name from Ansible inputting the Mount point name

Viewed 795

I'm trying to find if we have any options in setup or any combination of ansible modules to find the LVM / VG name if we give the mount point name as input to the playbook, please suggest if you have any options, as of now I can only see the only option in setup is to fetch the device name "device": "/dev/mapper/rhel-root", using the ansible_mounts.device. But to split the LV and VG name from the "/dev/mapper/rhel-root" is another challenge. Kindly suggest if any options.

2 Answers

This is actually possible since 2015 through the ansible_lvm facts returned by setup and part of the hardware subset.

To get a result, you need to run setup as root and the lvm utilities must be installed on the target.

You can make a quick test on your local machine (if relevant, adapt to whatever target where you have privilege escalation rights):

ansible localhost -b -m setup \
  -a 'gather_subset=!all,!min,hardware' -a 'filter=ansible_lvm'

Here is an example output from the first test vm I could connect to:

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_lvm": {
            "lvs": {
                "docker_data": {
                    "size_g": "80.00",
                    "vg": "docker"
                },
                "root": {
                    "size_g": "16.45",
                    "vg": "system"
                },
                "swap": {
                    "size_g": "3.00",
                    "vg": "system"
                }
            },
            "pvs": {
                "/dev/sda2": {
                    "free_g": "0.05",
                    "size_g": "19.50",
                    "vg": "system"
                },
                "/dev/sdb": {
                    "free_g": "0",
                    "size_g": "80.00",
                    "vg": "docker"
                }
            },
            "vgs": {
                "docker": {
                    "free_g": "0",
                    "num_lvs": "1",
                    "num_pvs": "1",
                    "size_g": "80.00"
                },
                "system": {
                    "free_g": "0.05",
                    "num_lvs": "2",
                    "num_pvs": "1",
                    "size_g": "19.50"
                }
            }
        }
    },
    "changed": false
}

This would be a very simple .yaml file, doing the same as what @Zeitounator suggested:

---
- name: detect lvm setup
    setup:
        filter: "ansible_lvm"
        gather_subset: "!all,!min,hardware"
      register: lvm_facts
    - debug: var=lvm_facts

Related