| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: load_balancer_info |
| 12 |
|
| 13 |
short_description: Gather infos about your Hetzner Cloud Load Balancers. |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Gather infos about your Hetzner Cloud Load Balancers.. |
| 18 |
|
| 19 |
author: |
| 20 |
- Lukas Kaemmerling (@LKaemmerling) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- The ID of the Load Balancers you want to get. |
| 26 |
- The module will fail if the provided ID is invalid. |
| 27 |
type: int |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the Load Balancers you want to get. |
| 31 |
type: str |
| 32 |
label_selector: |
| 33 |
description: |
| 34 |
- The label selector for the Load Balancers you want to get. |
| 35 |
type: str |
| 36 |
extends_documentation_fragment: |
| 37 |
- hetzner.hcloud.hcloud |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = """ |
| 42 |
- name: Gather hcloud load_balancer infos |
| 43 |
hetzner.hcloud.load_balancer_info: |
| 44 |
register: output |
| 45 |
|
| 46 |
- name: Print the gathered infos |
| 47 |
debug: |
| 48 |
var: output |
| 49 |
""" |
| 50 |
|
| 51 |
RETURN = """ |
| 52 |
hcloud_load_balancer_info: |
| 53 |
description: The load_balancer infos as list |
| 54 |
returned: always |
| 55 |
type: list |
| 56 |
contains: |
| 57 |
id: |
| 58 |
description: Numeric identifier of the Load Balancer |
| 59 |
returned: always |
| 60 |
type: int |
| 61 |
sample: 1937415 |
| 62 |
name: |
| 63 |
description: Name of the Load Balancer |
| 64 |
returned: always |
| 65 |
type: str |
| 66 |
sample: my-Load-Balancer |
| 67 |
load_balancer_type: |
| 68 |
description: Name of the Load Balancer type of the Load Balancer |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: lb11 |
| 72 |
ipv4_address: |
| 73 |
description: Public IPv4 address of the Load Balancer |
| 74 |
returned: always |
| 75 |
type: str |
| 76 |
sample: 116.203.104.109 |
| 77 |
ipv6_address: |
| 78 |
description: Public IPv6 address of the Load Balancer |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: 2a01:4f8:1c1c:c140::1 |
| 82 |
location: |
| 83 |
description: Name of the location of the Load Balancer |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: fsn1 |
| 87 |
labels: |
| 88 |
description: User-defined labels (key-value pairs) |
| 89 |
returned: always |
| 90 |
type: dict |
| 91 |
delete_protection: |
| 92 |
description: True if Load Balancer is protected for deletion |
| 93 |
type: bool |
| 94 |
returned: always |
| 95 |
sample: false |
| 96 |
disable_public_interface: |
| 97 |
description: True if Load Balancer public interface is disabled |
| 98 |
type: bool |
| 99 |
returned: always |
| 100 |
sample: false |
| 101 |
targets: |
| 102 |
description: The targets of the Load Balancer |
| 103 |
returned: always |
| 104 |
type: list |
| 105 |
contains: |
| 106 |
type: |
| 107 |
description: Type of the Load Balancer Target |
| 108 |
type: str |
| 109 |
returned: always |
| 110 |
sample: server |
| 111 |
load_balancer: |
| 112 |
description: Name of the Load Balancer |
| 113 |
type: str |
| 114 |
returned: always |
| 115 |
sample: my-LoadBalancer |
| 116 |
server: |
| 117 |
description: Name of the Server |
| 118 |
type: str |
| 119 |
returned: if I(type) is server |
| 120 |
sample: my-server |
| 121 |
label_selector: |
| 122 |
description: Label Selector |
| 123 |
type: str |
| 124 |
returned: if I(type) is label_selector |
| 125 |
sample: application=backend |
| 126 |
ip: |
| 127 |
description: IP of the dedicated server |
| 128 |
type: str |
| 129 |
returned: if I(type) is ip |
| 130 |
sample: 127.0.0.1 |
| 131 |
use_private_ip: |
| 132 |
description: |
| 133 |
- Route the traffic over the private IP of the Load Balancer through a Hetzner Cloud Network. |
| 134 |
- Load Balancer needs to be attached to a network. See M(hetzner.hcloud.load_balancer_network) |
| 135 |
type: bool |
| 136 |
sample: true |
| 137 |
returned: always |
| 138 |
health_status: |
| 139 |
description: |
| 140 |
- List of health statuses of the services on this target. Only present for target types "server" and "ip". |
| 141 |
type: list |
| 142 |
returned: if I(type) is server or ip |
| 143 |
contains: |
| 144 |
listen_port: |
| 145 |
description: Load Balancer Target listen port |
| 146 |
type: int |
| 147 |
returned: always |
| 148 |
sample: 80 |
| 149 |
status: |
| 150 |
description: Load Balancer Target status |
| 151 |
type: str |
| 152 |
choices: [healthy, unhealthy, unknown] |
| 153 |
returned: always |
| 154 |
sample: healthy |
| 155 |
services: |
| 156 |
description: all services from this Load Balancer |
| 157 |
returned: Always |
| 158 |
type: list |
| 159 |
contains: |
| 160 |
listen_port: |
| 161 |
description: The port the service listens on, i.e. the port users can connect to. |
| 162 |
returned: always |
| 163 |
type: int |
| 164 |
sample: 443 |
| 165 |
protocol: |
| 166 |
description: Protocol of the service |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: http |
| 170 |
destination_port: |
| 171 |
description: |
| 172 |
- The port traffic is forwarded to, i.e. the port the targets are listening and accepting connections on. |
| 173 |
returned: always |
| 174 |
type: int |
| 175 |
sample: 80 |
| 176 |
proxyprotocol: |
| 177 |
description: |
| 178 |
- Enable the PROXY protocol. |
| 179 |
returned: always |
| 180 |
type: bool |
| 181 |
sample: false |
| 182 |
http: |
| 183 |
description: Configuration for HTTP and HTTPS services |
| 184 |
returned: always |
| 185 |
type: dict |
| 186 |
contains: |
| 187 |
cookie_name: |
| 188 |
description: Name of the cookie which will be set when you enable sticky sessions |
| 189 |
returned: always |
| 190 |
type: str |
| 191 |
sample: HCLBSTICKY |
| 192 |
cookie_lifetime: |
| 193 |
description: Lifetime of the cookie which will be set when you enable sticky sessions, in seconds |
| 194 |
returned: always |
| 195 |
type: int |
| 196 |
sample: 3600 |
| 197 |
certificates: |
| 198 |
description: List of Names or IDs of certificates |
| 199 |
returned: always |
| 200 |
type: list |
| 201 |
elements: str |
| 202 |
sticky_sessions: |
| 203 |
description: Enable or disable sticky_sessions |
| 204 |
returned: always |
| 205 |
type: bool |
| 206 |
sample: true |
| 207 |
redirect_http: |
| 208 |
description: Redirect Traffic from Port 80 to Port 443, only available if protocol is https |
| 209 |
returned: always |
| 210 |
type: bool |
| 211 |
sample: false |
| 212 |
health_check: |
| 213 |
description: Configuration for health checks |
| 214 |
returned: always |
| 215 |
type: dict |
| 216 |
contains: |
| 217 |
protocol: |
| 218 |
description: Protocol the health checks will be performed over |
| 219 |
returned: always |
| 220 |
type: str |
| 221 |
sample: http |
| 222 |
port: |
| 223 |
description: Port the health check will be performed on |
| 224 |
returned: always |
| 225 |
type: int |
| 226 |
sample: 80 |
| 227 |
interval: |
| 228 |
description: Interval of health checks, in seconds |
| 229 |
returned: always |
| 230 |
type: int |
| 231 |
sample: 15 |
| 232 |
timeout: |
| 233 |
description: Timeout of health checks, in seconds |
| 234 |
returned: always |
| 235 |
type: int |
| 236 |
sample: 10 |
| 237 |
retries: |
| 238 |
description: Number of retries until a target is marked as unhealthy |
| 239 |
returned: always |
| 240 |
type: int |
| 241 |
sample: 3 |
| 242 |
http: |
| 243 |
description: Additional Configuration of health checks with protocol http/https |
| 244 |
returned: always |
| 245 |
type: dict |
| 246 |
contains: |
| 247 |
domain: |
| 248 |
description: Domain we will set within the HTTP HOST header |
| 249 |
returned: always |
| 250 |
type: str |
| 251 |
sample: example.com |
| 252 |
path: |
| 253 |
description: Path we will try to access |
| 254 |
returned: always |
| 255 |
type: str |
| 256 |
sample: / |
| 257 |
response: |
| 258 |
description: Response we expect, if response is not within the health check response the target is unhealthy |
| 259 |
returned: always |
| 260 |
type: str |
| 261 |
status_codes: |
| 262 |
description: List of HTTP status codes we expect to get when we perform the health check. |
| 263 |
returned: always |
| 264 |
type: list |
| 265 |
elements: str |
| 266 |
sample: ["2??","3??"] |
| 267 |
tls: |
| 268 |
description: Verify the TLS certificate, only available if health check protocol is https |
| 269 |
returned: always |
| 270 |
type: bool |
| 271 |
sample: false |
| 272 |
""" |
| 273 |
|
| 274 |
from ansible.module_utils.basic import AnsibleModule |
| 275 |
|
| 276 |
from ..module_utils._base import AnsibleHCloud |
| 277 |
from ..module_utils._vendor.hcloud import HCloudException |
| 278 |
from ..module_utils._vendor.hcloud.load_balancers import ( |
| 279 |
BoundLoadBalancer, |
| 280 |
LoadBalancerService, |
| 281 |
LoadBalancerTarget, |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
class AnsibleHCloudLoadBalancerInfo(AnsibleHCloud): |
| 286 |
represent = "hcloud_load_balancer_info" |
| 287 |
|
| 288 |
hcloud_load_balancer_info: list[BoundLoadBalancer] | None = None |
| 289 |
|
| 290 |
def _prepare_result(self): |
| 291 |
tmp = [] |
| 292 |
|
| 293 |
for load_balancer in self.hcloud_load_balancer_info: |
| 294 |
if load_balancer is None: |
| 295 |
continue |
| 296 |
|
| 297 |
tmp.append( |
| 298 |
{ |
| 299 |
"id": load_balancer.id, |
| 300 |
"name": load_balancer.name, |
| 301 |
"ipv4_address": load_balancer.public_net.ipv4.ip, |
| 302 |
"ipv6_address": load_balancer.public_net.ipv6.ip, |
| 303 |
"private_ipv4_address": load_balancer.private_net[0].ip if len(load_balancer.private_net) else None, |
| 304 |
"load_balancer_type": load_balancer.load_balancer_type.name, |
| 305 |
"location": load_balancer.location.name, |
| 306 |
"labels": load_balancer.labels, |
| 307 |
"delete_protection": load_balancer.protection["delete"], |
| 308 |
"disable_public_interface": False if load_balancer.public_net.enabled else True, |
| 309 |
"targets": [self._prepare_target_result(target) for target in load_balancer.targets], |
| 310 |
"services": [self._prepare_service_result(service) for service in load_balancer.services], |
| 311 |
} |
| 312 |
) |
| 313 |
return tmp |
| 314 |
|
| 315 |
@staticmethod |
| 316 |
def _prepare_service_result(service: LoadBalancerService): |
| 317 |
http = None |
| 318 |
if service.protocol != "tcp": |
| 319 |
http = { |
| 320 |
"cookie_name": service.http.cookie_name, |
| 321 |
"cookie_lifetime": service.http.cookie_lifetime, |
| 322 |
"redirect_http": service.http.redirect_http, |
| 323 |
"sticky_sessions": service.http.sticky_sessions, |
| 324 |
"certificates": [certificate.name for certificate in service.http.certificates], |
| 325 |
} |
| 326 |
health_check = { |
| 327 |
"protocol": service.health_check.protocol, |
| 328 |
"port": service.health_check.port, |
| 329 |
"interval": service.health_check.interval, |
| 330 |
"timeout": service.health_check.timeout, |
| 331 |
"retries": service.health_check.retries, |
| 332 |
} |
| 333 |
if service.health_check.protocol != "tcp": |
| 334 |
health_check["http"] = { |
| 335 |
"domain": service.health_check.http.domain, |
| 336 |
"path": service.health_check.http.path, |
| 337 |
"response": service.health_check.http.response, |
| 338 |
"certificates": service.health_check.http.status_codes, |
| 339 |
"tls": service.health_check.http.tls, |
| 340 |
} |
| 341 |
return { |
| 342 |
"protocol": service.protocol, |
| 343 |
"listen_port": service.listen_port, |
| 344 |
"destination_port": service.destination_port, |
| 345 |
"proxyprotocol": service.proxyprotocol, |
| 346 |
"http": http, |
| 347 |
"health_check": health_check, |
| 348 |
} |
| 349 |
|
| 350 |
@staticmethod |
| 351 |
def _prepare_target_result(target: LoadBalancerTarget): |
| 352 |
result = { |
| 353 |
"type": target.type, |
| 354 |
"use_private_ip": target.use_private_ip, |
| 355 |
} |
| 356 |
if target.type == "server": |
| 357 |
result["server"] = target.server.name |
| 358 |
elif target.type == "label_selector": |
| 359 |
result["label_selector"] = target.label_selector.selector |
| 360 |
elif target.type == "ip": |
| 361 |
result["ip"] = target.ip.ip |
| 362 |
|
| 363 |
if target.health_status is not None: |
| 364 |
result["health_status"] = [ |
| 365 |
{ |
| 366 |
"listen_port": item.listen_port, |
| 367 |
"status": item.status, |
| 368 |
} |
| 369 |
for item in target.health_status |
| 370 |
] |
| 371 |
|
| 372 |
return result |
| 373 |
|
| 374 |
def get_load_balancers(self): |
| 375 |
try: |
| 376 |
if self.module.params.get("id") is not None: |
| 377 |
self.hcloud_load_balancer_info = [self.client.load_balancers.get_by_id(self.module.params.get("id"))] |
| 378 |
elif self.module.params.get("name") is not None: |
| 379 |
self.hcloud_load_balancer_info = [ |
| 380 |
self.client.load_balancers.get_by_name(self.module.params.get("name")) |
| 381 |
] |
| 382 |
else: |
| 383 |
params = {} |
| 384 |
label_selector = self.module.params.get("label_selector") |
| 385 |
if label_selector: |
| 386 |
params["label_selector"] = label_selector |
| 387 |
|
| 388 |
self.hcloud_load_balancer_info = self.client.load_balancers.get_all(**params) |
| 389 |
|
| 390 |
except HCloudException as exception: |
| 391 |
self.fail_json_hcloud(exception) |
| 392 |
|
| 393 |
@classmethod |
| 394 |
def define_module(cls): |
| 395 |
return AnsibleModule( |
| 396 |
argument_spec=dict( |
| 397 |
id={"type": "int"}, |
| 398 |
name={"type": "str"}, |
| 399 |
label_selector={"type": "str"}, |
| 400 |
**super().base_module_arguments(), |
| 401 |
), |
| 402 |
supports_check_mode=True, |
| 403 |
) |
| 404 |
|
| 405 |
|
| 406 |
def main(): |
| 407 |
module = AnsibleHCloudLoadBalancerInfo.define_module() |
| 408 |
hcloud = AnsibleHCloudLoadBalancerInfo(module) |
| 409 |
|
| 410 |
hcloud.get_load_balancers() |
| 411 |
result = hcloud.get_result() |
| 412 |
|
| 413 |
ansible_info = {"hcloud_load_balancer_info": result["hcloud_load_balancer_info"]} |
| 414 |
module.exit_json(**ansible_info) |
| 415 |
|
| 416 |
|
| 417 |
if __name__ == "__main__": |
| 418 |
main() |