| 1 |
# Copyright (c) 2026, Alexei Znamensky <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import typing as t |
| 11 |
|
| 12 |
from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt |
| 13 |
|
| 14 |
if t.TYPE_CHECKING: |
| 15 |
from ansible.module_utils.basic import AnsibleModule |
| 16 |
|
| 17 |
|
| 18 |
# Options shared by the reporting commands: pvs, vgs, lvs (and pvdisplay in column mode). |
| 19 |
_REPORT_ARG_FORMATS = dict( |
| 20 |
noheadings=cmd_runner_fmt.as_fixed("--noheadings"), |
| 21 |
nosuffix=cmd_runner_fmt.as_fixed("--nosuffix"), |
| 22 |
readonly=cmd_runner_fmt.as_fixed("--readonly"), |
| 23 |
units=cmd_runner_fmt.as_opt_val("--units"), |
| 24 |
separator=cmd_runner_fmt.as_opt_val("--separator"), |
| 25 |
fields=cmd_runner_fmt.as_opt_val("-o"), |
| 26 |
select=cmd_runner_fmt.as_opt_val("--select"), |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
# ---- PV commands ---- |
| 31 |
|
| 32 |
|
| 33 |
def pvs_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 34 |
""" |
| 35 |
Runner for C(pvs). Used by: community.general.lvg, community.general.lvm_pv, |
| 36 |
community.general.lvm_pv_move_data, community.general.lvg_rename, |
| 37 |
community.general.filesystem. |
| 38 |
|
| 39 |
Suggested arg_formats keys: noheadings nosuffix readonly units separator fields select devices |
| 40 |
""" |
| 41 |
return CmdRunner( |
| 42 |
module, |
| 43 |
command="pvs", |
| 44 |
arg_formats=dict( |
| 45 |
**_REPORT_ARG_FORMATS, |
| 46 |
devices=cmd_runner_fmt.as_list(), |
| 47 |
), |
| 48 |
**kwargs, |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
def pvcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 53 |
""" |
| 54 |
Runner for C(pvcreate). Used by: community.general.lvg, community.general.lvm_pv, |
| 55 |
community.general.filesystem. |
| 56 |
|
| 57 |
Suggested arg_formats keys: pv_options force yes device |
| 58 |
|
| 59 |
Note: C(pv_options) accepts a pre-split list (e.g. from C(shlex.split())). |
| 60 |
""" |
| 61 |
return CmdRunner( |
| 62 |
module, |
| 63 |
command="pvcreate", |
| 64 |
arg_formats=dict( |
| 65 |
pv_options=cmd_runner_fmt.as_list(), |
| 66 |
force=cmd_runner_fmt.as_bool("-f"), |
| 67 |
yes=cmd_runner_fmt.as_bool("--yes"), |
| 68 |
device=cmd_runner_fmt.as_list(), |
| 69 |
), |
| 70 |
**kwargs, |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
def pvchange_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 75 |
""" |
| 76 |
Runner for C(pvchange). Used by: community.general.lvg, community.general.filesystem. |
| 77 |
|
| 78 |
Suggested arg_formats keys: uuid yes device |
| 79 |
""" |
| 80 |
return CmdRunner( |
| 81 |
module, |
| 82 |
command="pvchange", |
| 83 |
arg_formats=dict( |
| 84 |
uuid=cmd_runner_fmt.as_bool("-u"), |
| 85 |
yes=cmd_runner_fmt.as_bool("--yes"), |
| 86 |
device=cmd_runner_fmt.as_list(), |
| 87 |
), |
| 88 |
**kwargs, |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def pvresize_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 93 |
""" |
| 94 |
Runner for C(pvresize). Used by: community.general.lvg, community.general.lvm_pv, |
| 95 |
community.general.filesystem. |
| 96 |
|
| 97 |
Suggested arg_formats keys: set_size device |
| 98 |
""" |
| 99 |
return CmdRunner( |
| 100 |
module, |
| 101 |
command="pvresize", |
| 102 |
arg_formats=dict( |
| 103 |
set_size=cmd_runner_fmt.as_opt_val("--setphysicalvolumesize"), |
| 104 |
device=cmd_runner_fmt.as_list(), |
| 105 |
), |
| 106 |
**kwargs, |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
def pvremove_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 111 |
""" |
| 112 |
Runner for C(pvremove). Used by: community.general.lvm_pv. |
| 113 |
|
| 114 |
Suggested arg_formats keys: force device |
| 115 |
|
| 116 |
Note: C(-y) is always passed (non-interactive). C(force=True) passes C(-ff), |
| 117 |
which removes PVs even when part of a VG. |
| 118 |
""" |
| 119 |
return CmdRunner( |
| 120 |
module, |
| 121 |
command=["pvremove", "-y"], |
| 122 |
arg_formats=dict( |
| 123 |
force=cmd_runner_fmt.as_bool("-ff"), |
| 124 |
device=cmd_runner_fmt.as_list(), |
| 125 |
), |
| 126 |
**kwargs, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
def pvdisplay_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 131 |
""" |
| 132 |
Runner for C(pvdisplay). Used by: community.general.lvg. |
| 133 |
|
| 134 |
Enables machine-readable output via C(--columns). Fields such as C(dev_size), |
| 135 |
C(pe_start), and C(vg_extent_size) are only available through C(pvdisplay), |
| 136 |
not C(pvs). |
| 137 |
|
| 138 |
Suggested arg_formats keys: columns noheadings nosuffix units separator fields device |
| 139 |
""" |
| 140 |
return CmdRunner( |
| 141 |
module, |
| 142 |
command="pvdisplay", |
| 143 |
arg_formats=dict( |
| 144 |
columns=cmd_runner_fmt.as_fixed("--columns"), |
| 145 |
**_REPORT_ARG_FORMATS, |
| 146 |
device=cmd_runner_fmt.as_list(), |
| 147 |
), |
| 148 |
**kwargs, |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
def pvmove_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 153 |
""" |
| 154 |
Runner for C(pvmove). Used by: community.general.lvm_pv_move_data. |
| 155 |
|
| 156 |
Suggested arg_formats keys: auto_answer atomic autobackup verbosity source destination |
| 157 |
|
| 158 |
Note: C(auto_answer) matches the O(auto_answer) module parameter. |
| 159 |
Pass C(autobackup) as a boolean; it maps to C(--autobackup y/n). |
| 160 |
""" |
| 161 |
return CmdRunner( |
| 162 |
module, |
| 163 |
command="pvmove", |
| 164 |
arg_formats=dict( |
| 165 |
auto_answer=cmd_runner_fmt.as_bool("-y"), |
| 166 |
atomic=cmd_runner_fmt.as_bool("--atomic"), |
| 167 |
autobackup=cmd_runner_fmt.as_bool(["--autobackup", "y"], ["--autobackup", "n"], ignore_none=False), |
| 168 |
verbosity=cmd_runner_fmt.as_func(lambda v: [f"-{'v' * v}"] if v > 0 else []), |
| 169 |
source=cmd_runner_fmt.as_list(), |
| 170 |
destination=cmd_runner_fmt.as_list(), |
| 171 |
), |
| 172 |
**kwargs, |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
# ---- VG commands ---- |
| 177 |
|
| 178 |
|
| 179 |
def vgs_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 180 |
""" |
| 181 |
Runner for C(vgs). Used by: community.general.lvol, community.general.lvg, |
| 182 |
community.general.lvg_rename, community.general.lxc_container. |
| 183 |
|
| 184 |
Suggested arg_formats keys: noheadings nosuffix readonly units separator fields select vg |
| 185 |
""" |
| 186 |
return CmdRunner( |
| 187 |
module, |
| 188 |
command="vgs", |
| 189 |
arg_formats=dict( |
| 190 |
**_REPORT_ARG_FORMATS, |
| 191 |
vg=cmd_runner_fmt.as_list(), |
| 192 |
), |
| 193 |
**kwargs, |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
def vgcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 198 |
""" |
| 199 |
Runner for C(vgcreate). Used by: community.general.lvg. |
| 200 |
|
| 201 |
Suggested args order: vg_options pesize setautoactivation vg pvs |
| 202 |
|
| 203 |
Note: C(vg) and C(pvs) are positional — C(vg) must appear before C(pvs) |
| 204 |
in the args_order string. C(pvs) matches the O(pvs) module parameter in |
| 205 |
community.general.lvg. C(vg_options) accepts a pre-split list (e.g. from |
| 206 |
C(shlex.split())). C(setautoactivation) accepts C(True)/C(False)/C(None); |
| 207 |
C(None) omits the flag entirely (C(ignore_none=True)). |
| 208 |
""" |
| 209 |
return CmdRunner( |
| 210 |
module, |
| 211 |
command="vgcreate", |
| 212 |
arg_formats=dict( |
| 213 |
vg_options=cmd_runner_fmt.as_list(), |
| 214 |
pesize=cmd_runner_fmt.as_opt_val("-s"), |
| 215 |
yes=cmd_runner_fmt.as_bool("--yes"), |
| 216 |
setautoactivation=cmd_runner_fmt.as_bool( |
| 217 |
["--setautoactivation", "y"], |
| 218 |
["--setautoactivation", "n"], |
| 219 |
ignore_none=True, |
| 220 |
), |
| 221 |
vg=cmd_runner_fmt.as_list(), |
| 222 |
pvs=cmd_runner_fmt.as_list(), |
| 223 |
), |
| 224 |
**kwargs, |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
def vgchange_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 229 |
""" |
| 230 |
Runner for C(vgchange). Used by: community.general.lvg, community.general.lvg_rename. |
| 231 |
|
| 232 |
Suggested arg_formats keys: activate uuid setautoactivation help vg |
| 233 |
|
| 234 |
Note: C(activate) and C(setautoactivation) are passed as booleans and map to |
| 235 |
C(--activate y/n) and C(--setautoactivation y/n) respectively. |
| 236 |
""" |
| 237 |
return CmdRunner( |
| 238 |
module, |
| 239 |
command="vgchange", |
| 240 |
arg_formats=dict( |
| 241 |
activate=cmd_runner_fmt.as_bool( |
| 242 |
["--activate", "y"], |
| 243 |
["--activate", "n"], |
| 244 |
ignore_none=False, |
| 245 |
), |
| 246 |
uuid=cmd_runner_fmt.as_bool("-u"), |
| 247 |
setautoactivation=cmd_runner_fmt.as_bool( |
| 248 |
["--setautoactivation", "y"], |
| 249 |
["--setautoactivation", "n"], |
| 250 |
ignore_none=False, |
| 251 |
), |
| 252 |
help=cmd_runner_fmt.as_fixed("--help"), |
| 253 |
vg=cmd_runner_fmt.as_list(), |
| 254 |
), |
| 255 |
**kwargs, |
| 256 |
) |
| 257 |
|
| 258 |
|
| 259 |
def vgextend_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 260 |
""" |
| 261 |
Runner for C(vgextend). Used by: community.general.lvg. |
| 262 |
|
| 263 |
Suggested args order: vg pvs |
| 264 |
|
| 265 |
Note: C(vg) must appear before C(pvs) in the args_order string. |
| 266 |
C(pvs) matches the O(pvs) module parameter in community.general.lvg, |
| 267 |
but callers must pass the subset of PVs to add explicitly. |
| 268 |
""" |
| 269 |
return CmdRunner( |
| 270 |
module, |
| 271 |
command="vgextend", |
| 272 |
arg_formats=dict( |
| 273 |
vg=cmd_runner_fmt.as_list(), |
| 274 |
pvs=cmd_runner_fmt.as_list(), |
| 275 |
), |
| 276 |
**kwargs, |
| 277 |
) |
| 278 |
|
| 279 |
|
| 280 |
def vgreduce_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 281 |
""" |
| 282 |
Runner for C(vgreduce). Used by: community.general.lvg. |
| 283 |
|
| 284 |
Suggested args order: force vg pvs |
| 285 |
|
| 286 |
Note: C(vg) must appear before C(pvs) in the args_order string. |
| 287 |
C(pvs) matches the O(pvs) module parameter in community.general.lvg, |
| 288 |
but callers must pass the subset of PVs to remove explicitly. |
| 289 |
""" |
| 290 |
return CmdRunner( |
| 291 |
module, |
| 292 |
command="vgreduce", |
| 293 |
arg_formats=dict( |
| 294 |
force=cmd_runner_fmt.as_bool("--force"), |
| 295 |
vg=cmd_runner_fmt.as_list(), |
| 296 |
pvs=cmd_runner_fmt.as_list(), |
| 297 |
), |
| 298 |
**kwargs, |
| 299 |
) |
| 300 |
|
| 301 |
|
| 302 |
def vgremove_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 303 |
""" |
| 304 |
Runner for C(vgremove). Used by: community.general.lvg. |
| 305 |
|
| 306 |
Suggested arg_formats keys: force yes vg |
| 307 |
""" |
| 308 |
return CmdRunner( |
| 309 |
module, |
| 310 |
command="vgremove", |
| 311 |
arg_formats=dict( |
| 312 |
force=cmd_runner_fmt.as_bool("--force"), |
| 313 |
yes=cmd_runner_fmt.as_bool("--yes"), |
| 314 |
vg=cmd_runner_fmt.as_list(), |
| 315 |
), |
| 316 |
**kwargs, |
| 317 |
) |
| 318 |
|
| 319 |
|
| 320 |
def vgrename_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 321 |
""" |
| 322 |
Runner for C(vgrename). Used by: community.general.lvg_rename. |
| 323 |
|
| 324 |
Suggested args order: vg vg_new |
| 325 |
|
| 326 |
Note: C(vg) (old name or UUID) must appear before C(vg_new) in the args_order string. |
| 327 |
""" |
| 328 |
return CmdRunner( |
| 329 |
module, |
| 330 |
command="vgrename", |
| 331 |
arg_formats=dict( |
| 332 |
vg=cmd_runner_fmt.as_list(), |
| 333 |
vg_new=cmd_runner_fmt.as_list(), |
| 334 |
), |
| 335 |
**kwargs, |
| 336 |
) |
| 337 |
|
| 338 |
|
| 339 |
# ---- LV commands ---- |
| 340 |
|
| 341 |
|
| 342 |
def lvs_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 343 |
""" |
| 344 |
Runner for C(lvs). Used by: community.general.lvol, community.general.lxc_container. |
| 345 |
|
| 346 |
Suggested arg_formats keys: all noheadings nosuffix units separator fields select vg |
| 347 |
|
| 348 |
Note: C(all) includes hidden internal LVs such as thin pool metadata. |
| 349 |
""" |
| 350 |
return CmdRunner( |
| 351 |
module, |
| 352 |
command="lvs", |
| 353 |
arg_formats=dict( |
| 354 |
all=cmd_runner_fmt.as_fixed("-a"), |
| 355 |
**_REPORT_ARG_FORMATS, |
| 356 |
vg=cmd_runner_fmt.as_list(), |
| 357 |
), |
| 358 |
**kwargs, |
| 359 |
) |
| 360 |
|
| 361 |
|
| 362 |
def lvcreate_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 363 |
""" |
| 364 |
Runner for C(lvcreate). Used by: community.general.lvol, community.general.lxc_container. |
| 365 |
|
| 366 |
Suggested args order (normal LV): test yes lv size_L opts vg pvs |
| 367 |
Suggested args order (snapshot): test yes size_L is_snapshot lv opts vg |
| 368 |
Suggested args order (thin pool): test yes size_L opts thin vg pvs |
| 369 |
Suggested args order (thin vol): test yes lv size_V opts thin vg |
| 370 |
|
| 371 |
Note: C(vg) must appear after all option args and before C(pvs). |
| 372 |
For snapshots and thin volumes, pass the origin as C(vg="{vg}/{lv}"). |
| 373 |
C(opts) accepts a pre-split list (e.g. from C(shlex.split())). |
| 374 |
|
| 375 |
Intentional mismatches with module parameters: |
| 376 |
- C(lv) matches O(lv) in community.general.lvol (the LV name, passed as C(-n lv)). |
| 377 |
- C(is_snapshot) is a boolean flag for C(-s); it differs from the O(snapshot) |
| 378 |
module parameter, which is a string holding the snapshot name. |
| 379 |
- C(thin) is a boolean flag for C(-T); it differs from the O(thinpool) |
| 380 |
module parameter, which is the pool name string. |
| 381 |
- C(size_L), C(size_l), C(size_V) cannot match the O(size) module parameter |
| 382 |
because one module param maps to three distinct CLI size options; callers |
| 383 |
must always pass the appropriate size key explicitly. |
| 384 |
- C(pvs) matches O(pvs) in community.general.lvol; callers must pass the value |
| 385 |
explicitly since it is the list of PV paths, not the full module param value. |
| 386 |
- C(opts) matches O(opts) in community.general.lvol; callers must pass the |
| 387 |
pre-split list explicitly. |
| 388 |
""" |
| 389 |
return CmdRunner( |
| 390 |
module, |
| 391 |
command="lvcreate", |
| 392 |
arg_formats=dict( |
| 393 |
test=cmd_runner_fmt.as_bool("--test"), |
| 394 |
yes=cmd_runner_fmt.as_bool("--yes"), |
| 395 |
lv=cmd_runner_fmt.as_opt_val("-n"), |
| 396 |
size_L=cmd_runner_fmt.as_opt_val("-L"), |
| 397 |
size_l=cmd_runner_fmt.as_opt_val("-l"), |
| 398 |
size_V=cmd_runner_fmt.as_opt_val("-V"), |
| 399 |
is_snapshot=cmd_runner_fmt.as_bool("-s"), |
| 400 |
thin=cmd_runner_fmt.as_fixed("-T"), |
| 401 |
opts=cmd_runner_fmt.as_list(), |
| 402 |
vg=cmd_runner_fmt.as_list(), |
| 403 |
pvs=cmd_runner_fmt.as_list(), |
| 404 |
), |
| 405 |
**kwargs, |
| 406 |
) |
| 407 |
|
| 408 |
|
| 409 |
def lvchange_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 410 |
""" |
| 411 |
Runner for C(lvchange). Used by: community.general.lvol. |
| 412 |
|
| 413 |
Suggested arg_formats keys: active lv |
| 414 |
|
| 415 |
Note: C(active) matches the O(active) module parameter in community.general.lvol. |
| 416 |
It maps to C(-ay) when true and C(-an) when false. |
| 417 |
""" |
| 418 |
return CmdRunner( |
| 419 |
module, |
| 420 |
command="lvchange", |
| 421 |
arg_formats=dict( |
| 422 |
active=cmd_runner_fmt.as_bool("-ay", "-an", ignore_none=False), |
| 423 |
lv=cmd_runner_fmt.as_list(), |
| 424 |
), |
| 425 |
**kwargs, |
| 426 |
) |
| 427 |
|
| 428 |
|
| 429 |
def lvextend_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 430 |
""" |
| 431 |
Runner for C(lvextend). Used by: community.general.lvol. |
| 432 |
|
| 433 |
Suggested args order: test resizefs size_L lv pvs |
| 434 |
(use size_l instead of size_L when sizing by extents or percentage) |
| 435 |
|
| 436 |
Note: C(lv) must appear before C(pvs) in the args_order string. |
| 437 |
C(pvs) matches the O(pvs) module parameter in community.general.lvol. |
| 438 |
""" |
| 439 |
return CmdRunner( |
| 440 |
module, |
| 441 |
command="lvextend", |
| 442 |
arg_formats=dict( |
| 443 |
test=cmd_runner_fmt.as_bool("--test"), |
| 444 |
resizefs=cmd_runner_fmt.as_bool("--resizefs"), |
| 445 |
size_L=cmd_runner_fmt.as_opt_val("-L"), |
| 446 |
size_l=cmd_runner_fmt.as_opt_val("-l"), |
| 447 |
lv=cmd_runner_fmt.as_list(), |
| 448 |
pvs=cmd_runner_fmt.as_list(), |
| 449 |
), |
| 450 |
**kwargs, |
| 451 |
) |
| 452 |
|
| 453 |
|
| 454 |
def lvreduce_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 455 |
""" |
| 456 |
Runner for C(lvreduce). Used by: community.general.lvol. |
| 457 |
|
| 458 |
Suggested args order: test force resizefs size_L lv pvs |
| 459 |
(use size_l instead of size_L when sizing by extents or percentage) |
| 460 |
|
| 461 |
Note: C(lv) must appear before C(pvs) in the args_order string. |
| 462 |
C(pvs) matches the O(pvs) module parameter in community.general.lvol. |
| 463 |
""" |
| 464 |
return CmdRunner( |
| 465 |
module, |
| 466 |
command="lvreduce", |
| 467 |
arg_formats=dict( |
| 468 |
test=cmd_runner_fmt.as_bool("--test"), |
| 469 |
force=cmd_runner_fmt.as_bool("--force"), |
| 470 |
resizefs=cmd_runner_fmt.as_bool("--resizefs"), |
| 471 |
size_L=cmd_runner_fmt.as_opt_val("-L"), |
| 472 |
size_l=cmd_runner_fmt.as_opt_val("-l"), |
| 473 |
lv=cmd_runner_fmt.as_list(), |
| 474 |
pvs=cmd_runner_fmt.as_list(), |
| 475 |
), |
| 476 |
**kwargs, |
| 477 |
) |
| 478 |
|
| 479 |
|
| 480 |
def lvremove_runner(module: AnsibleModule, **kwargs) -> CmdRunner: |
| 481 |
""" |
| 482 |
Runner for C(lvremove). Used by: community.general.lvol, community.general.lxc_container. |
| 483 |
|
| 484 |
Suggested arg_formats keys: test force yes lv |
| 485 |
""" |
| 486 |
return CmdRunner( |
| 487 |
module, |
| 488 |
command="lvremove", |
| 489 |
arg_formats=dict( |
| 490 |
test=cmd_runner_fmt.as_bool("--test"), |
| 491 |
force=cmd_runner_fmt.as_bool("--force"), |
| 492 |
yes=cmd_runner_fmt.as_bool("--yes"), |
| 493 |
lv=cmd_runner_fmt.as_list(), |
| 494 |
), |
| 495 |
**kwargs, |
| 496 |
) |