mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-19 07:11:25 +00:00
[PR #11811/ff5c34c4 backport][stable-12] lvm_pv - use CmdRunner (#11833)
lvm_pv - use CmdRunner (#11811)
* lvm_pv - migrate to CmdRunner using shared runners from module_utils/_lvm
* lvm_pv - add changelog fragment for #11811
* Update changelogs/fragments/11811-lvm_pv-use-cmdrunner.yml
---------
(cherry picked from commit ff5c34c4a7)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
@@ -54,7 +54,7 @@ def pvcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
||||
Runner for C(pvcreate). Used by: community.general.lvg, community.general.lvm_pv,
|
||||
community.general.filesystem.
|
||||
|
||||
Suggested arg_formats keys: force yes devices
|
||||
Suggested arg_formats keys: force yes device
|
||||
"""
|
||||
return CmdRunner(
|
||||
module,
|
||||
@@ -62,7 +62,7 @@ def pvcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
||||
arg_formats=dict(
|
||||
force=cmd_runner_fmt.as_bool("-f"),
|
||||
yes=cmd_runner_fmt.as_bool("--yes"),
|
||||
devices=cmd_runner_fmt.as_list(),
|
||||
device=cmd_runner_fmt.as_list(),
|
||||
),
|
||||
**kwargs,
|
||||
)
|
||||
@@ -108,15 +108,15 @@ def pvremove_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
||||
"""
|
||||
Runner for C(pvremove). Used by: community.general.lvm_pv.
|
||||
|
||||
Suggested arg_formats keys: yes force device
|
||||
Suggested arg_formats keys: force device
|
||||
|
||||
Note: C(force=True) passes C(-ff), which removes PVs even when part of a VG.
|
||||
Note: C(-y) is always passed (non-interactive). C(force=True) passes C(-ff),
|
||||
which removes PVs even when part of a VG.
|
||||
"""
|
||||
return CmdRunner(
|
||||
module,
|
||||
command="pvremove",
|
||||
command=["pvremove", "-y"],
|
||||
arg_formats=dict(
|
||||
yes=cmd_runner_fmt.as_bool("-y"),
|
||||
force=cmd_runner_fmt.as_bool("-ff"),
|
||||
device=cmd_runner_fmt.as_list(),
|
||||
),
|
||||
|
||||
@@ -73,18 +73,12 @@ import os
|
||||
|
||||
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]
|
||||
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]
|
||||
rc, out, err = module.run_command(cmd, check_rc=True)
|
||||
return int(out.strip())
|
||||
from ansible_collections.community.general.plugins.module_utils._lvm import (
|
||||
pvcreate_runner,
|
||||
pvremove_runner,
|
||||
pvresize_runner,
|
||||
pvs_runner,
|
||||
)
|
||||
|
||||
|
||||
def rescan_device(module, device):
|
||||
@@ -129,16 +123,30 @@ def main():
|
||||
|
||||
device = module.params["device"]
|
||||
state = module.params["state"]
|
||||
force = module.params["force"]
|
||||
resize = module.params["resize"]
|
||||
changed = False
|
||||
actions = []
|
||||
|
||||
pvs = pvs_runner(module)
|
||||
pvcreate = pvcreate_runner(module)
|
||||
pvresize = pvresize_runner(module)
|
||||
pvremove = pvremove_runner(module)
|
||||
|
||||
def get_pv_status():
|
||||
with pvs("noheadings readonly devices", check_rc=False) as ctx:
|
||||
rc, dummy, dummy = ctx.run(devices=[device])
|
||||
return rc == 0
|
||||
|
||||
def get_pv_size():
|
||||
with pvs("noheadings nosuffix readonly units fields devices") as ctx:
|
||||
dummy, out, dummy = ctx.run(units="b", fields="pv_size", devices=[device])
|
||||
return int(out.strip())
|
||||
|
||||
# Validate device existence for present state
|
||||
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)
|
||||
is_pv = get_pv_status()
|
||||
|
||||
if state == "present":
|
||||
# Create PV if needed
|
||||
@@ -147,11 +155,7 @@ def main():
|
||||
changed = True
|
||||
actions.append("would be created")
|
||||
else:
|
||||
cmd = ["pvcreate"]
|
||||
if force:
|
||||
cmd.append("-f")
|
||||
cmd.append(device)
|
||||
rc, out, err = module.run_command(cmd, check_rc=True)
|
||||
pvcreate("force device", check_rc=True).run()
|
||||
changed = True
|
||||
actions.append("created")
|
||||
is_pv = True
|
||||
@@ -163,12 +167,12 @@ def main():
|
||||
changed = True
|
||||
actions.append("would be resized")
|
||||
else:
|
||||
# Perform device rescan if each time
|
||||
# Perform device rescan each time
|
||||
if rescan_device(module, device):
|
||||
actions.append("rescanned")
|
||||
original_size = get_pv_size(module, device)
|
||||
rc, out, err = module.run_command(["pvresize", device], check_rc=True)
|
||||
new_size = get_pv_size(module, device)
|
||||
original_size = get_pv_size()
|
||||
pvresize("device", check_rc=True).run()
|
||||
new_size = get_pv_size()
|
||||
if new_size != original_size:
|
||||
changed = True
|
||||
actions.append("resized")
|
||||
@@ -179,12 +183,8 @@ def main():
|
||||
changed = True
|
||||
actions.append("would be removed")
|
||||
else:
|
||||
cmd = ["pvremove", "-y"]
|
||||
if force:
|
||||
cmd.append("-ff")
|
||||
pvremove("force device", check_rc=True).run()
|
||||
changed = True
|
||||
cmd.append(device)
|
||||
rc, out, err = module.run_command(cmd, check_rc=True)
|
||||
actions.append("removed")
|
||||
|
||||
# Generate final message
|
||||
|
||||
Reference in New Issue
Block a user