| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2023, Sergei Antipov (UnderGreen) <[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 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: proxmox_pool |
| 10 |
short_description: Pool management for Proxmox VE cluster |
| 11 |
description: |
| 12 |
- Create or delete a pool for Proxmox VE clusters. |
| 13 |
- For pool members management please consult M(community.proxmox.proxmox_pool_member) module. |
| 14 |
author: "Sergei Antipov (@UnderGreen) <[email protected]>" |
| 15 |
attributes: |
| 16 |
check_mode: |
| 17 |
support: full |
| 18 |
diff_mode: |
| 19 |
support: none |
| 20 |
options: |
| 21 |
poolid: |
| 22 |
description: |
| 23 |
- The pool ID. |
| 24 |
type: str |
| 25 |
aliases: ["name"] |
| 26 |
required: true |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Indicate desired state of the pool. |
| 30 |
- The pool must be empty prior deleting it with O(state=absent). |
| 31 |
choices: ['present', 'absent'] |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
comment: |
| 35 |
description: |
| 36 |
- Specify the description for the pool. |
| 37 |
- Parameter is ignored when pool already exists or O(state=absent). |
| 38 |
type: str |
| 39 |
|
| 40 |
extends_documentation_fragment: |
| 41 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 42 |
- community.proxmox.proxmox.documentation |
| 43 |
- community.proxmox.attributes |
| 44 |
""" |
| 45 |
|
| 46 |
EXAMPLES = r""" |
| 47 |
- name: Create new Proxmox VE pool |
| 48 |
community.proxmox.proxmox_pool: |
| 49 |
poolid: test |
| 50 |
comment: 'New pool' |
| 51 |
|
| 52 |
- name: Delete the Proxmox VE pool |
| 53 |
community.proxmox.proxmox_pool: |
| 54 |
poolid: test |
| 55 |
state: absent |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = r""" |
| 59 |
poolid: |
| 60 |
description: The pool ID. |
| 61 |
returned: success |
| 62 |
type: str |
| 63 |
sample: test |
| 64 |
msg: |
| 65 |
description: A short message on what the module did. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
sample: "Pool test successfully created" |
| 69 |
""" |
| 70 |
|
| 71 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 72 |
ProxmoxAnsible, |
| 73 |
create_proxmox_module, |
| 74 |
is_not_found_error, |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def module_args(): |
| 79 |
return dict( |
| 80 |
poolid=dict(type="str", aliases=["name"], required=True), |
| 81 |
comment=dict(type="str"), |
| 82 |
state=dict(default="present", choices=["present", "absent"]), |
| 83 |
) |
| 84 |
|
| 85 |
|
| 86 |
def module_options(): |
| 87 |
return {} |
| 88 |
|
| 89 |
|
| 90 |
class ProxmoxPoolAnsible(ProxmoxAnsible): |
| 91 |
def is_pool_existing(self, poolid): |
| 92 |
"""Check whether pool already exist |
| 93 |
|
| 94 |
:param poolid: str - name of the pool |
| 95 |
:return: bool - is pool exists? |
| 96 |
""" |
| 97 |
try: |
| 98 |
self.proxmox_api.pools.get(poolid=poolid) |
| 99 |
return True |
| 100 |
except Exception as e: |
| 101 |
if is_not_found_error(e): |
| 102 |
return False |
| 103 |
self.module.fail_json(msg=f"Unable to retrieve pool {poolid}: {e}") |
| 104 |
|
| 105 |
def is_pool_empty(self, poolid): |
| 106 |
"""Check whether pool has members |
| 107 |
|
| 108 |
:param poolid: str - name of the pool |
| 109 |
:return: bool - is pool empty? |
| 110 |
""" |
| 111 |
return bool(not (self.get_pool(poolid)["members"])) |
| 112 |
|
| 113 |
def create_pool(self, poolid, comment=None): |
| 114 |
"""Create Proxmox VE pool |
| 115 |
|
| 116 |
:param poolid: str - name of the pool |
| 117 |
:param comment: str, optional - Description of a pool |
| 118 |
:return: None |
| 119 |
""" |
| 120 |
if self.is_pool_existing(poolid): |
| 121 |
self.module.exit_json(changed=False, poolid=poolid, msg=f"Pool {poolid} already exists") |
| 122 |
|
| 123 |
if self.module.check_mode: |
| 124 |
return |
| 125 |
|
| 126 |
try: |
| 127 |
self.proxmox_api.pools.post(poolid=poolid, comment=comment) |
| 128 |
except Exception as e: |
| 129 |
self.module.fail_json(msg=f"Failed to create pool with ID {poolid}: {e}") |
| 130 |
|
| 131 |
def delete_pool(self, poolid): |
| 132 |
"""Delete Proxmox VE pool |
| 133 |
|
| 134 |
:param poolid: str - name of the pool |
| 135 |
:return: None |
| 136 |
""" |
| 137 |
if not self.is_pool_existing(poolid): |
| 138 |
self.module.exit_json(changed=False, poolid=poolid, msg=f"Pool {poolid} doesn't exist") |
| 139 |
|
| 140 |
if self.is_pool_empty(poolid): |
| 141 |
if self.module.check_mode: |
| 142 |
return |
| 143 |
|
| 144 |
try: |
| 145 |
self.proxmox_api.pools.delete(poolid=poolid) |
| 146 |
except Exception as e: |
| 147 |
self.module.fail_json(msg=f"Failed to delete pool with ID {poolid}: {e}") |
| 148 |
else: |
| 149 |
self.module.fail_json( |
| 150 |
msg=f"Can't delete pool {poolid} with members. Please remove members from pool first." |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
def main(): |
| 155 |
module = create_proxmox_module(module_args(), **module_options()) |
| 156 |
proxmox = ProxmoxPoolAnsible(module) |
| 157 |
|
| 158 |
poolid = module.params["poolid"] |
| 159 |
comment = module.params["comment"] |
| 160 |
state = module.params["state"] |
| 161 |
|
| 162 |
if state == "present": |
| 163 |
proxmox.create_pool(poolid, comment) |
| 164 |
module.exit_json(changed=True, poolid=poolid, msg=f"Pool {poolid} successfully created") |
| 165 |
else: |
| 166 |
proxmox.delete_pool(poolid) |
| 167 |
module.exit_json(changed=True, poolid=poolid, msg=f"Pool {poolid} successfully deleted") |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__": |
| 171 |
main() |