| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2025, Jeffrey van Pelt (@Thulium-Drake) <[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-FileCopyrightText: (c) 2025, Jeffrey van Pelt (Thulium-Drake) <[email protected]> |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: proxmox_group |
| 11 |
short_description: Group management for Proxmox VE cluster |
| 12 |
description: |
| 13 |
- Create or delete a user group for Proxmox VE clusters. |
| 14 |
author: "Jeffrey van Pelt (@Thulium-Drake) <[email protected]>" |
| 15 |
version_added: "1.2.0" |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
groupid: |
| 23 |
description: |
| 24 |
- The group name. |
| 25 |
type: str |
| 26 |
aliases: ["name"] |
| 27 |
required: true |
| 28 |
state: |
| 29 |
description: |
| 30 |
- Indicate desired state of the group. |
| 31 |
choices: ['present', 'absent'] |
| 32 |
default: present |
| 33 |
type: str |
| 34 |
comment: |
| 35 |
description: |
| 36 |
- Specify the description for the group. |
| 37 |
- Parameter is ignored when group 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 user group |
| 48 |
community.proxmox.proxmox_group: |
| 49 |
name: administrators |
| 50 |
comment: IT Admins |
| 51 |
|
| 52 |
- name: Delete a Proxmox VE user group |
| 53 |
community.proxmox.proxmox_group: |
| 54 |
name: administrators |
| 55 |
state: absent |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = r""" |
| 59 |
groupid: |
| 60 |
description: The group name. |
| 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: "Group administrators successfully created" |
| 69 |
""" |
| 70 |
|
| 71 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 72 |
ProxmoxAnsible, |
| 73 |
create_proxmox_module, |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
def module_args(): |
| 78 |
return dict( |
| 79 |
groupid=dict(type="str", aliases=["name"], required=True), |
| 80 |
comment=dict(type="str"), |
| 81 |
state=dict(default="present", choices=["present", "absent"]), |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
def module_options(): |
| 86 |
return {} |
| 87 |
|
| 88 |
|
| 89 |
class ProxmoxGroupAnsible(ProxmoxAnsible): |
| 90 |
def is_group_existing(self, groupid): |
| 91 |
"""Check whether group already exist |
| 92 |
|
| 93 |
:param groupid: str - name of the group |
| 94 |
:return: bool - is group exists? |
| 95 |
""" |
| 96 |
try: |
| 97 |
groups = self.proxmox_api.access.groups.get() |
| 98 |
return any(group["groupid"] == groupid for group in groups) |
| 99 |
except Exception as e: |
| 100 |
self.module.fail_json(msg=f"Unable to retrieve groups: {e}") |
| 101 |
|
| 102 |
def create_group(self, groupid, comment=None): |
| 103 |
"""Create Proxmox VE group |
| 104 |
|
| 105 |
:param groupid: str - name of the group |
| 106 |
:param comment: str, optional - Description of a group |
| 107 |
:return: None |
| 108 |
""" |
| 109 |
if self.is_group_existing(groupid): |
| 110 |
self.module.exit_json(changed=False, groupid=groupid, msg=f"Group {groupid} already exists") |
| 111 |
|
| 112 |
if self.module.check_mode: |
| 113 |
return |
| 114 |
|
| 115 |
try: |
| 116 |
self.proxmox_api.access.groups.post(groupid=groupid, comment=comment) |
| 117 |
except Exception as e: |
| 118 |
self.module.fail_json(msg=f"Failed to create group with ID {groupid}: {e}") |
| 119 |
|
| 120 |
def delete_group(self, groupid): |
| 121 |
"""Delete Proxmox VE group |
| 122 |
|
| 123 |
:param groupid: str - name of the group |
| 124 |
:return: None |
| 125 |
""" |
| 126 |
if not self.is_group_existing(groupid): |
| 127 |
self.module.exit_json(changed=False, groupid=groupid, msg=f"Group {groupid} doesn't exist") |
| 128 |
|
| 129 |
if self.module.check_mode: |
| 130 |
return |
| 131 |
|
| 132 |
try: |
| 133 |
self.proxmox_api.access.groups(groupid).delete() |
| 134 |
except Exception as e: |
| 135 |
self.module.fail_json(msg=f"Failed to delete group with ID {groupid}: {e}") |
| 136 |
|
| 137 |
|
| 138 |
def main(): |
| 139 |
module = create_proxmox_module(module_args(), **module_options()) |
| 140 |
proxmox = ProxmoxGroupAnsible(module) |
| 141 |
|
| 142 |
groupid = module.params["groupid"] |
| 143 |
comment = module.params["comment"] |
| 144 |
state = module.params["state"] |
| 145 |
|
| 146 |
if state == "present": |
| 147 |
proxmox.create_group(groupid, comment) |
| 148 |
module.exit_json(changed=True, groupid=groupid, msg=f"Group {groupid} successfully created") |
| 149 |
else: |
| 150 |
proxmox.delete_group(groupid) |
| 151 |
module.exit_json(changed=True, groupid=groupid, msg=f"Group {groupid} successfully deleted") |
| 152 |
|
| 153 |
|
| 154 |
if __name__ == "__main__": |
| 155 |
main() |