1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-30 15:38:53 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -76,13 +76,13 @@ from ansible.module_utils.basic import AnsibleModule
def get_pv_status(module, device):
"""Check if the device is already a PV."""
cmd = ['pvs', '--noheadings', '--readonly', device]
cmd = ["pvs", "--noheadings", "--readonly", device]
return module.run_command(cmd)[0] == 0
def get_pv_size(module, device):
"""Get current PV size in bytes."""
cmd = ['pvs', '--noheadings', '--nosuffix', '--units', 'b', '-o', 'pv_size', device]
cmd = ["pvs", "--noheadings", "--nosuffix", "--units", "b", "-o", "pv_size", device]
rc, out, err = module.run_command(cmd, check_rc=True)
return int(out.strip())
@ -96,17 +96,18 @@ def rescan_device(module, device):
parent_device = base_device
if os.path.exists(is_partition):
parent_device = (
base_device.rpartition('p')[0] if base_device.startswith('nvme')
else base_device.rstrip('0123456789')
base_device.rpartition("p")[0] if base_device.startswith("nvme") else base_device.rstrip("0123456789")
)
# Determine rescan path
rescan_path = f"/sys/block/{parent_device}/device/{'rescan_controller' if base_device.startswith('nvme') else 'rescan'}"
rescan_path = (
f"/sys/block/{parent_device}/device/{'rescan_controller' if base_device.startswith('nvme') else 'rescan'}"
)
if os.path.exists(rescan_path):
try:
with open(rescan_path, 'w') as f:
f.write('1')
with open(rescan_path, "w") as f:
f.write("1")
return True
except IOError as e:
module.warn(f"Failed to rescan device {device}: {e!s}")
@ -118,41 +119,41 @@ def rescan_device(module, device):
def main():
module = AnsibleModule(
argument_spec=dict(
device=dict(type='path', required=True),
state=dict(type='str', default='present', choices=['present', 'absent']),
force=dict(type='bool', default=False),
resize=dict(type='bool', default=False),
device=dict(type="path", required=True),
state=dict(type="str", default="present", choices=["present", "absent"]),
force=dict(type="bool", default=False),
resize=dict(type="bool", default=False),
),
supports_check_mode=True,
)
device = module.params['device']
state = module.params['state']
force = module.params['force']
resize = module.params['resize']
device = module.params["device"]
state = module.params["state"]
force = module.params["force"]
resize = module.params["resize"]
changed = False
actions = []
# Validate device existence for present state
if state == 'present' and not os.path.exists(device):
if state == "present" and not os.path.exists(device):
module.fail_json(msg=f"Device {device} not found")
is_pv = get_pv_status(module, device)
if state == 'present':
if state == "present":
# Create PV if needed
if not is_pv:
if module.check_mode:
changed = True
actions.append('would be created')
actions.append("would be created")
else:
cmd = ['pvcreate']
cmd = ["pvcreate"]
if force:
cmd.append('-f')
cmd.append("-f")
cmd.append(device)
rc, out, err = module.run_command(cmd, check_rc=True)
changed = True
actions.append('created')
actions.append("created")
is_pv = True
# Handle resizing
@ -160,31 +161,31 @@ def main():
if module.check_mode:
# In check mode, assume resize would change
changed = True
actions.append('would be resized')
actions.append("would be resized")
else:
# Perform device rescan if each time
if rescan_device(module, device):
actions.append('rescanned')
actions.append("rescanned")
original_size = get_pv_size(module, device)
rc, out, err = module.run_command(['pvresize', device], check_rc=True)
rc, out, err = module.run_command(["pvresize", device], check_rc=True)
new_size = get_pv_size(module, device)
if new_size != original_size:
changed = True
actions.append('resized')
actions.append("resized")
elif state == 'absent':
elif state == "absent":
if is_pv:
if module.check_mode:
changed = True
actions.append('would be removed')
actions.append("would be removed")
else:
cmd = ['pvremove', '-y']
cmd = ["pvremove", "-y"]
if force:
cmd.append('-ff')
cmd.append("-ff")
changed = True
cmd.append(device)
rc, out, err = module.run_command(cmd, check_rc=True)
actions.append('removed')
actions.append("removed")
# Generate final message
if actions:
@ -194,5 +195,5 @@ def main():
module.exit_json(changed=changed, msg=msg)
if __name__ == '__main__':
if __name__ == "__main__":
main()