Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_ceph_mon.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_mon
10 version_added: 1.5.0
11 short_description: Add or delete Ceph Monitor.
12 description:
13 - Add or delete monitors 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 monitor 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 monitor
42 community.proxmox.proxmox_ceph_mon:
43 node: proxmox-02
44 state: present
45
46 - name: Delete a ceph monitor
47 community.proxmox.proxmox_ceph_mon:
48 node: proxmox-02
49 state: absent
50 """
51
52 RETURN = r"""
53 monitor:
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 ProxmoxCephMonAnsible(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_monitors(self, node):
90 monitors = self.proxmox_api.nodes(node).ceph.mon.get()
91 return any(mon["name"] == node for mon in monitors)
92
93 def add_mon(self, monitor):
94 self.check_node(monitor)
95 if self.check_monitors(monitor):
96 self.module.exit_json(changed=False, msg="Monitor already exists", monitor=monitor)
97 else:
98 if not self.module.check_mode:
99 self.proxmox_api.nodes(monitor).ceph.mon(monitor).create()
100 msg = f"Monitor {monitor} added"
101 else:
102 msg = f"Monitor {monitor} would be added"
103
104 self.module.exit_json(changed=True, msg=msg, monitor=monitor)
105
106 def del_mon(self, monitor):
107 self.check_node(monitor)
108 if self.check_monitors(monitor):
109 if not self.module.check_mode:
110 self.proxmox_api.nodes(monitor).ceph.mon(monitor).delete()
111 msg = f"Monitor {monitor} deleted"
112 else:
113 msg = f"Monitor {monitor} would be deleted"
114
115 self.module.exit_json(changed=True, msg=msg, monitor=monitor)
116 else:
117 self.module.exit_json(changed=False, msg="Monitor not present", monitor=monitor)
118
119
120 def main():
121 module = create_proxmox_module(module_args(), **module_options())
122 proxmox = ProxmoxCephMonAnsible(module)
123
124 state = module.params["state"]
125
126 if state == "present":
127 try:
128 proxmox.add_mon(module.params["node"])
129 except Exception as e:
130 module.fail_json(msg=f"Adding monitor failed with exception: {to_native(e)}")
131
132 elif state == "absent":
133 try:
134 proxmox.del_mon(module.params["node"])
135 except Exception as e:
136 module.fail_json(msg=f"Deleting monitor failed with exception: {to_native(e)}")
137
138
139 if __name__ == "__main__":
140 main()