| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2025, Dexter Le <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
module: pacemaker_stonith |
| 11 |
short_description: Manage Pacemaker STONITH |
| 12 |
author: |
| 13 |
- Dexter Le (@munchtoast) |
| 14 |
version_added: 11.3.0 |
| 15 |
description: |
| 16 |
- This module manages STONITH in a Pacemaker cluster using the Pacemaker CLI. |
| 17 |
seealso: |
| 18 |
- name: Pacemaker STONITH documentation |
| 19 |
description: Complete documentation for Pacemaker STONITH. |
| 20 |
link: https://clusterlabs.org/projects/pacemaker/doc/3.0/Pacemaker_Explained/html/resources.html#stonith |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: partial |
| 28 |
details: |
| 29 |
- Only works when check mode is not enabled. |
| 30 |
options: |
| 31 |
state: |
| 32 |
description: |
| 33 |
- Indicate desired state for cluster STONITH. |
| 34 |
choices: [present, absent, enabled, disabled] |
| 35 |
default: present |
| 36 |
type: str |
| 37 |
name: |
| 38 |
description: |
| 39 |
- Specify the STONITH name to create. |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
stonith_type: |
| 43 |
description: |
| 44 |
- Specify the STONITH device type. |
| 45 |
type: str |
| 46 |
stonith_options: |
| 47 |
description: |
| 48 |
- Specify the STONITH option to create. |
| 49 |
type: list |
| 50 |
elements: str |
| 51 |
default: [] |
| 52 |
stonith_operations: |
| 53 |
description: |
| 54 |
- List of operations to associate with STONITH. |
| 55 |
type: list |
| 56 |
elements: dict |
| 57 |
default: [] |
| 58 |
suboptions: |
| 59 |
operation_action: |
| 60 |
description: |
| 61 |
- Operation action to associate with STONITH. |
| 62 |
type: str |
| 63 |
operation_options: |
| 64 |
description: |
| 65 |
- Operation options to associate with action. |
| 66 |
type: list |
| 67 |
elements: str |
| 68 |
stonith_metas: |
| 69 |
description: |
| 70 |
- List of metadata to associate with STONITH. |
| 71 |
type: list |
| 72 |
elements: str |
| 73 |
stonith_argument: |
| 74 |
description: |
| 75 |
- Action to associate with STONITH. |
| 76 |
type: dict |
| 77 |
suboptions: |
| 78 |
argument_action: |
| 79 |
description: |
| 80 |
- Action to apply to STONITH. |
| 81 |
type: str |
| 82 |
choices: [group, before, after] |
| 83 |
argument_options: |
| 84 |
description: |
| 85 |
- Options to associate with STONITH action. |
| 86 |
type: list |
| 87 |
elements: str |
| 88 |
agent_validation: |
| 89 |
description: |
| 90 |
- Enabled agent validation for STONITH creation. |
| 91 |
type: bool |
| 92 |
default: false |
| 93 |
wait: |
| 94 |
description: |
| 95 |
- Timeout period for polling the STONITH creation. |
| 96 |
type: int |
| 97 |
default: 300 |
| 98 |
""" |
| 99 |
|
| 100 |
EXAMPLES = """ |
| 101 |
- name: Create virtual-ip STONITH |
| 102 |
community.general.pacemaker_stonith: |
| 103 |
state: present |
| 104 |
name: virtual-stonith |
| 105 |
stonith_type: fence_virt |
| 106 |
stonith_options: |
| 107 |
- "pcmk_host_list=f1" |
| 108 |
stonith_operations: |
| 109 |
- operation_action: monitor |
| 110 |
operation_options: |
| 111 |
- "interval=30s" |
| 112 |
""" |
| 113 |
|
| 114 |
RETURN = """ |
| 115 |
previous_value: |
| 116 |
description: The value of the STONITH before executing the module. |
| 117 |
type: str |
| 118 |
sample: " * virtual-stonith\t(stonith:fence_virt):\t Started" |
| 119 |
returned: on success |
| 120 |
value: |
| 121 |
description: The value of the STONITH after executing the module. |
| 122 |
type: str |
| 123 |
sample: " * virtual-stonith\t(stonith:fence_virt):\t Started" |
| 124 |
returned: on success |
| 125 |
""" |
| 126 |
|
| 127 |
from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper |
| 128 |
from ansible_collections.community.general.plugins.module_utils._pacemaker import pacemaker_runner, wait_for_resource |
| 129 |
|
| 130 |
|
| 131 |
class PacemakerStonith(StateModuleHelper): |
| 132 |
module = dict( |
| 133 |
argument_spec=dict( |
| 134 |
state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]), |
| 135 |
name=dict(type="str", required=True), |
| 136 |
stonith_type=dict(type="str"), |
| 137 |
stonith_options=dict(type="list", elements="str", default=[]), |
| 138 |
stonith_operations=dict( |
| 139 |
type="list", |
| 140 |
elements="dict", |
| 141 |
default=[], |
| 142 |
options=dict( |
| 143 |
operation_action=dict(type="str"), |
| 144 |
operation_options=dict(type="list", elements="str"), |
| 145 |
), |
| 146 |
), |
| 147 |
stonith_metas=dict(type="list", elements="str"), |
| 148 |
stonith_argument=dict( |
| 149 |
type="dict", |
| 150 |
options=dict( |
| 151 |
argument_action=dict(type="str", choices=["before", "after", "group"]), |
| 152 |
argument_options=dict(type="list", elements="str"), |
| 153 |
), |
| 154 |
), |
| 155 |
agent_validation=dict(type="bool", default=False), |
| 156 |
wait=dict(type="int", default=300), |
| 157 |
), |
| 158 |
required_if=[("state", "present", ["stonith_type", "stonith_options"])], |
| 159 |
supports_check_mode=True, |
| 160 |
) |
| 161 |
|
| 162 |
def __init_module__(self): |
| 163 |
self.runner = pacemaker_runner(self.module) |
| 164 |
self.vars.set("previous_value", self._get()["out"]) |
| 165 |
self.vars.set("value", self.vars.previous_value, change=True, diff=True) |
| 166 |
|
| 167 |
def __quit_module__(self): |
| 168 |
self.vars.set("value", self._get()["out"]) |
| 169 |
|
| 170 |
def _process_command_output(self, fail_on_err, ignore_err_msg=""): |
| 171 |
def process(rc, out, err): |
| 172 |
if fail_on_err and rc != 0 and err and ignore_err_msg not in err: |
| 173 |
self.do_raise(f"pcs failed with error (rc={rc}): {err}") |
| 174 |
out = out.rstrip() |
| 175 |
return None if out == "" else out |
| 176 |
|
| 177 |
return process |
| 178 |
|
| 179 |
def _get(self): |
| 180 |
with self.runner("cli_action state name") as ctx: |
| 181 |
result = ctx.run(cli_action="stonith", state="status") |
| 182 |
return dict(rc=result[0], out=result[1] if result[1] != "" else None, err=result[2]) |
| 183 |
|
| 184 |
def fmt_stonith_resource(self): |
| 185 |
return dict(resource_name=self.vars.stonith_type) |
| 186 |
|
| 187 |
# TODO: Pluralize operation_options in separate PR and remove this helper fmt function |
| 188 |
def fmt_stonith_operations(self): |
| 189 |
modified_stonith_operations = [] |
| 190 |
for stonith_operation in self.vars.stonith_operations: |
| 191 |
modified_stonith_operations.append( |
| 192 |
dict( |
| 193 |
operation_action=stonith_operation.get("operation_action"), |
| 194 |
operation_option=stonith_operation.get("operation_options"), |
| 195 |
) |
| 196 |
) |
| 197 |
return modified_stonith_operations |
| 198 |
|
| 199 |
def state_absent(self): |
| 200 |
with self.runner( |
| 201 |
"cli_action state name", |
| 202 |
output_process=self._process_command_output(True, "does not exist"), |
| 203 |
check_mode_skip=True, |
| 204 |
) as ctx: |
| 205 |
ctx.run(cli_action="stonith") |
| 206 |
|
| 207 |
def state_present(self): |
| 208 |
with self.runner( |
| 209 |
"cli_action state name resource_type resource_option resource_operation resource_meta resource_argument agent_validation", |
| 210 |
output_process=self._process_command_output(True, "already exists"), |
| 211 |
check_mode_skip=True, |
| 212 |
) as ctx: |
| 213 |
ctx.run( |
| 214 |
cli_action="stonith", |
| 215 |
resource_type=self.fmt_stonith_resource(), |
| 216 |
resource_option=self.vars.stonith_options, |
| 217 |
resource_operation=self.fmt_stonith_operations(), |
| 218 |
resource_meta=self.vars.stonith_metas, |
| 219 |
resource_argument=self.vars.stonith_argument, |
| 220 |
) |
| 221 |
if not self.module.check_mode and self.vars.wait: |
| 222 |
wait_for_resource(self.runner, "stonith", self.vars.name, self.vars.wait) |
| 223 |
|
| 224 |
def state_enabled(self): |
| 225 |
with self.runner( |
| 226 |
"cli_action state name", output_process=self._process_command_output(True, "Starting"), check_mode_skip=True |
| 227 |
) as ctx: |
| 228 |
ctx.run(cli_action="stonith") |
| 229 |
|
| 230 |
def state_disabled(self): |
| 231 |
with self.runner( |
| 232 |
"cli_action state name", output_process=self._process_command_output(True, "Stopped"), check_mode_skip=True |
| 233 |
) as ctx: |
| 234 |
ctx.run(cli_action="stonith") |
| 235 |
|
| 236 |
|
| 237 |
def main(): |
| 238 |
PacemakerStonith.execute() |
| 239 |
|
| 240 |
|
| 241 |
if __name__ == "__main__": |
| 242 |
main() |