Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_cluster_ha_resources.py

raw
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_resources
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: create or delete
29 required: true
30 choices: ['present', 'absent']
31 type: str
32 name:
33 description: |
34 HA resource ID. This consists of a resource type followed by a resource specific name, separated with colon (example: V(vm:100) / V(ct:100) ).
35 For virtual machines and containers, you can simply use the VM or CT id as a shortcut (example: 100).
36 The API documentation refers to this field as \"sid\".
37 required: true
38 type: str
39 comment:
40 description: Description
41 required: false
42 default: ""
43 type: str
44 group:
45 description: The HA group identifier.
46 required: false
47 type: str
48 max_relocate:
49 description: Maximal number of service relocate tries when a service failes to start.
50 required: false
51 type: int
52 default: 1
53 max_restart:
54 description: Maximal number of tries to restart the service on a node after its start failed.
55 required: false
56 type: int
57 default: 1
58 hastate:
59 description: Requested resource state. The CRM reads this state and acts accordingly. Please note that V(enabled) is just an alias for V(started).
60 type: str
61 choices: ["started", "stopped", "disabled", "ignored"]
62 default: started
63
64 extends_documentation_fragment:
65 - community.proxmox.proxmox.actiongroup_proxmox
66 - community.proxmox.proxmox.documentation
67 - community.proxmox.attributes
68
69 author:
70 - Markus Kötter (@commonism)
71 """
72
73 EXAMPLES = r"""
74 - name: Add VM to HA group
75 community.proxmox.proxmox_cluster_ha_resources:
76 name: vm:100
77 state: "present"
78 group: ha0
79 max_relocate: 2
80 max_restart: 2
81 delegate_to: localhost
82
83 - name: Delete vm from HA group
84 community.proxmox.proxmox_cluster_ha_resources:
85 state: "absent"
86 name: vm:100
87 delegate_to: localhost
88
89 - name: Add VM to HA group based on 'ha_' tag
90 community.proxmox.proxmox_cluster_ha_resources:
91 name: "{{ proxmox_type }}:{{ proxmox_vmid }}"
92 state: "present"
93 group: "{{ proxmox_tags | split(';') | select('match', '^ha_') }}"
94 max_relocate: 2
95 max_restart: 2
96 delegate_to: localhost
97 """
98
99 RETURN = r"""
100 # These are examples of possible return values, and in general should use other names for return values.
101 old_groups:
102 description: The original name param that was passed in.
103 type: list
104 returned: always
105 new_groups:
106 description: The output message that the test module generates.
107 type: list
108 returned: when changed
109 """
110
111 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
112 ProxmoxAnsible,
113 create_proxmox_module,
114 )
115
116
117 def module_args():
118 return dict(
119 state=dict(choices=["present", "absent"], required=True),
120 name=dict(type="str", required=True),
121 comment=dict(type="str", default="", required=False),
122 group=dict(type="str", required=False),
123 max_relocate=dict(type="int", default=1, required=False),
124 max_restart=dict(type="int", default=1, required=False),
125 hastate=dict(choices=["started", "stopped", "disabled", "ignored"], default="started", required=False),
126 )
127
128
129 def module_options():
130 return dict(supports_check_mode=False)
131
132
133 class ProxmoxClusterHAResourcesAnsible(ProxmoxAnsible):
134 def _get(self):
135 resources = self.proxmox_api.cluster.ha.resources.get()
136 return resources
137
138 def _post(self, **data):
139 return self.proxmox_api.cluster.ha.resources.post(**data)
140
141 def _put(self, sid, data):
142 return self.proxmox_api.cluster.ha.resources(sid).put(**data)
143
144 def _delete(self, sid):
145 return self.proxmox_api.cluster.ha.resources(sid).delete()
146
147 def create(self, resources, sid, comment, group, max_relocate, max_restart, state): # noqa: PLR0913
148 data = {
149 "comment": comment,
150 "group": group,
151 "max_relocate": max_relocate,
152 "max_restart": max_restart,
153 "state": state,
154 }
155
156 # Normalize input SIDs to version without : in them
157 if ":" in sid:
158 sid = sid.partition(":")[2]
159
160 for resource in resources:
161 if resource["sid"].partition(":")[2] != sid:
162 continue
163
164 # Compare all keys in the desired state against current state
165 if all(resource.get(k) == v for k, v in data.items()):
166 return False
167 else:
168 self._put(sid, data)
169 return True
170
171 self._post(sid=sid, **data)
172 return True
173
174 def delete(self, resources, sid):
175 for resource in resources:
176 if resource["sid"] != sid:
177 continue
178 self._delete(sid)
179 return True
180
181 return False
182
183
184 def run_module():
185 module = create_proxmox_module(module_args(), **module_options())
186 proxmox = ProxmoxClusterHAResourcesAnsible(module)
187
188 result = dict(
189 changed=False,
190 )
191
192 sid = module.params["name"]
193 comment = module.params["comment"]
194 group = module.params["group"]
195 max_relocate = module.params["max_relocate"]
196 max_restart = module.params["max_restart"]
197 state = module.params["hastate"]
198 try:
199 resources = proxmox._get()
200
201 if module.params["state"] == "present":
202 changed = proxmox.create(resources, sid, comment, group, max_relocate, max_restart, state)
203 else:
204 changed = proxmox.delete(resources, sid)
205
206 result["changed"] = changed
207 except Exception as e:
208 module.fail_json(msg=str(e), **result)
209
210 module.exit_json(**result)
211
212
213 def main():
214 run_module()
215
216
217 if __name__ == "__main__":
218 main()