Debian Codegraph @

ansible_collections/community/general/plugins/modules/pacemaker_cluster.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Mathieu Bultel <[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_cluster
11 short_description: Manage pacemaker clusters
12 author:
13 - Mathieu Bultel (@matbu)
14 - Dexter Le (@munchtoast)
15 description:
16 - This module can manage a pacemaker cluster and nodes from Ansible 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 of the cluster.
28 - The value V(maintenance) has been added in community.general 11.1.0.
29 choices: [cleanup, offline, online, restart, maintenance]
30 type: str
31 required: true
32 name:
33 description:
34 - Specify which node of the cluster you want to manage. V(null) == the cluster status itself, V(all) == check the status
35 of all nodes.
36 type: str
37 aliases: ['node']
38 timeout:
39 description:
40 - Timeout period (in seconds) for polling the cluster operation.
41 type: int
42 default: 300
43 force:
44 description:
45 - Force the change of the cluster state.
46 type: bool
47 default: true
48 """
49
50 EXAMPLES = r"""
51 - name: Set cluster Online
52 hosts: localhost
53 gather_facts: false
54 tasks:
55 - name: Get cluster state
56 community.general.pacemaker_cluster:
57 state: online
58 """
59
60 RETURN = r"""
61 out:
62 description: The output of the current state of the cluster. It returns a list of the nodes state.
63 type: str
64 sample: 'out: [[" overcloud-controller-0", " Online"]]}'
65 returned: always
66 """
67
68 from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper
69 from ansible_collections.community.general.plugins.module_utils._pacemaker import (
70 get_pacemaker_maintenance_mode,
71 pacemaker_runner,
72 )
73
74
75 class PacemakerCluster(StateModuleHelper):
76 module = dict(
77 argument_spec=dict(
78 state=dict(type="str", choices=["cleanup", "offline", "online", "restart", "maintenance"], required=True),
79 name=dict(type="str", aliases=["node"]),
80 timeout=dict(type="int", default=300),
81 force=dict(type="bool", default=True),
82 ),
83 supports_check_mode=True,
84 )
85 default_state = ""
86
87 def __init_module__(self):
88 self.runner = pacemaker_runner(self.module)
89 self.vars.set("apply_all", True if not self.module.params["name"] else False)
90 get_args = dict(cli_action="cluster", state="status", name=None, apply_all=self.vars.apply_all)
91 if self.module.params["state"] == "maintenance":
92 get_args["cli_action"] = "property"
93 get_args["state"] = "config"
94 get_args["name"] = "maintenance-mode"
95 elif self.module.params["state"] == "cleanup":
96 get_args["cli_action"] = "resource"
97 get_args["name"] = self.module.params["name"]
98
99 self.vars.set("get_args", get_args)
100 self.vars.set("previous_value", self._get()["out"])
101 self.vars.set("value", self.vars.previous_value, change=True, diff=True)
102
103 if self.module.params["state"] == "cleanup":
104 self.module.deprecate(
105 'The value `cleanup` for "state" is being deprecated, use pacemaker_resource module instead.',
106 version="14.0.0",
107 collection_name="community.general",
108 )
109
110 def __quit_module__(self):
111 self.vars.set("value", self._get()["out"])
112
113 def _process_command_output(self, fail_on_err, ignore_err_msg=""):
114 def process(rc, out, err):
115 if fail_on_err and rc != 0 and err and ignore_err_msg not in err:
116 self.do_raise(f"pcs failed with error (rc={rc}): {err}")
117 out = out.rstrip()
118 return None if out == "" else out
119
120 return process
121
122 def _get(self):
123 with self.runner("cli_action state name") as ctx:
124 result = ctx.run(
125 cli_action=self.vars.get_args["cli_action"],
126 state=self.vars.get_args["state"],
127 name=self.vars.get_args["name"],
128 )
129 return dict(rc=result[0], out=(result[1] if result[1] != "" else None), err=result[2])
130
131 def state_cleanup(self):
132 with self.runner(
133 "cli_action state name", output_process=self._process_command_output(True, "Fail"), check_mode_skip=True
134 ) as ctx:
135 ctx.run(cli_action="resource")
136
137 def state_offline(self):
138 with self.runner(
139 "cli_action state name apply_all wait",
140 output_process=self._process_command_output(True, "not currently running"),
141 check_mode_skip=True,
142 ) as ctx:
143 ctx.run(cli_action="cluster", apply_all=self.vars.apply_all, wait=self.module.params["timeout"])
144
145 def state_online(self):
146 with self.runner(
147 "cli_action state name apply_all wait",
148 output_process=self._process_command_output(True, "currently running"),
149 check_mode_skip=True,
150 ) as ctx:
151 ctx.run(cli_action="cluster", apply_all=self.vars.apply_all, wait=self.module.params["timeout"])
152
153 if get_pacemaker_maintenance_mode(self.runner):
154 with self.runner(
155 "cli_action state name", output_process=self._process_command_output(True, "Fail"), check_mode_skip=True
156 ) as ctx:
157 ctx.run(cli_action="property", state="maintenance", name="maintenance-mode=false")
158
159 def state_maintenance(self):
160 with self.runner(
161 "cli_action state name", output_process=self._process_command_output(True, "Fail"), check_mode_skip=True
162 ) as ctx:
163 ctx.run(cli_action="property", name="maintenance-mode=true")
164
165 def state_restart(self):
166 with self.runner(
167 "cli_action state name apply_all wait",
168 output_process=self._process_command_output(True, "not currently running"),
169 check_mode_skip=True,
170 ) as ctx:
171 ctx.run(
172 cli_action="cluster", state="offline", apply_all=self.vars.apply_all, wait=self.module.params["timeout"]
173 )
174 ctx.run(
175 cli_action="cluster", state="online", apply_all=self.vars.apply_all, wait=self.module.params["timeout"]
176 )
177
178 if get_pacemaker_maintenance_mode(self.runner):
179 with self.runner(
180 "cli_action state name", output_process=self._process_command_output(True, "Fail"), check_mode_skip=True
181 ) as ctx:
182 ctx.run(cli_action="property", state="maintenance", name="maintenance-mode=false")
183
184
185 def main():
186 PacemakerCluster.execute()
187
188
189 if __name__ == "__main__":
190 main()