| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2026, Clément Cruau (@PendaGTP) <[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 |
--- |
| 10 |
module: proxmox_cluster_ha_rules_info |
| 11 |
short_description: Retrieve Proxmox VE HA rules |
| 12 |
description: |
| 13 |
- Retreive Proxmox VE High Availability managed resources rules |
| 14 |
version_added: 2.0.0 |
| 15 |
author: Clément Cruau (@PendaGTP) |
| 16 |
options: |
| 17 |
rule: |
| 18 |
description: |
| 19 |
- Target by rule name. |
| 20 |
aliases: ["name"] |
| 21 |
type: str |
| 22 |
type: |
| 23 |
description: |
| 24 |
- Target rules by type. |
| 25 |
choices: ["node-affinity", "resource-affinity"] |
| 26 |
type: str |
| 27 |
resource: |
| 28 |
description: |
| 29 |
- Target rules affecting the specified resource. |
| 30 |
type: str |
| 31 |
seealso: |
| 32 |
- module: community.proxmox.proxmox_cluster_ha_rules |
| 33 |
description: Management of HA rules |
| 34 |
- name: Proxmox HA rules configuration |
| 35 |
description: Complete reference of Proxmox VE HA rules |
| 36 |
link: https://pve.proxmox.com/pve-docs/chapter-ha-manager.html#ha_manager_rules |
| 37 |
extends_documentation_fragment: |
| 38 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 39 |
- community.proxmox.proxmox.documentation |
| 40 |
- community.proxmox.attributes |
| 41 |
- community.proxmox.attributes.info_module |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = r""" |
| 45 |
- name: Get prefer-pve-001 HA rule |
| 46 |
community.proxmox.proxmox_cluster_ha_rules_info: |
| 47 |
rule: prefer-pve-001 |
| 48 |
|
| 49 |
- name: List HA rules |
| 50 |
community.proxmox.proxmox_cluster_ha_rules_info: |
| 51 |
|
| 52 |
- name: List HA node-affinity rules |
| 53 |
community.proxmox.proxmox_cluster_ha_rules_info: |
| 54 |
type: node-affinity |
| 55 |
|
| 56 |
- name: List HA resource-affinity rules |
| 57 |
community.proxmox.proxmox_cluster_ha_rules_info: |
| 58 |
type: resource-affinity |
| 59 |
|
| 60 |
- name: List HA rules affected by a resource |
| 61 |
community.proxmox.proxmox_cluster_ha_rules_info: |
| 62 |
resource: vm:100 |
| 63 |
|
| 64 |
- name: List HA node-affinity rules affected by a resource |
| 65 |
community.proxmox.proxmox_cluster_ha_rules_info: |
| 66 |
type: node-affinity |
| 67 |
resource: vm:100 |
| 68 |
""" |
| 69 |
|
| 70 |
RETURN = r""" |
| 71 |
rules: |
| 72 |
description: HA rules |
| 73 |
returned: on success |
| 74 |
type: list |
| 75 |
elements: dict |
| 76 |
contains: |
| 77 |
rule: |
| 78 |
description: The name of the HA rule. |
| 79 |
returned: on success |
| 80 |
sample: prefer-pve-001 |
| 81 |
type: str |
| 82 |
type: |
| 83 |
description: The HA rule type |
| 84 |
returned: on success |
| 85 |
sample: node-affinity |
| 86 |
type: str |
| 87 |
comment: |
| 88 |
description: A comment associated with the rule. |
| 89 |
returned: on success |
| 90 |
sample: Prefer pve-001 for these VMs |
| 91 |
type: str |
| 92 |
resources: |
| 93 |
description: A list of HA resource IDs that this rule applies to. |
| 94 |
returned: on success |
| 95 |
sample: [ |
| 96 |
"vm:100", |
| 97 |
"ct:101" |
| 98 |
] |
| 99 |
type: list |
| 100 |
elements: str |
| 101 |
nodes: |
| 102 |
description: A list of cluster node names and their priorities (if configured, otherwise only the node name is returned) |
| 103 |
returned: on success, when rule type is node-affinity |
| 104 |
sample: [ |
| 105 |
"pve-001:1", |
| 106 |
"pve-002:2", |
| 107 |
"pve-003" |
| 108 |
] |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
affinity: |
| 112 |
description: The affinity type |
| 113 |
returned: on success, when rule type is resource-affinity |
| 114 |
sample: positive |
| 115 |
type: str |
| 116 |
disable: |
| 117 |
description: Whether the HA rule is disabled. |
| 118 |
returned: on success |
| 119 |
sample: false |
| 120 |
type: bool |
| 121 |
strict: |
| 122 |
description: Whether the node affinity rule is strict. |
| 123 |
returned: on success, when rule type is node-affinity |
| 124 |
sample: false |
| 125 |
type: bool |
| 126 |
order: |
| 127 |
description: The rule position. |
| 128 |
returned: on success |
| 129 |
sample: 1 |
| 130 |
type: int |
| 131 |
""" |
| 132 |
|
| 133 |
|
| 134 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 135 |
ProxmoxAnsible, |
| 136 |
create_proxmox_module, |
| 137 |
proxmox_to_ansible_bool, |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
def _normalize_rules(rules): |
| 142 |
rules = sorted(rules, key=lambda x: x["order"]) |
| 143 |
for rule in rules: |
| 144 |
rule.pop("digest", None) |
| 145 |
rule["resources"] = rule.get("resources", "").split(",") |
| 146 |
rule["disable"] = proxmox_to_ansible_bool(rule.get("disable", 0)) |
| 147 |
if rule["type"] == "node-affinity": |
| 148 |
rule["nodes"] = rule.get("nodes", "").split(",") |
| 149 |
rule["strict"] = proxmox_to_ansible_bool(rule.get("strict", 0)) |
| 150 |
|
| 151 |
return rules |
| 152 |
|
| 153 |
|
| 154 |
def module_args(): |
| 155 |
return dict( |
| 156 |
rule=dict(type="str", aliases=["name"]), |
| 157 |
type=dict(type="str", choices=["node-affinity", "resource-affinity"]), |
| 158 |
resource=dict(type="str"), |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def module_options(): |
| 163 |
return dict( |
| 164 |
mutually_exclusive=[("rule", "type"), ("rule", "resource")], |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
class ProxmoxClusterHARuleInfoAnsible(ProxmoxAnsible): |
| 169 |
def __init__(self, module): |
| 170 |
super().__init__(module) |
| 171 |
self.params = module.params |
| 172 |
|
| 173 |
def _list(self, rule_type=None, resource=None): |
| 174 |
params = {} |
| 175 |
if rule_type: |
| 176 |
params["type"] = rule_type |
| 177 |
if resource: |
| 178 |
params["resource"] = resource |
| 179 |
try: |
| 180 |
rules = self.proxmox_api.cluster.ha.rules.get(**params) |
| 181 |
return _normalize_rules(rules) |
| 182 |
except Exception as e: |
| 183 |
self.module.fail_json(msg=f"An error occurred: {e}") |
| 184 |
|
| 185 |
def _get(self, rule): |
| 186 |
try: |
| 187 |
rule = self.proxmox_api.cluster.ha.rules(rule).get() |
| 188 |
return _normalize_rules([rule]) |
| 189 |
except Exception as e: |
| 190 |
self.module.fail_json(msg=f"An error occurred: {e}") |
| 191 |
|
| 192 |
def run(self): |
| 193 |
rule = self.params.get("rule") |
| 194 |
rule_type = self.params.get("type") |
| 195 |
resource = self.params.get("resource") |
| 196 |
|
| 197 |
if rule: |
| 198 |
rules = self._get(rule) |
| 199 |
else: |
| 200 |
rules = self._list(rule_type, resource) |
| 201 |
|
| 202 |
return rules |
| 203 |
|
| 204 |
|
| 205 |
def main(): |
| 206 |
module = create_proxmox_module(module_args(), **module_options()) |
| 207 |
proxmox = ProxmoxClusterHARuleInfoAnsible(module) |
| 208 |
|
| 209 |
result = dict(changed=False) |
| 210 |
|
| 211 |
result["rules"] = proxmox.run() |
| 212 |
module.exit_json(**result) |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
main() |