| 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_mgr |
| 10 |
version_added: 1.5.0 |
| 11 |
short_description: Add or delete Ceph Manager. |
| 12 |
description: |
| 13 |
- Add or delete managers of a ceph cluster. |
| 14 |
author: Vial Nicolas (@teslamania) |
| 15 |
attributes: |
| 16 |
check_mode: |
| 17 |
support: full |
| 18 |
diff_mode: |
| 19 |
support: none |
| 20 |
|
| 21 |
options: |
| 22 |
state: |
| 23 |
description: Indicate whether the Ceph manager should be present (created if missing) or absent (deleted if it exists). |
| 24 |
required: true |
| 25 |
choices: ['present', 'absent'] |
| 26 |
type: str |
| 27 |
node: |
| 28 |
description: The cluster node name. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
|
| 32 |
extends_documentation_fragment: |
| 33 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 34 |
- community.proxmox.proxmox.documentation |
| 35 |
- community.proxmox.attributes |
| 36 |
- community.proxmox.attributes.info_module |
| 37 |
""" |
| 38 |
|
| 39 |
|
| 40 |
EXAMPLES = r""" |
| 41 |
- name: Add a ceph manager |
| 42 |
community.proxmox.proxmox_ceph_mgr: |
| 43 |
node: proxmox-02 |
| 44 |
state: present |
| 45 |
|
| 46 |
- name: Delete a ceph manager |
| 47 |
community.proxmox.proxmox_ceph_mgr: |
| 48 |
node: proxmox-02 |
| 49 |
state: absent |
| 50 |
""" |
| 51 |
|
| 52 |
RETURN = r""" |
| 53 |
manager: |
| 54 |
description: The node name. |
| 55 |
type: str |
| 56 |
returned: always |
| 57 |
msg: |
| 58 |
description: The output message that the module generates. |
| 59 |
type: str |
| 60 |
returned: always |
| 61 |
""" |
| 62 |
|
| 63 |
from ansible.module_utils.common.text.converters import to_native |
| 64 |
|
| 65 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 66 |
ProxmoxAnsible, |
| 67 |
create_proxmox_module, |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def module_args(): |
| 72 |
return dict( |
| 73 |
node=dict(type="str", required=True), |
| 74 |
state=dict(choices=["present", "absent"], required=True), |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def module_options(): |
| 79 |
return {} |
| 80 |
|
| 81 |
|
| 82 |
class ProxmoxCephMgrAnsible(ProxmoxAnsible): |
| 83 |
def check_node(self, node): |
| 84 |
nodes = self.proxmox_api.cluster.resources.get(type="node") |
| 85 |
nodes = [item["node"] for item in nodes] |
| 86 |
if node not in nodes: |
| 87 |
self.module.fail_json(msg=f"Node {node} does not exist in the cluster") |
| 88 |
|
| 89 |
def check_managers(self, node): |
| 90 |
managers = self.proxmox_api.nodes(node).ceph.mgr.get() |
| 91 |
return any(mgr["name"] == node for mgr in managers) |
| 92 |
|
| 93 |
def add_mgr(self, manager): |
| 94 |
self.check_node(manager) |
| 95 |
if self.check_managers(manager): |
| 96 |
self.module.exit_json(changed=False, msg="Manager already exists", manager=manager) |
| 97 |
else: |
| 98 |
if not self.module.check_mode: |
| 99 |
self.proxmox_api.nodes(manager).ceph.mgr(manager).create() |
| 100 |
msg = f"Manager {manager} added" |
| 101 |
else: |
| 102 |
msg = f"Manager {manager} would be added" |
| 103 |
|
| 104 |
self.module.exit_json(changed=True, msg=msg, manager=manager) |
| 105 |
|
| 106 |
def del_mgr(self, manager): |
| 107 |
self.check_node(manager) |
| 108 |
if self.check_managers(manager): |
| 109 |
if not self.module.check_mode: |
| 110 |
self.proxmox_api.nodes(manager).ceph.mgr(manager).delete() |
| 111 |
msg = f"Manager {manager} deleted" |
| 112 |
else: |
| 113 |
msg = f"Manager {manager} would be deleted" |
| 114 |
|
| 115 |
self.module.exit_json(changed=True, msg=msg, manager=manager) |
| 116 |
else: |
| 117 |
self.module.exit_json(changed=False, msg="Manager not present", manager=manager) |
| 118 |
|
| 119 |
|
| 120 |
def main(): |
| 121 |
module = create_proxmox_module(module_args(), **module_options()) |
| 122 |
proxmox = ProxmoxCephMgrAnsible(module) |
| 123 |
|
| 124 |
state = module.params["state"] |
| 125 |
|
| 126 |
if state == "present": |
| 127 |
try: |
| 128 |
proxmox.add_mgr(module.params["node"]) |
| 129 |
except Exception as e: |
| 130 |
module.fail_json(msg=f"Adding manager failed with exception: {to_native(e)}") |
| 131 |
|
| 132 |
elif state == "absent": |
| 133 |
try: |
| 134 |
proxmox.del_mgr(module.params["node"]) |
| 135 |
except Exception as e: |
| 136 |
module.fail_json(msg=f"Deleting manager failed with exception: {to_native(e)}") |
| 137 |
|
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
main() |