mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
feat: compute load balancer targets status using a filter (#550)
##### SUMMARY Allow to compute the status of a load balancer using a filter. Closes #467 ##### ISSUE TYPE - Feature Pull Request ##### COMPONENT NAME hetzner.hcloud.loab_balancer_status
This commit is contained in:
parent
a85bd39738
commit
fce8bc9bb9
10 changed files with 199 additions and 10 deletions
49
plugins/filter/all.py
Normal file
49
plugins/filter/all.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def load_balancer_status(load_balancer: dict, *args, **kwargs) -> Literal["unknown", "unhealthy", "healthy"]:
|
||||
"""
|
||||
Return the status of a Load Balancer based on its targets.
|
||||
"""
|
||||
try:
|
||||
result = "healthy"
|
||||
for target in load_balancer["targets"]:
|
||||
target_health_status = target.get("health_status")
|
||||
|
||||
# Report missing health status as unknown
|
||||
if not target_health_status:
|
||||
result = "unknown"
|
||||
continue
|
||||
|
||||
for health_status in target_health_status:
|
||||
status = health_status.get("status")
|
||||
if status == "healthy":
|
||||
continue
|
||||
|
||||
if status in (None, "unknown"):
|
||||
result = "unknown"
|
||||
continue
|
||||
|
||||
if status == "unhealthy":
|
||||
return "unhealthy"
|
||||
|
||||
return result
|
||||
except Exception as exc:
|
||||
raise AnsibleFilterError(f"load_balancer_status - {to_native(exc)}", orig_exc=exc) from exc
|
||||
|
||||
|
||||
class FilterModule:
|
||||
"""
|
||||
Hetzner Cloud filters.
|
||||
"""
|
||||
|
||||
def filters(self):
|
||||
return {
|
||||
"load_balancer_status": load_balancer_status,
|
||||
}
|
||||
19
plugins/filter/load_balancer_status.yml
Normal file
19
plugins/filter/load_balancer_status.yml
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
DOCUMENTATION:
|
||||
name: load_balancer_status
|
||||
version_added: 4.2.0
|
||||
short_description: Compute the status of a Load Balancer
|
||||
description:
|
||||
- Compute the status of a Load Balancer based on its targets.
|
||||
options:
|
||||
_input:
|
||||
description: Load Balancer data.
|
||||
type: dict
|
||||
required: true
|
||||
EXAMPLES: |
|
||||
# Ensure a load balancer is healthy
|
||||
{{ result.hcloud_load_balancer_info[0] | hetzner.hcloud.load_balancer_status == "healthy" }}
|
||||
|
||||
RETURN:
|
||||
_value:
|
||||
description: The status of the Load Balancer targets, can be one of C(unknown), C(unhealthy) or C(healthy).
|
||||
type: string
|
||||
Loading…
Add table
Add a link
Reference in a new issue