| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2026, (@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_pool |
| 10 |
version_added: 2.0.0 |
| 11 |
short_description: Manage Ceph Pool. |
| 12 |
description: |
| 13 |
- Add, edit or delete pool 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 |
add_storages: |
| 23 |
description: Add the new pool to the cluster storage configuration. |
| 24 |
required: false |
| 25 |
type: bool |
| 26 |
crush_rule: |
| 27 |
description: The rule to use for mapping object placement in the cluster. |
| 28 |
required: false |
| 29 |
type: str |
| 30 |
min_size: |
| 31 |
description: Minimum number of replicas per object. |
| 32 |
required: false |
| 33 |
type: int |
| 34 |
name: |
| 35 |
description: The name of the pool. It must be unique. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
node: |
| 39 |
description: The cluster node name. |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
pg_autoscale_mode: |
| 43 |
description: The automatic placement groups scaling mode of the pool. |
| 44 |
required: false |
| 45 |
type: str |
| 46 |
choices: ["on", "off", "warn"] |
| 47 |
pg_num: |
| 48 |
description: Number of placement groups. |
| 49 |
required: false |
| 50 |
type: int |
| 51 |
pg_num_min: |
| 52 |
description: Minimal number of placement groups. |
| 53 |
required: false |
| 54 |
type: int |
| 55 |
state: |
| 56 |
description: Indicate whether the Ceph pool should be present (created if missing) or absent (deleted if it exists). |
| 57 |
required: true |
| 58 |
choices: ['present', 'absent'] |
| 59 |
type: str |
| 60 |
size: |
| 61 |
description: Number of replicas per object. |
| 62 |
required: false |
| 63 |
type: int |
| 64 |
timeout: |
| 65 |
description: Timeout for operations. |
| 66 |
default: 5 |
| 67 |
required: false |
| 68 |
type: int |
| 69 |
target_size: |
| 70 |
description: The estimated target size of the pool for the placement groups autoscaler. |
| 71 |
required: false |
| 72 |
type: str |
| 73 |
target_size_ratio: |
| 74 |
description: The estimated target ratio of the pool for the placement groups autoscaler. |
| 75 |
required: false |
| 76 |
type: int |
| 77 |
|
| 78 |
extends_documentation_fragment: |
| 79 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 80 |
- community.proxmox.proxmox.documentation |
| 81 |
- community.proxmox.attributes |
| 82 |
- community.proxmox.attributes.info_module |
| 83 |
""" |
| 84 |
|
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
- name: Add a ceph pool |
| 88 |
community.proxmox.proxmox_ceph_pool: |
| 89 |
api_host: proxmox |
| 90 |
api_user: root@pam |
| 91 |
api_password: secret |
| 92 |
node: proxmox |
| 93 |
name: ceph-pool |
| 94 |
state: present |
| 95 |
|
| 96 |
- name: Add a ceph pool and storage |
| 97 |
community.proxmox.proxmox_ceph_pool: |
| 98 |
api_host: proxmox |
| 99 |
api_user: root@pam |
| 100 |
api_password: secret |
| 101 |
node: proxmox |
| 102 |
name: ceph-pool |
| 103 |
state: present |
| 104 |
add_storages: true |
| 105 |
|
| 106 |
- name: Delete a ceph pool |
| 107 |
community.proxmox.proxmox_ceph_pool: |
| 108 |
api_host: proxmox |
| 109 |
api_user: root@pam |
| 110 |
api_password: secret |
| 111 |
node: proxmox |
| 112 |
name: ceph-pool |
| 113 |
state: absent |
| 114 |
""" |
| 115 |
|
| 116 |
RETURN = r""" |
| 117 |
msg: |
| 118 |
description: The output message that the module generates. |
| 119 |
type: str |
| 120 |
returned: always |
| 121 |
""" |
| 122 |
|
| 123 |
from ansible.module_utils.common.text.converters import to_native |
| 124 |
|
| 125 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 126 |
ProxmoxAnsible, |
| 127 |
ansible_to_proxmox_bool, |
| 128 |
create_proxmox_module, |
| 129 |
) |
| 130 |
|
| 131 |
|
| 132 |
def module_args(): |
| 133 |
return dict( |
| 134 |
add_storages=dict(type="bool"), |
| 135 |
crush_rule=dict(type="str"), |
| 136 |
min_size=dict(type="int"), |
| 137 |
name=dict(type="str", required=True), |
| 138 |
node=dict(type="str", required=True), |
| 139 |
pg_autoscale_mode=dict(type="str", choices=["on", "off", "warn"]), |
| 140 |
pg_num=dict(type="int"), |
| 141 |
pg_num_min=dict(type="int"), |
| 142 |
size=dict(type="int"), |
| 143 |
timeout=dict(type="int", default=5), |
| 144 |
target_size=dict(type="str"), |
| 145 |
target_size_ratio=dict(type="int"), |
| 146 |
state=dict(choices=["present", "absent"], required=True), |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
class ProxmoxCephPoolAnsible(ProxmoxAnsible): |
| 151 |
def __init__(self, module): |
| 152 |
super().__init__(module) |
| 153 |
self.params = module.params |
| 154 |
|
| 155 |
def get_params(self): |
| 156 |
params_list = [ |
| 157 |
"add_storages", |
| 158 |
"crush_rule", |
| 159 |
"node", |
| 160 |
"name", |
| 161 |
"min_size", |
| 162 |
"pg_autoscale_mode", |
| 163 |
"pg_num", |
| 164 |
"pg_num_min", |
| 165 |
"size", |
| 166 |
"target_size", |
| 167 |
"target_size_ratio", |
| 168 |
] |
| 169 |
params = { |
| 170 |
k: ansible_to_proxmox_bool(v) if isinstance(v, bool) else v |
| 171 |
for k, v in self.params.items() |
| 172 |
if v is not None and k in params_list |
| 173 |
} |
| 174 |
return params |
| 175 |
|
| 176 |
def is_equal(self, params, current): |
| 177 |
return all(params[k] == current.get(k) for k in params if k not in ("add_storages", "node")) |
| 178 |
|
| 179 |
def check_pool(self, node, name): |
| 180 |
pools = self.proxmox_api.nodes(node).ceph.pool.get() |
| 181 |
return any(pool["pool_name"] == name for pool in pools) |
| 182 |
|
| 183 |
def get_pool(self, node, name): |
| 184 |
return self.proxmox_api.nodes(node).ceph.pool(name).status.get() |
| 185 |
|
| 186 |
def add_pool(self): |
| 187 |
node = self.params["node"] |
| 188 |
name = self.params["name"] |
| 189 |
self.get_node(node, strict=True) |
| 190 |
pool_params = self.get_params() |
| 191 |
if self.check_pool(node, name): |
| 192 |
pool_current = self.get_pool(node, name) |
| 193 |
if self.is_equal(pool_params, pool_current): |
| 194 |
self.module.exit_json(changed=False, msg=f"Ceph pool {name} already exists.") |
| 195 |
else: |
| 196 |
# Remove add_storage : not in put parameters |
| 197 |
pool_params.pop("add_storages", None) |
| 198 |
if self.module.check_mode: |
| 199 |
self.module.exit_json(changed=True, msg=f"Ceph pool {name} would be updated.") |
| 200 |
else: |
| 201 |
current_task_id = self.proxmox_api.nodes(node).ceph.pool(name).put(**pool_params) |
| 202 |
task_success, fail_reason = self.api_task_complete( |
| 203 |
node, current_task_id, self.module.params["timeout"] |
| 204 |
) |
| 205 |
if task_success: |
| 206 |
self.module.exit_json(changed=True, msg=f"Ceph pool {name} updated.") |
| 207 |
else: |
| 208 |
self.module.fail_json(msg=f"Error occurred on task execution: {fail_reason}") |
| 209 |
elif self.module.check_mode: |
| 210 |
self.module.exit_json(changed=True, msg=f"Ceph pool {name} would be added.") |
| 211 |
else: |
| 212 |
current_task_id = self.proxmox_api.nodes(node).ceph.pool.create(**pool_params) |
| 213 |
task_success, fail_reason = self.api_task_complete(node, current_task_id, self.module.params["timeout"]) |
| 214 |
if task_success: |
| 215 |
self.module.exit_json(changed=True, msg=f"Ceph pool {name} added.") |
| 216 |
else: |
| 217 |
self.module.fail_json(msg=f"Error occurred on task execution: {fail_reason}") |
| 218 |
|
| 219 |
def del_pool(self): |
| 220 |
node = self.params["node"] |
| 221 |
name = self.params["name"] |
| 222 |
self.get_node(node) |
| 223 |
if self.check_pool(node, name): |
| 224 |
if self.module.check_mode: |
| 225 |
self.module.exit_json(changed=True, msg=f"Ceph pool {name} would be deleted.") |
| 226 |
else: |
| 227 |
current_task_id = self.proxmox_api.nodes(node).ceph.pool(name).delete() |
| 228 |
task_success, fail_reason = self.api_task_complete(node, current_task_id, self.module.params["timeout"]) |
| 229 |
if task_success: |
| 230 |
self.module.exit_json(changed=True, msg=f"Ceph pool {name} deleted.") |
| 231 |
else: |
| 232 |
self.module.fail_json(msg=f"Error occurred on task execution: {fail_reason}") |
| 233 |
else: |
| 234 |
self.module.exit_json(changed=False, msg=f"Ceph pool {name} not present.") |
| 235 |
|
| 236 |
|
| 237 |
def main(): |
| 238 |
module = create_proxmox_module(module_args()) |
| 239 |
proxmox = ProxmoxCephPoolAnsible(module) |
| 240 |
state = module.params["state"] |
| 241 |
|
| 242 |
if state == "present": |
| 243 |
try: |
| 244 |
proxmox.add_pool() |
| 245 |
except Exception as e: |
| 246 |
module.fail_json(msg=f"Adding ceph pool failed with exception: {to_native(e)}") |
| 247 |
|
| 248 |
elif state == "absent": |
| 249 |
try: |
| 250 |
proxmox.del_pool() |
| 251 |
except Exception as e: |
| 252 |
module.fail_json(msg=f"Deleting ceph pool failed with exception: {to_native(e)}") |
| 253 |
|
| 254 |
|
| 255 |
if __name__ == "__main__": |
| 256 |
main() |