Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_ceph_osd.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2025, (@teslamania) <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 DOCUMENTATION = r"""
9 module: proxmox_ceph_osd
10 version_added: 1.5.0
11 short_description: Manage osd.
12 description:
13 - This module allows you to add or delete an osd.
14 - You can also in, out or scrub an osd.
15 author: Vial Nicolas (@teslamania)
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21
22 options:
23 cleanup:
24 description:
25 - If set remove partition table entries.
26 - Used with O(state=absent).
27 required: false
28 type: bool
29 default: false
30 crush_device_class:
31 description:
32 - Set the device class of the OSD in crush.
33 - Used with O(state=present).
34 required: false
35 type: str
36 db_dev:
37 description:
38 - Block device name for block.db.
39 - Used with O(state=present).
40 required: false
41 type: str
42 db_dev_size:
43 description:
44 - If a block.db is requested but the size is not given, will be automatically selected by
45 - bluestore_block_db_size from the ceph database (osd or global section) or config (osd or global section) in that order.
46 - If this is not available, it will be sized 10% of the size of the OSD device.
47 - Fails if the available size is not enough.
48 - Used with O(state=present).
49 required: false
50 type: int
51 deep:
52 description:
53 - If set, instructs a deep scrub instead of a normal one.
54 - Used with O(state=scrub).
55 required: false
56 type: bool
57 default: false
58 dev:
59 description:
60 - Block device name.
61 - Required when O(state=present).
62 required: false
63 type: str
64 encrypted:
65 description:
66 - Enables encryption of the OSD.
67 - Used with O(state=present).
68 required: false
69 type: bool
70 default: false
71 node:
72 description: The cluster node name.
73 required: true
74 type: str
75 osdid:
76 description:
77 - Osd id.
78 - Required when O(state=absent), O(state=in), O(state=out), O(state=scrub), O(state=start), O(state=stop) or O-state=restart).
79 required: false
80 type: int
81 osds_per_device:
82 description:
83 - OSD services per physical device.
84 - Used with O(state=present).
85 required: false
86 type: int
87 state:
88 description: create, delete, in, out or scrub
89 required: true
90 choices: ['present', 'absent', 'in', 'out', 'scrub', 'start', 'stop', 'restart']
91 type: str
92 wal_dev:
93 description:
94 - Block device name for block.wal.
95 - Used with O(state=present).
96 required: false
97 type: str
98 wal_dev_size:
99 description:
100 - If a block.wal is requested but the size is not given, will be automatically selected by
101 - bluestore_block_wal_size from the ceph database (osd or global section) or config (osd or global section) in that order.
102 - If this is not available, it will be sized 1% of the size of the OSD device.
103 - Fails if the available size is not enough.
104 - Used with O(state=present).
105 required: false
106 type: int
107
108 extends_documentation_fragment:
109 - community.proxmox.proxmox.actiongroup_proxmox
110 - community.proxmox.proxmox.documentation
111 - community.proxmox.attributes
112 - community.proxmox.attributes.info_module
113 """
114
115
116 EXAMPLES = r"""
117 - name: Add an osd
118 community.proxmox.proxmox_ceph_osd:
119 node: proxmox-01
120 dev: /dev/sdb
121 state: present
122
123 - name: Delete an osd
124 community.proxmox.proxmox_ceph_osd:
125 node: proxmox-01
126 osdid: 2
127 cleanup: true
128 state: absent
129
130 - name: Scrub an osd
131 community.proxmox.proxmox_ceph_osd:
132 node: proxmox-01
133 osdid: 2
134 deep: true
135 state: scrub
136
137 - name: Out an osd
138 community.proxmox.proxmox_ceph_osd:
139 node: proxmox-01
140 osdid: 2
141 state: out
142
143 - name: In an osd
144 community.proxmox.proxmox_ceph_osd:
145 node: proxmox-01
146 osdid: 2
147 state: in
148
149 - name: Start an osd
150 community.proxmox.proxmox_ceph_osd:
151 node: proxmox-01
152 osdid: 2
153 state: start
154
155 - name: Stop an osd
156 community.proxmox.proxmox_ceph_osd:
157 node: proxmox-01
158 osdid: 2
159 state: stop
160
161 - name: Restart an osd
162 community.proxmox.proxmox_ceph_osd:
163 node: proxmox-01
164 osdid: 2
165 state: restart
166 """
167
168 RETURN = r"""
169 msg:
170 description: The output message that the module generates.
171 type: str
172 returned: always
173 """
174
175 from ansible.module_utils.common.text.converters import to_native
176
177 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
178 ProxmoxAnsible,
179 ansible_to_proxmox_bool,
180 create_proxmox_module,
181 )
182
183
184 def module_args():
185 return dict(
186 node=dict(type="str", required=True),
187 state=dict(choices=["present", "absent", "in", "out", "scrub", "start", "stop", "restart"], required=True),
188 dev=dict(type="str"),
189 osdid=dict(type="int"),
190 cleanup=dict(type="bool", default=False),
191 crush_device_class=dict(type="str"),
192 db_dev=dict(type="str"),
193 db_dev_size=dict(type="int"),
194 deep=dict(type="bool", default=False),
195 encrypted=dict(type="bool", default=False),
196 osds_per_device=dict(type="int"),
197 wal_dev=dict(type="str"),
198 wal_dev_size=dict(type="int"),
199 )
200
201
202 def module_options():
203 return dict(
204 required_if=[
205 ("state", "present", ["dev"]),
206 ("state", "absent", ["osdid"]),
207 ("state", "in", ["osdid"]),
208 ("state", "out", ["osdid"]),
209 ("state", "scrub", ["osdid"]),
210 ("state", "start", ["osdid"]),
211 ("state", "stop", ["osdid"]),
212 ("state", "restart", ["osdid"]),
213 ],
214 )
215
216
217 class ProxmoxCephOsdAnsible(ProxmoxAnsible):
218 def check_node(self, node):
219 nodes = self.proxmox_api.cluster.resources.get(type="node")
220 if node not in [item["node"] for item in nodes]:
221 self.module.fail_json(msg=f"Node {node} does not exist.")
222
223 def check_dev(self, node, dev):
224 devs = self.proxmox_api.nodes(node).disks.list.get()
225 if dev not in [dev_data["devpath"] for dev_data in devs]:
226 self.module.fail_json(msg=f"{dev} does not exist on the node {node}.")
227 dev_data = list(filter(lambda d: d["devpath"] == dev, devs))[0]
228 if "used" in dev_data:
229 if int(dev_data["osdid"]) >= 0:
230 self.module.exit_json(
231 changed=False,
232 msg=f"{dev} is already an osd.",
233 )
234 else:
235 self.module.fail_json(msg=f"{dev} is already in use by the node {node}.")
236
237 def check_osd(self, node, osdid):
238 try:
239 root = self.proxmox_api.nodes(node).ceph.osd.get()
240 root = root["root"]["children"][0]["children"]
241 return True in [
242 any(d.get("id") == str(osdid) for d in host.get("children"))
243 for host in root
244 if host.get("children") is not None
245 ]
246 except Exception as e:
247 self.module.fail_json(msg=f"Failure checking osd with exception : {to_native(e)}.")
248
249 # If in return True Else return False, fails if osdid not exists
250 def check_osd_in(self, node, osdid):
251 try:
252 root = self.proxmox_api.nodes(node).ceph.osd.get()
253 root = root["root"]["children"][0]["children"]
254 for host in root:
255 if host.get("children") is not None:
256 for osd in host.get("children"):
257 if osd.get("id") == str(osdid):
258 return osd["in"] == 1
259 return False
260 except Exception as e:
261 self.module.fail_json(msg=f"Failure checking osd in with exception : {to_native(e)}.")
262
263 # If status==up return True Else return False, fails if osdid not exists
264 def check_osd_started(self, node, osdid):
265 try:
266 root = self.proxmox_api.nodes(node).ceph.osd.get()
267 root = root["root"]["children"][0]["children"]
268 for host in root:
269 if host.get("children") is not None:
270 for osd in host.get("children"):
271 if osd.get("id") == str(osdid):
272 return osd["status"] == "up"
273 return False
274 except Exception as e:
275 self.module.fail_json(msg=f"Failure checking osd status with exception : {to_native(e)}.")
276
277 def add_osd(self, node, dev, encrypted, args):
278 self.check_node(node)
279 self.check_dev(node, dev)
280 if not self.module.check_mode:
281 self.proxmox_api.nodes(node).ceph.osd.create(
282 node=node,
283 dev=dev,
284 encrypted=encrypted,
285 **args,
286 )
287 msg = "Osd added."
288 else:
289 msg = "Osd would be added."
290 self.module.exit_json(
291 changed=True,
292 msg=msg,
293 )
294
295 def in_osd(self, node, osdid):
296 self.check_node(node)
297 if not self.check_osd(node, osdid):
298 self.module.fail_json(msg=f"Osd {osdid} does not exist.")
299 if not self.check_osd_in(node, osdid):
300 if not self.module.check_mode:
301 self.proxmox_api(f"nodes/{node}/ceph/osd/{osdid}/in").create()
302 msg = f"In osd {osdid}."
303 else:
304 msg = f"Would in osd {osdid}."
305 self.module.exit_json(changed=True, msg=msg)
306 else:
307 self.module.exit_json(changed=False, msg=f"Osd {osdid} already in.")
308
309 def out_osd(self, node, osdid):
310 self.check_node(node)
311 if not self.check_osd(node, osdid):
312 self.module.fail_json(msg=f"Osd {osdid} does not exist.")
313 if self.check_osd_in(node, osdid):
314 if not self.module.check_mode:
315 self.proxmox_api.nodes(node).ceph.osd(osdid).out.create()
316 msg = f"Out osd {osdid}."
317 else:
318 msg = f"Would out osd {osdid}."
319 self.module.exit_json(changed=True, msg=msg)
320 else:
321 self.module.exit_json(changed=False, msg=f"Osd {osdid} already out.")
322
323 def scrub_osd(self, node, osdid, deep):
324 self.check_node(node)
325 if not self.check_osd(node, osdid):
326 self.module.fail_json(msg=f"Osd {osdid} does not exist.")
327 if self.check_osd_started(node, osdid):
328 if not self.module.check_mode:
329 self.proxmox_api.nodes(node).ceph.osd(osdid).scrub.create(deep=deep)
330 msg = f"Scrub Osd {osdid}."
331 else:
332 msg = f"Would scrub Osd {osdid}."
333 self.module.exit_json(changed=True, msg=msg)
334 else:
335 self.module.fail_json(msg=f"Osd {osdid} is not up.")
336
337 def del_osd(self, node, osdid, cleanup):
338 self.check_node(node)
339 if not self.check_osd(node, osdid):
340 self.module.exit_json(changed=False, msg=f"Osd {osdid} not present.")
341 if not self.check_osd_in(node, osdid):
342 if not self.check_osd_started(node, osdid):
343 if not self.module.check_mode:
344 self.proxmox_api.nodes(node).ceph.osd(osdid).delete(cleanup=cleanup)
345 msg = f"Osd {osdid} deleted."
346 else:
347 msg = f"Osd {osdid} would be deleted."
348 self.module.exit_json(changed=True, msg=msg)
349 else:
350 self.module.fail_json(
351 msg=f"Cannot delete osd {osdid} is started.",
352 )
353 else:
354 self.module.fail_json(
355 msg=f"Cannot delete osd {osdid} is in.",
356 )
357
358 def start_osd(self, node, osdid):
359 self.check_node(node)
360 if not self.check_osd(node, osdid):
361 self.module.fail_json(msg=f"Osd {osdid} does not exist.")
362 if not self.check_osd_started(node, osdid):
363 if not self.module.check_mode:
364 self.proxmox_api.nodes(node).ceph.start.create(service=f"osd.{osdid}")
365 msg = f"Start Osd {osdid}."
366 else:
367 msg = f"Would start Osd {osdid}."
368 self.module.exit_json(changed=True, msg=msg)
369 else:
370 self.module.exit_json(
371 changed=False,
372 msg=f"Osd {osdid} already started.",
373 )
374
375 def stop_osd(self, node, osdid):
376 self.check_node(node)
377 if not self.check_osd(node, osdid):
378 self.module.fail_json(msg=f"Osd {osdid} does not exist.")
379 if self.check_osd_started(node, osdid):
380 if not self.module.check_mode:
381 self.proxmox_api.nodes(node).ceph.stop.create(service=f"osd.{osdid}")
382 msg = f"Stop Osd {osdid}."
383 else:
384 msg = f"Would stop Osd {osdid}."
385 self.module.exit_json(changed=True, msg=msg)
386 else:
387 self.module.exit_json(
388 changed=False,
389 msg=f"Osd {osdid} already stopped.",
390 )
391
392 def restart_osd(self, node, osdid):
393 self.check_node(node)
394 if not self.check_osd(node, osdid):
395 self.module.fail_json(msg=f"Osd {osdid} does not exist.")
396 if not self.module.check_mode:
397 self.proxmox_api.nodes(node).ceph.restart.create(service=f"osd.{osdid}")
398 msg = f"Restart Osd {osdid}."
399 else:
400 msg = f"Would restart Osd {osdid}."
401
402 self.module.exit_json(changed=True, msg=msg)
403
404
405 def get_present_optional_args(args):
406 args_list = [
407 "db_dev",
408 "db_dev_size",
409 "wal_dev",
410 "wal_dev_size",
411 ]
412 o_args = {k: args[k] for k in args_list if args[k] is not None}
413 if args["osds_per_device"] is not None:
414 o_args["osds-per-device"] = args["osds_per_device"]
415 if args["crush_device_class"] is not None:
416 o_args["crush-device-class"] = args["crush_device_class"]
417 return o_args
418
419
420 def main():
421 module = create_proxmox_module(module_args(), **module_options())
422 proxmox = ProxmoxCephOsdAnsible(module)
423
424 state = module.params["state"]
425
426 if state == "present":
427 optional_args = get_present_optional_args(module.params)
428 proxmox.add_osd(
429 module.params["node"],
430 module.params["dev"],
431 ansible_to_proxmox_bool(module.params["encrypted"]),
432 optional_args,
433 )
434
435 elif state == "absent":
436 proxmox.del_osd(
437 module.params["node"],
438 module.params["osdid"],
439 ansible_to_proxmox_bool(module.params["cleanup"]),
440 )
441
442 elif state == "in":
443 proxmox.in_osd(
444 module.params["node"],
445 module.params["osdid"],
446 )
447
448 elif state == "out":
449 proxmox.out_osd(
450 module.params["node"],
451 module.params["osdid"],
452 )
453
454 elif state == "scrub":
455 proxmox.scrub_osd(
456 module.params["node"],
457 module.params["osdid"],
458 ansible_to_proxmox_bool(module.params["deep"]),
459 )
460
461 elif state == "start":
462 proxmox.start_osd(
463 module.params["node"],
464 module.params["osdid"],
465 )
466
467 elif state == "stop":
468 proxmox.stop_osd(
469 module.params["node"],
470 module.params["osdid"],
471 )
472
473 elif state == "restart":
474 proxmox.restart_osd(
475 module.params["node"],
476 module.params["osdid"],
477 )
478
479
480 if __name__ == "__main__":
481 main()