kayobe/ansible/roles/wipe-disks/library/blockdevice_info.py
Dawud b2f0ef64ce Fix wipe-disks role to work on util-linux >= 2.37
On newer versions of util-linux, the ``wipe_disks`` role was not
properly configured due to the fact that ``lsblk -J`` returns a slightly
different output than what was previously returned. Previously the
output would contain a key called ``mountpoint``, however this has been
changed to ``mountpoints``. This has now been fixed by looking at the
new key as well as the old key.

Closes-bug: #2051859
Change-Id: I2a59f4eb3ae27f37fda38e05924315887cb04c41
(cherry picked from commit 3942d29448d16b4ef8e9682b4d08fd03d1dbe2cd)
2024-02-15 21:15:05 +00:00

79 lines
1.6 KiB
Python

DOCUMENTATION = '''
---
module: blockdevice_info
short_description: Returns information about block devices
version_added: "N/A"
description:
- "Returns information about block devices"
author:
- Will Szumski
'''
EXAMPLES = '''
- name: Retrieve information about block devices
blockdevice_info:
become: true
register: result
'''
RETURN = '''
umounted:
description: A list of all umounted block devices.
type: list
returned: always
'''
import json
from ansible.module_utils.basic import AnsibleModule
def _has_mounts(device):
try:
if device["mountpoint"]:
return True
# If unmounted, the JSON output contains "mountpoints": [null] so we handle
# the KeyError here.
except KeyError:
if device["mountpoints"][0]:
return True
for child in device.get("children", []):
if _has_mounts(child):
return True
return False
def unmounted(module, lsblk):
result = []
for device in lsblk.get("blockdevices", []):
if not _has_mounts(device) and device["type"] == 'disk':
result.append(device["name"])
return result
def run_module():
# The module takes no argumnets.
module_args = dict()
result = dict(
changed=False,
unmounted=[]
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
_rc, stdout, _stderr = module.run_command("lsblk -J")
lsblk = json.loads(stdout)
result['unmounted'] = unmounted(module, lsblk)
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()