| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2026, Michael Dombek (@michaelwdombek) <[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_cluster_status_info |
| 10 |
short_description: Retrieve Proxmox VE cluster status information |
| 11 |
description: |
| 12 |
- Retrieve status information about the Proxmox VE cluster. |
| 13 |
- Returns information about both the cluster itself and the nodes within it. |
| 14 |
author: Michael Dombek (@michaelwdombek) |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 17 |
- community.proxmox.proxmox.documentation |
| 18 |
- community.proxmox.attributes |
| 19 |
- community.proxmox.attributes.info_module |
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
EXAMPLES = r""" |
| 24 |
- name: Get cluster status |
| 25 |
community.proxmox.proxmox_cluster_status_info: |
| 26 |
register: cluster_status |
| 27 |
|
| 28 |
- name: Display cluster quorum status |
| 29 |
ansible.builtin.debug: |
| 30 |
msg: "Cluster has quorum: {{ cluster_status.cluster_status | selectattr('type', 'equalto', 'cluster') | map(attribute='quorate') | first }}" |
| 31 |
""" |
| 32 |
|
| 33 |
|
| 34 |
RETURN = r""" |
| 35 |
cluster_status: |
| 36 |
description: List containing cluster and node status information. |
| 37 |
returned: always, but can be empty |
| 38 |
type: list |
| 39 |
elements: dict |
| 40 |
contains: |
| 41 |
id: |
| 42 |
description: Unique identifier for the entry. |
| 43 |
returned: always |
| 44 |
type: str |
| 45 |
name: |
| 46 |
description: Name of the cluster or node. |
| 47 |
returned: always |
| 48 |
type: str |
| 49 |
type: |
| 50 |
description: Type of entry, either 'cluster' or 'node'. |
| 51 |
returned: always |
| 52 |
type: str |
| 53 |
ip: |
| 54 |
description: IP address of the resolved nodename. |
| 55 |
returned: when type is 'node' |
| 56 |
type: str |
| 57 |
level: |
| 58 |
description: Proxmox VE Subscription level, indicates if eligible for enterprise support. |
| 59 |
returned: when type is 'node' |
| 60 |
type: str |
| 61 |
local: |
| 62 |
description: Indicates if this is the responding node. |
| 63 |
returned: when type is 'node' |
| 64 |
type: bool |
| 65 |
nodeid: |
| 66 |
description: ID of the node from the corosync configuration. |
| 67 |
returned: when type is 'node' |
| 68 |
type: int |
| 69 |
nodes: |
| 70 |
description: Total count of nodes in the cluster, including offline nodes. |
| 71 |
returned: when type is 'cluster' |
| 72 |
type: int |
| 73 |
online: |
| 74 |
description: Indicates if the node is online or offline. |
| 75 |
returned: when type is 'node' |
| 76 |
type: bool |
| 77 |
quorate: |
| 78 |
description: Indicates if there is a majority of nodes online to make decisions. |
| 79 |
returned: when type is 'cluster' |
| 80 |
type: bool |
| 81 |
version: |
| 82 |
description: Current version of the corosync configuration file. |
| 83 |
returned: when type is 'cluster' |
| 84 |
type: int |
| 85 |
""" |
| 86 |
|
| 87 |
|
| 88 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 89 |
ProxmoxAnsible, |
| 90 |
create_proxmox_module, |
| 91 |
proxmox_to_ansible_bool, |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
class ProxmoxClusterStatusEntry: |
| 96 |
def __init__(self, entry): |
| 97 |
self.entry = entry |
| 98 |
# Convert proxmox representation of boolean fields for easier |
| 99 |
# manipulation within ansible. |
| 100 |
if "quorate" in self.entry: |
| 101 |
self.entry["quorate"] = proxmox_to_ansible_bool(self.entry["quorate"]) |
| 102 |
if "local" in self.entry: |
| 103 |
self.entry["local"] = proxmox_to_ansible_bool(self.entry["local"]) |
| 104 |
if "online" in self.entry: |
| 105 |
self.entry["online"] = proxmox_to_ansible_bool(self.entry["online"]) |
| 106 |
|
| 107 |
def to_dict(self): |
| 108 |
return self.entry |
| 109 |
|
| 110 |
|
| 111 |
def module_args(): |
| 112 |
return dict() |
| 113 |
|
| 114 |
|
| 115 |
def module_options(): |
| 116 |
return {} |
| 117 |
|
| 118 |
|
| 119 |
class ProxmoxClusterStatusInfoAnsible(ProxmoxAnsible): |
| 120 |
def get_cluster_status(self): |
| 121 |
try: |
| 122 |
status = self.proxmox_api.cluster.status.get() |
| 123 |
status = [ProxmoxClusterStatusEntry(entry).to_dict() for entry in status] |
| 124 |
return status |
| 125 |
except Exception as e: |
| 126 |
self.module.fail_json(msg=f"Failed to retrieve cluster status: {e}") |
| 127 |
|
| 128 |
|
| 129 |
def main(): |
| 130 |
module = create_proxmox_module(module_args(), **module_options()) |
| 131 |
proxmox = ProxmoxClusterStatusInfoAnsible(module) |
| 132 |
|
| 133 |
result = dict(changed=False) |
| 134 |
|
| 135 |
proxmox = ProxmoxClusterStatusInfoAnsible(module) |
| 136 |
|
| 137 |
cluster_status = proxmox.get_cluster_status() |
| 138 |
result["cluster_status"] = cluster_status |
| 139 |
|
| 140 |
module.exit_json(**result) |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
main() |