mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +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:
2
changelogs/fragments/11811-lvm_pv-use-cmdrunner.yml
Normal file
2
changelogs/fragments/11811-lvm_pv-use-cmdrunner.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
minor_changes:
|
||||||
|
- lvm_pv - migrate to ``CmdRunner`` using shared runners from ``module_utils/_lvm`` (https://github.com/ansible-collections/community.general/pull/11811).
|
||||||
@@ -54,7 +54,7 @@ def pvcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
|||||||
Runner for C(pvcreate). Used by: community.general.lvg, community.general.lvm_pv,
|
Runner for C(pvcreate). Used by: community.general.lvg, community.general.lvm_pv,
|
||||||
community.general.filesystem.
|
community.general.filesystem.
|
||||||
|
|
||||||
Suggested arg_formats keys: force yes devices
|
Suggested arg_formats keys: force yes device
|
||||||
"""
|
"""
|
||||||
return CmdRunner(
|
return CmdRunner(
|
||||||
module,
|
module,
|
||||||
@@ -62,7 +62,7 @@ def pvcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
|||||||
arg_formats=dict(
|
arg_formats=dict(
|
||||||
force=cmd_runner_fmt.as_bool("-f"),
|
force=cmd_runner_fmt.as_bool("-f"),
|
||||||
yes=cmd_runner_fmt.as_bool("--yes"),
|
yes=cmd_runner_fmt.as_bool("--yes"),
|
||||||
devices=cmd_runner_fmt.as_list(),
|
device=cmd_runner_fmt.as_list(),
|
||||||
),
|
),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
@@ -108,15 +108,15 @@ def pvremove_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
|
|||||||
"""
|
"""
|
||||||
Runner for C(pvremove). Used by: community.general.lvm_pv.
|
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(
|
return CmdRunner(
|
||||||
module,
|
module,
|
||||||
command="pvremove",
|
command=["pvremove", "-y"],
|
||||||
arg_formats=dict(
|
arg_formats=dict(
|
||||||
yes=cmd_runner_fmt.as_bool("-y"),
|
|
||||||
force=cmd_runner_fmt.as_bool("-ff"),
|
force=cmd_runner_fmt.as_bool("-ff"),
|
||||||
device=cmd_runner_fmt.as_list(),
|
device=cmd_runner_fmt.as_list(),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -73,18 +73,12 @@ import os
|
|||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
from ansible_collections.community.general.plugins.module_utils._lvm import (
|
||||||
def get_pv_status(module, device):
|
pvcreate_runner,
|
||||||
"""Check if the device is already a PV."""
|
pvremove_runner,
|
||||||
cmd = ["pvs", "--noheadings", "--readonly", device]
|
pvresize_runner,
|
||||||
return module.run_command(cmd)[0] == 0
|
pvs_runner,
|
||||||
|
)
|
||||||
|
|
||||||
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())
|
|
||||||
|
|
||||||
|
|
||||||
def rescan_device(module, device):
|
def rescan_device(module, device):
|
||||||
@@ -129,16 +123,30 @@ def main():
|
|||||||
|
|
||||||
device = module.params["device"]
|
device = module.params["device"]
|
||||||
state = module.params["state"]
|
state = module.params["state"]
|
||||||
force = module.params["force"]
|
|
||||||
resize = module.params["resize"]
|
resize = module.params["resize"]
|
||||||
changed = False
|
changed = False
|
||||||
actions = []
|
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
|
# 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")
|
module.fail_json(msg=f"Device {device} not found")
|
||||||
|
|
||||||
is_pv = get_pv_status(module, device)
|
is_pv = get_pv_status()
|
||||||
|
|
||||||
if state == "present":
|
if state == "present":
|
||||||
# Create PV if needed
|
# Create PV if needed
|
||||||
@@ -147,11 +155,7 @@ def main():
|
|||||||
changed = True
|
changed = True
|
||||||
actions.append("would be created")
|
actions.append("would be created")
|
||||||
else:
|
else:
|
||||||
cmd = ["pvcreate"]
|
pvcreate("force device", check_rc=True).run()
|
||||||
if force:
|
|
||||||
cmd.append("-f")
|
|
||||||
cmd.append(device)
|
|
||||||
rc, out, err = module.run_command(cmd, check_rc=True)
|
|
||||||
changed = True
|
changed = True
|
||||||
actions.append("created")
|
actions.append("created")
|
||||||
is_pv = True
|
is_pv = True
|
||||||
@@ -163,12 +167,12 @@ def main():
|
|||||||
changed = True
|
changed = True
|
||||||
actions.append("would be resized")
|
actions.append("would be resized")
|
||||||
else:
|
else:
|
||||||
# Perform device rescan if each time
|
# Perform device rescan each time
|
||||||
if rescan_device(module, device):
|
if rescan_device(module, device):
|
||||||
actions.append("rescanned")
|
actions.append("rescanned")
|
||||||
original_size = get_pv_size(module, device)
|
original_size = get_pv_size()
|
||||||
rc, out, err = module.run_command(["pvresize", device], check_rc=True)
|
pvresize("device", check_rc=True).run()
|
||||||
new_size = get_pv_size(module, device)
|
new_size = get_pv_size()
|
||||||
if new_size != original_size:
|
if new_size != original_size:
|
||||||
changed = True
|
changed = True
|
||||||
actions.append("resized")
|
actions.append("resized")
|
||||||
@@ -179,12 +183,8 @@ def main():
|
|||||||
changed = True
|
changed = True
|
||||||
actions.append("would be removed")
|
actions.append("would be removed")
|
||||||
else:
|
else:
|
||||||
cmd = ["pvremove", "-y"]
|
pvremove("force device", check_rc=True).run()
|
||||||
if force:
|
|
||||||
cmd.append("-ff")
|
|
||||||
changed = True
|
changed = True
|
||||||
cmd.append(device)
|
|
||||||
rc, out, err = module.run_command(cmd, check_rc=True)
|
|
||||||
actions.append("removed")
|
actions.append("removed")
|
||||||
|
|
||||||
# Generate final message
|
# Generate final message
|
||||||
|
|||||||
Reference in New Issue
Block a user