| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2025, Markus Kötter <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-FileCopyrightText: (c) 2025, Markus Kötter <[email protected]> |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
module: proxmox_cluster_ha_groups |
| 12 |
|
| 13 |
short_description: Management of HA groups in Proxmox VE Cluster |
| 14 |
|
| 15 |
version_added: "1.1.0" |
| 16 |
|
| 17 |
description: |
| 18 |
- Configure HA groups via C(/cluster/ha/groups). |
| 19 |
|
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: none |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
|
| 26 |
options: |
| 27 |
state: |
| 28 |
description: Whether the HA groups should be there (created if missing) or not (deleted if they exist). |
| 29 |
required: true |
| 30 |
choices: ['present', 'absent'] |
| 31 |
type: str |
| 32 |
name: |
| 33 |
description: group name |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
comment: |
| 37 |
description: Description |
| 38 |
required: false |
| 39 |
type: str |
| 40 |
nodes: |
| 41 |
description: | |
| 42 |
List of cluster node members, where a priority can be given to each node. A resource bound to a group will run on the available nodes with the |
| 43 |
highest priority. If there are more nodes in the highest priority class, the services will get distributed to those nodes. The priorities have a |
| 44 |
relative meaning only. The higher the number, the higher the priority. |
| 45 |
It can either be a string C(node_name:priority,node_name:priority) or an actual list of strings. |
| 46 |
required: false |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
nofailback: |
| 50 |
description: | |
| 51 |
The CRM tries to run services on the node with the highest priority. If a node with higher priority comes online, the CRM migrates the service to |
| 52 |
that node. Setting O(nofailback=true) prevents that behavior. |
| 53 |
required: false |
| 54 |
type: bool |
| 55 |
default: false |
| 56 |
restricted: |
| 57 |
description: | |
| 58 |
Resources bound to restricted groups may only run on nodes defined by the group. The resource will be placed in the stopped state if no group node |
| 59 |
member is online. Resources on unrestricted groups may run on any cluster node if all group members are offline, but they will migrate back as |
| 60 |
soon as a group member comes online. One can implement a 'preferred node' behavior using an unrestricted group with only one member. |
| 61 |
required: False |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
extends_documentation_fragment: |
| 65 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 66 |
- community.proxmox.proxmox.documentation |
| 67 |
- community.proxmox.attributes |
| 68 |
author: |
| 69 |
- Markus Kötter (@commonism) |
| 70 |
""" |
| 71 |
|
| 72 |
EXAMPLES = r""" |
| 73 |
- name: Create HA group |
| 74 |
community.proxmox.proxmox_cluster_ha_groups: |
| 75 |
state: "present" |
| 76 |
name: ha0 |
| 77 |
comment: yes |
| 78 |
nodes: node0:0,node1:1 |
| 79 |
nofailback: true |
| 80 |
restricted: false |
| 81 |
|
| 82 |
- name: Delete HA group |
| 83 |
community.proxmox.proxmox_cluster_ha_groups: |
| 84 |
state: "absent" |
| 85 |
name: ha0 |
| 86 |
""" |
| 87 |
|
| 88 |
RETURN = r"""#""" |
| 89 |
|
| 90 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 91 |
ProxmoxAnsible, |
| 92 |
create_proxmox_module, |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def module_args(): |
| 97 |
return dict( |
| 98 |
state=dict(choices=["present", "absent"], required=True), |
| 99 |
name=dict(type="str", required=True), |
| 100 |
comment=dict(type="str", required=False), |
| 101 |
nodes=dict(type="list", elements="str", required=False), |
| 102 |
nofailback=dict(type="bool", default=False), |
| 103 |
restricted=dict(type="bool", default=False), |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def module_options(): |
| 108 |
return dict(supports_check_mode=False) |
| 109 |
|
| 110 |
|
| 111 |
class ProxmoxClusterHAGroupsAnsible(ProxmoxAnsible): |
| 112 |
def _get(self): |
| 113 |
groups = self.proxmox_api.cluster.ha.groups.get() |
| 114 |
return groups |
| 115 |
|
| 116 |
def _post(self, **data): |
| 117 |
return self.proxmox_api.cluster.ha.groups.post(**data) |
| 118 |
|
| 119 |
def _put(self, name, data): |
| 120 |
return self.proxmox_api.cluster.ha.groups(name).put(**data) |
| 121 |
|
| 122 |
def _delete(self, name): |
| 123 |
return self.proxmox_api.cluster.ha.groups(name).delete() |
| 124 |
|
| 125 |
def create(self, groups, name, comment, nodes, nofailback, restricted): # noqa: PLR0913 |
| 126 |
data = { |
| 127 |
"comment": comment, |
| 128 |
"nodes": ",".join(nodes), |
| 129 |
"nofailback": int(nofailback), |
| 130 |
"restricted": int(restricted), |
| 131 |
} |
| 132 |
|
| 133 |
for group in groups: |
| 134 |
if group["group"] != name: |
| 135 |
continue |
| 136 |
|
| 137 |
group["nodes"] = sorted(group.get("nodes", "").split(",")) |
| 138 |
|
| 139 |
if ( |
| 140 |
group.get("comment", ""), |
| 141 |
group.get("nodes", ""), |
| 142 |
bool(group.get("nofailback", 0)), |
| 143 |
bool(group.get("restricted", 0)), |
| 144 |
) == (comment, nodes, nofailback, restricted): |
| 145 |
return False |
| 146 |
else: |
| 147 |
self._put(name, data) |
| 148 |
return True |
| 149 |
|
| 150 |
self._post(group=name, **data) |
| 151 |
return True |
| 152 |
|
| 153 |
def delete(self, groups, name): |
| 154 |
for group in groups: |
| 155 |
if group["group"] != name: |
| 156 |
continue |
| 157 |
self._delete(name) |
| 158 |
return True |
| 159 |
|
| 160 |
return False |
| 161 |
|
| 162 |
|
| 163 |
def run_module(): |
| 164 |
module = create_proxmox_module(module_args(), **module_options()) |
| 165 |
proxmox = ProxmoxClusterHAGroupsAnsible(module) |
| 166 |
|
| 167 |
result = dict( |
| 168 |
changed=False, |
| 169 |
) |
| 170 |
|
| 171 |
name = module.params["name"] |
| 172 |
comment = module.params["comment"] |
| 173 |
nodes = sorted(module.params["nodes"]) |
| 174 |
nofailback = module.params["nofailback"] |
| 175 |
restricted = module.params["restricted"] |
| 176 |
try: |
| 177 |
groups = proxmox._get() |
| 178 |
|
| 179 |
if module.params["state"] == "present": |
| 180 |
changed = proxmox.create(groups, name, comment, nodes, nofailback, restricted) |
| 181 |
else: |
| 182 |
changed = proxmox.delete(groups, name) |
| 183 |
|
| 184 |
result["changed"] = changed |
| 185 |
except Exception as e: |
| 186 |
module.fail_json(msg=str(e), **result) |
| 187 |
|
| 188 |
module.exit_json(**result) |
| 189 |
|
| 190 |
|
| 191 |
def main(): |
| 192 |
run_module() |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
main() |