| 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 = r""" |
| 10 |
module: pacemaker_resource |
| 11 |
short_description: Manage pacemaker resources |
| 12 |
author: |
| 13 |
- Dexter Le (@munchtoast) |
| 14 |
version_added: 10.5.0 |
| 15 |
description: |
| 16 |
- This module can manage resources in a Pacemaker cluster using the pacemaker CLI. |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Indicate desired state for cluster resource. |
| 28 |
- The states V(cleanup) and V(cloned) have been added in community.general 11.3.0. |
| 29 |
- If O(state=cloned) or O(state=present), you can set O(resource_clone_ids) and O(resource_clone_meta) to determine exactly what and how to clone. |
| 30 |
choices: [present, absent, cloned, enabled, disabled, cleanup] |
| 31 |
default: present |
| 32 |
type: str |
| 33 |
name: |
| 34 |
description: |
| 35 |
- Specify the resource name to create or clone to. |
| 36 |
- This is required if O(state=present), O(state=absent), O(state=enabled), or O(state=disabled). |
| 37 |
type: str |
| 38 |
resource_type: |
| 39 |
description: |
| 40 |
- Resource type to create. |
| 41 |
type: dict |
| 42 |
suboptions: |
| 43 |
resource_name: |
| 44 |
description: |
| 45 |
- Specify the resource type name. |
| 46 |
type: str |
| 47 |
resource_standard: |
| 48 |
description: |
| 49 |
- Specify the resource type standard. |
| 50 |
type: str |
| 51 |
resource_provider: |
| 52 |
description: |
| 53 |
- Specify the resource type providers. |
| 54 |
type: str |
| 55 |
resource_option: |
| 56 |
description: |
| 57 |
- Specify the resource option to create. |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
default: [] |
| 61 |
resource_operation: |
| 62 |
description: |
| 63 |
- List of operations to associate with resource. |
| 64 |
type: list |
| 65 |
elements: dict |
| 66 |
default: [] |
| 67 |
suboptions: |
| 68 |
operation_action: |
| 69 |
description: |
| 70 |
- Operation action to associate with resource. |
| 71 |
type: str |
| 72 |
operation_option: |
| 73 |
description: |
| 74 |
- Operation option to associate with action. |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
resource_meta: |
| 78 |
description: |
| 79 |
- List of meta to associate with resource. |
| 80 |
type: list |
| 81 |
elements: str |
| 82 |
resource_argument: |
| 83 |
description: |
| 84 |
- Action to associate with resource. |
| 85 |
type: dict |
| 86 |
suboptions: |
| 87 |
argument_action: |
| 88 |
description: |
| 89 |
- Action to apply to resource. |
| 90 |
type: str |
| 91 |
choices: [clone, master, group, promotable] |
| 92 |
argument_option: |
| 93 |
description: |
| 94 |
- Options to associate with resource action. |
| 95 |
type: list |
| 96 |
elements: str |
| 97 |
resource_clone_ids: |
| 98 |
description: |
| 99 |
- List of clone resource IDs to clone from. |
| 100 |
type: list |
| 101 |
elements: str |
| 102 |
version_added: 11.3.0 |
| 103 |
resource_clone_meta: |
| 104 |
description: |
| 105 |
- List of metadata to associate with clone resource. |
| 106 |
type: list |
| 107 |
elements: str |
| 108 |
version_added: 11.3.0 |
| 109 |
wait: |
| 110 |
description: |
| 111 |
- Timeout period for polling the resource creation. |
| 112 |
type: int |
| 113 |
default: 300 |
| 114 |
""" |
| 115 |
|
| 116 |
EXAMPLES = r""" |
| 117 |
--- |
| 118 |
- name: Create pacemaker resource |
| 119 |
hosts: localhost |
| 120 |
gather_facts: false |
| 121 |
tasks: |
| 122 |
- name: Create virtual-ip resource |
| 123 |
community.general.pacemaker_resource: |
| 124 |
state: present |
| 125 |
name: virtual-ip |
| 126 |
resource_type: |
| 127 |
resource_name: IPaddr2 |
| 128 |
resource_option: |
| 129 |
- "ip=[192.168.2.1]" |
| 130 |
resource_argument: |
| 131 |
argument_action: group |
| 132 |
argument_option: |
| 133 |
- master |
| 134 |
resource_operation: |
| 135 |
- operation_action: monitor |
| 136 |
operation_option: |
| 137 |
- interval=20 |
| 138 |
""" |
| 139 |
|
| 140 |
RETURN = r""" |
| 141 |
cluster_resources: |
| 142 |
description: The cluster resource output message. |
| 143 |
type: str |
| 144 |
sample: "Assumed agent name ocf:heartbeat:IPaddr2 (deduced from IPaddr2)" |
| 145 |
returned: always |
| 146 |
""" |
| 147 |
|
| 148 |
from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper |
| 149 |
from ansible_collections.community.general.plugins.module_utils._pacemaker import ( |
| 150 |
get_pacemaker_maintenance_mode, |
| 151 |
pacemaker_runner, |
| 152 |
wait_for_resource, |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
class PacemakerResource(StateModuleHelper): |
| 157 |
module = dict( |
| 158 |
argument_spec=dict( |
| 159 |
state=dict( |
| 160 |
type="str", default="present", choices=["present", "absent", "cloned", "enabled", "disabled", "cleanup"] |
| 161 |
), |
| 162 |
name=dict(type="str"), |
| 163 |
resource_type=dict( |
| 164 |
type="dict", |
| 165 |
options=dict( |
| 166 |
resource_name=dict(type="str"), |
| 167 |
resource_standard=dict(type="str"), |
| 168 |
resource_provider=dict(type="str"), |
| 169 |
), |
| 170 |
), |
| 171 |
resource_option=dict(type="list", elements="str", default=list()), |
| 172 |
resource_operation=dict( |
| 173 |
type="list", |
| 174 |
elements="dict", |
| 175 |
default=list(), |
| 176 |
options=dict( |
| 177 |
operation_action=dict(type="str"), |
| 178 |
operation_option=dict(type="list", elements="str"), |
| 179 |
), |
| 180 |
), |
| 181 |
resource_meta=dict(type="list", elements="str"), |
| 182 |
resource_argument=dict( |
| 183 |
type="dict", |
| 184 |
options=dict( |
| 185 |
argument_action=dict(type="str", choices=["clone", "master", "group", "promotable"]), |
| 186 |
argument_option=dict(type="list", elements="str"), |
| 187 |
), |
| 188 |
), |
| 189 |
resource_clone_ids=dict(type="list", elements="str"), |
| 190 |
resource_clone_meta=dict(type="list", elements="str"), |
| 191 |
wait=dict(type="int", default=300), |
| 192 |
), |
| 193 |
required_if=[ |
| 194 |
("state", "present", ["resource_type", "resource_option", "name"]), |
| 195 |
("state", "absent", ["name"]), |
| 196 |
("state", "enabled", ["name"]), |
| 197 |
("state", "disabled", ["name"]), |
| 198 |
], |
| 199 |
supports_check_mode=True, |
| 200 |
) |
| 201 |
|
| 202 |
def __init_module__(self): |
| 203 |
self.runner = pacemaker_runner(self.module) |
| 204 |
self.vars.set("previous_value", self._get()["out"]) |
| 205 |
self.vars.set("value", self.vars.previous_value, change=True, diff=True) |
| 206 |
self.module.params["name"] = self.module.params["name"] or None |
| 207 |
|
| 208 |
def __quit_module__(self): |
| 209 |
self.vars.set("value", self._get()["out"]) |
| 210 |
|
| 211 |
def _process_command_output(self, fail_on_err, ignore_err_msg=""): |
| 212 |
def process(rc, out, err): |
| 213 |
if fail_on_err and rc != 0 and err and ignore_err_msg not in err: |
| 214 |
self.do_raise(f"pcs failed with error (rc={rc}): {err}") |
| 215 |
out = out.rstrip() |
| 216 |
return None if out == "" else out |
| 217 |
|
| 218 |
return process |
| 219 |
|
| 220 |
def _get(self): |
| 221 |
with self.runner("cli_action state name") as ctx: |
| 222 |
result = ctx.run(cli_action="resource", state="status") |
| 223 |
return dict(rc=result[0], out=(result[1] if result[1] != "" else None), err=result[2]) |
| 224 |
|
| 225 |
def fmt_as_stack_argument(self, value, arg): |
| 226 |
if value is not None: |
| 227 |
return [x for k in value for x in (arg, k)] |
| 228 |
|
| 229 |
def state_absent(self): |
| 230 |
force = get_pacemaker_maintenance_mode(self.runner) |
| 231 |
with self.runner( |
| 232 |
"cli_action state name force", |
| 233 |
output_process=self._process_command_output(True, "does not exist"), |
| 234 |
check_mode_skip=True, |
| 235 |
) as ctx: |
| 236 |
ctx.run(cli_action="resource", force=force) |
| 237 |
|
| 238 |
def state_present(self): |
| 239 |
with self.runner( |
| 240 |
"cli_action state name resource_type resource_option resource_operation resource_meta resource_argument " |
| 241 |
"resource_clone_ids resource_clone_meta", |
| 242 |
output_process=self._process_command_output( |
| 243 |
not get_pacemaker_maintenance_mode(self.runner), "already exists" |
| 244 |
), |
| 245 |
check_mode_skip=True, |
| 246 |
) as ctx: |
| 247 |
ctx.run( |
| 248 |
cli_action="resource", |
| 249 |
resource_clone_ids=self.fmt_as_stack_argument(self.module.params["resource_clone_ids"], "clone"), |
| 250 |
) |
| 251 |
if not self.module.check_mode and self.vars.wait and not get_pacemaker_maintenance_mode(self.runner): |
| 252 |
wait_for_resource(self.runner, "resource", self.vars.name, self.vars.wait) |
| 253 |
|
| 254 |
def state_cloned(self): |
| 255 |
with self.runner( |
| 256 |
"cli_action state name resource_clone_ids resource_clone_meta", |
| 257 |
output_process=self._process_command_output( |
| 258 |
not get_pacemaker_maintenance_mode(self.runner), "already a clone resource" |
| 259 |
), |
| 260 |
check_mode_skip=True, |
| 261 |
) as ctx: |
| 262 |
ctx.run( |
| 263 |
cli_action="resource", |
| 264 |
resource_clone_meta=self.fmt_as_stack_argument(self.module.params["resource_clone_meta"], "meta"), |
| 265 |
) |
| 266 |
if not self.module.check_mode and self.vars.wait and not get_pacemaker_maintenance_mode(self.runner): |
| 267 |
wait_for_resource(self.runner, "resource", self.vars.name, self.vars.wait) |
| 268 |
|
| 269 |
def state_enabled(self): |
| 270 |
with self.runner( |
| 271 |
"cli_action state name", output_process=self._process_command_output(True, "Starting"), check_mode_skip=True |
| 272 |
) as ctx: |
| 273 |
ctx.run(cli_action="resource") |
| 274 |
|
| 275 |
def state_disabled(self): |
| 276 |
with self.runner( |
| 277 |
"cli_action state name", output_process=self._process_command_output(True, "Stopped"), check_mode_skip=True |
| 278 |
) as ctx: |
| 279 |
ctx.run(cli_action="resource") |
| 280 |
|
| 281 |
def state_cleanup(self): |
| 282 |
runner_args = ["cli_action", "state"] |
| 283 |
if self.module.params["name"]: |
| 284 |
runner_args.append("name") |
| 285 |
with self.runner( |
| 286 |
runner_args, output_process=self._process_command_output(True, "Clean"), check_mode_skip=True |
| 287 |
) as ctx: |
| 288 |
ctx.run(cli_action="resource") |
| 289 |
|
| 290 |
|
| 291 |
def main(): |
| 292 |
PacemakerResource.execute() |
| 293 |
|
| 294 |
|
| 295 |
if __name__ == "__main__": |
| 296 |
main() |