2020-03-09 13:36:01 +00:00
|
|
|
# Copyright (c) 2019 Hetzner Cloud GmbH <info@hetzner-cloud.de>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-12-21 16:49:24 +00:00
|
|
|
DOCUMENTATION = """
|
2023-11-23 13:29:27 +00:00
|
|
|
name: hcloud
|
|
|
|
short_description: Ansible dynamic inventory plugin for the Hetzner Cloud.
|
|
|
|
|
|
|
|
description:
|
|
|
|
- Reads inventories from the Hetzner Cloud API.
|
|
|
|
- Uses a YAML configuration file that ends with C(hcloud.yml) or C(hcloud.yaml).
|
|
|
|
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
|
|
|
|
requirements:
|
|
|
|
- python-dateutil >= 2.7.5
|
|
|
|
- requests >=2.20
|
|
|
|
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- constructed
|
|
|
|
- inventory_cache
|
|
|
|
|
|
|
|
options:
|
|
|
|
plugin:
|
2023-12-21 16:49:24 +00:00
|
|
|
description: Mark this as an P(hetzner.hcloud.hcloud#inventory) inventory instance.
|
2023-11-23 13:29:27 +00:00
|
|
|
required: true
|
|
|
|
choices: [hcloud, hetzner.hcloud.hcloud]
|
|
|
|
|
|
|
|
api_token:
|
|
|
|
description:
|
|
|
|
- The API Token for the Hetzner Cloud.
|
|
|
|
type: str
|
2024-02-05 11:18:39 +00:00
|
|
|
required: true
|
2023-11-23 13:29:27 +00:00
|
|
|
aliases: [token]
|
|
|
|
env:
|
|
|
|
- name: HCLOUD_TOKEN
|
|
|
|
api_endpoint:
|
|
|
|
description:
|
|
|
|
- The API Endpoint for the Hetzner Cloud.
|
|
|
|
type: str
|
|
|
|
default: https://api.hetzner.cloud/v1
|
|
|
|
env:
|
|
|
|
- name: HCLOUD_ENDPOINT
|
|
|
|
|
|
|
|
group:
|
|
|
|
description: The group all servers are automatically added to.
|
|
|
|
default: hcloud
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
connect_with:
|
|
|
|
description: |
|
2023-12-21 16:49:24 +00:00
|
|
|
Connect to the server using the value from this field. This sets the C(ansible_host)
|
2023-11-23 13:29:27 +00:00
|
|
|
variable to the value indicated, if that value is available. If you need further
|
|
|
|
customization, like falling back to private ipv4 if the server has no public ipv4,
|
2023-12-21 16:49:24 +00:00
|
|
|
you can use O(compose) top-level key.
|
2023-11-23 13:29:27 +00:00
|
|
|
default: public_ipv4
|
|
|
|
type: str
|
|
|
|
choices:
|
|
|
|
- public_ipv4
|
|
|
|
- public_ipv6
|
|
|
|
- hostname
|
|
|
|
- ipv4_dns_ptr
|
|
|
|
- private_ipv4
|
|
|
|
|
|
|
|
locations:
|
|
|
|
description: Populate inventory with instances in this location.
|
|
|
|
default: []
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
types:
|
|
|
|
description: Populate inventory with instances with this type.
|
|
|
|
default: []
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
images:
|
|
|
|
description: Populate inventory with instances with this image name, only available for system images.
|
|
|
|
default: []
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
label_selector:
|
|
|
|
description: Populate inventory with instances with this label.
|
|
|
|
default: ""
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
network:
|
|
|
|
description: Populate inventory with instances which are attached to this network name or ID.
|
|
|
|
default: ""
|
|
|
|
type: str
|
|
|
|
required: false
|
|
|
|
status:
|
|
|
|
description: Populate inventory with instances with this status.
|
|
|
|
default: []
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
required: false
|
feat: add `hostvars_prefix` and `hostvars_suffix` options to inventory hostvars (#423)
##### SUMMARY
Add `hostvars_prefix` and `hostvars_suffix` options to customize the
inventory host variables keys.
For example, with `hostvars_prefix: hcloud_ `, the host vars will be
stored as follows:
```json
{
"_meta": {
"hostvars": {
"tmp": {
"ansible_host": "65.109.169.27",
"hcloud_architecture": "x86",
"hcloud_datacenter": "hel1-dc2",
"hcloud_id": 40573407,
"hcloud_image_id": 114690387,
"hcloud_image_name": "debian-12",
"hcloud_image_os_flavor": "debian",
"hcloud_ipv4": "65.109.169.27",
"hcloud_ipv6_network_mask": "64",
"hcloud_ipv6_network": "2a01:4f9:c012:4377::",
"hcloud_ipv6": "2a01:4f9:c012:4377::1",
"hcloud_labels": {},
"hcloud_location": "hel1",
"hcloud_name": "tmp",
"hcloud_private_networks": [],
"hcloud_server_type": "cx11",
"hcloud_status": "running",
"hcloud_type": "cx11"
}
}
}
}
```
Related to #116
2023-12-15 14:40:37 +00:00
|
|
|
|
|
|
|
hostvars_prefix:
|
|
|
|
description:
|
|
|
|
- The prefix for host variables names coming from Hetzner Cloud.
|
|
|
|
type: str
|
|
|
|
version_added: 2.5.0
|
|
|
|
hostvars_suffix:
|
|
|
|
description:
|
|
|
|
- The suffix for host variables names coming from Hetzner Cloud.
|
|
|
|
type: str
|
|
|
|
version_added: 2.5.0
|
2024-02-05 13:32:01 +00:00
|
|
|
|
|
|
|
hostname:
|
|
|
|
description:
|
|
|
|
- A template for the instances hostname, if not provided the Hetzner Cloud server name will be used.
|
|
|
|
- Available variables are the Hetzner Cloud host variables.
|
|
|
|
- The available variables names are provide with the O(hostvars_prefix) or O(hostvars_suffix) modifications.
|
|
|
|
type: str
|
|
|
|
version_added: 3.0.0
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-12-21 16:49:24 +00:00
|
|
|
EXAMPLES = """
|
|
|
|
# Minimal example. 'HCLOUD_TOKEN' is exposed in environment.
|
|
|
|
plugin: hetzner.hcloud.hcloud
|
|
|
|
|
|
|
|
---
|
|
|
|
# Example with templated token, e.g. provided through extra vars.
|
|
|
|
plugin: hetzner.hcloud.hcloud
|
|
|
|
api_token: "{{ _vault_hetzner_cloud_token }}"
|
|
|
|
|
|
|
|
---
|
|
|
|
# Example with locations, types, status
|
|
|
|
plugin: hetzner.hcloud.hcloud
|
2020-03-09 13:36:01 +00:00
|
|
|
locations:
|
|
|
|
- nbg1
|
|
|
|
types:
|
2024-06-11 13:30:47 +00:00
|
|
|
- cx22
|
2022-06-13 06:57:16 +00:00
|
|
|
status:
|
|
|
|
- running
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-12-21 16:49:24 +00:00
|
|
|
---
|
2020-03-09 13:36:01 +00:00
|
|
|
# Group by a location with prefix e.g. "hcloud_location_nbg1"
|
|
|
|
# and image_os_flavor without prefix and separator e.g. "ubuntu"
|
|
|
|
# and status with prefix e.g. "server_status_running"
|
2023-12-21 16:49:24 +00:00
|
|
|
plugin: hetzner.hcloud.hcloud
|
2020-03-09 13:36:01 +00:00
|
|
|
keyed_groups:
|
|
|
|
- key: location
|
|
|
|
prefix: hcloud_location
|
|
|
|
- key: image_os_flavor
|
|
|
|
separator: ""
|
|
|
|
- key: status
|
|
|
|
prefix: server_status
|
2024-02-05 13:32:01 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
# Use a custom hostname template.
|
|
|
|
plugin: hetzner.hcloud.hcloud
|
|
|
|
|
|
|
|
# Available variables are for example:
|
|
|
|
## Server
|
|
|
|
# id: 42984895
|
|
|
|
# name: "my-server"
|
|
|
|
# labels:
|
|
|
|
# foo: "bar"
|
|
|
|
# status: "running"
|
|
|
|
## Server Type
|
2024-06-11 13:30:47 +00:00
|
|
|
# type: "cx22"
|
|
|
|
# server_type: "cx22"
|
2024-02-05 13:32:01 +00:00
|
|
|
# architecture: "x86"
|
|
|
|
## Image
|
|
|
|
# image_id: 114690387
|
|
|
|
# image_name: "debian-12"
|
|
|
|
# image_os_flavor: "debian"
|
|
|
|
## Datacenter
|
|
|
|
# datacenter: "hel1-dc2"
|
|
|
|
# location: "hel1"
|
|
|
|
## Network
|
|
|
|
# ipv4: "65.109.140.95" # Value is optional!
|
|
|
|
# ipv6: "2a01:4f9:c011:b83f::1" # Value is optional!
|
|
|
|
# ipv6_network: 2a01:4f9:c011:b83f::" # Value is optional!
|
|
|
|
# ipv6_network_mask: "64" # Value is optional!
|
|
|
|
# private_ipv4: "10.0.0.3" # Value is optional!
|
|
|
|
# private_networks:
|
|
|
|
# - id: 114690387
|
|
|
|
# name: "my-private-network"
|
|
|
|
# ip: "10.0.0.3"
|
|
|
|
#
|
|
|
|
hostname: "my-prefix-{{ datacenter }}-{{ name }}-{{ server_type }}"
|
2020-03-09 13:36:01 +00:00
|
|
|
"""
|
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
import sys
|
2023-06-27 09:50:13 +00:00
|
|
|
from ipaddress import IPv6Network
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2023-08-16 09:59:17 +00:00
|
|
|
from ansible.inventory.manager import InventoryData
|
2023-06-27 11:17:21 +00:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2023-08-16 09:59:17 +00:00
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, Constructable
|
2023-11-23 13:29:27 +00:00
|
|
|
from ansible.utils.display import Display
|
2024-02-05 13:32:01 +00:00
|
|
|
from ansible.utils.vars import combine_vars
|
2023-07-31 08:12:55 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
from ..module_utils.client import (
|
|
|
|
Client,
|
|
|
|
ClientException,
|
|
|
|
client_check_required_lib,
|
|
|
|
client_get_by_name_or_id,
|
|
|
|
)
|
|
|
|
from ..module_utils.vendor.hcloud import APIException
|
|
|
|
from ..module_utils.vendor.hcloud.networks import Network
|
2023-08-16 09:59:17 +00:00
|
|
|
from ..module_utils.vendor.hcloud.servers import Server
|
2023-08-14 13:55:04 +00:00
|
|
|
from ..module_utils.version import version
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
if sys.version_info >= (3, 11):
|
|
|
|
# The typed dicts are only used to help development and we prefer not requiring
|
|
|
|
# the additional typing-extensions dependency
|
|
|
|
from typing import NotRequired, TypedDict
|
|
|
|
|
|
|
|
class InventoryPrivateNetwork(TypedDict):
|
|
|
|
id: int
|
|
|
|
name: str
|
|
|
|
ip: str
|
|
|
|
|
|
|
|
class InventoryServer(TypedDict):
|
|
|
|
id: int
|
|
|
|
name: str
|
|
|
|
status: str
|
2023-12-15 14:25:12 +00:00
|
|
|
|
|
|
|
# Server Type
|
2023-08-16 09:59:17 +00:00
|
|
|
type: str
|
2023-12-15 14:25:12 +00:00
|
|
|
server_type: str
|
2023-08-16 09:59:17 +00:00
|
|
|
architecture: str
|
|
|
|
|
|
|
|
# Datacenter
|
|
|
|
datacenter: str
|
|
|
|
location: str
|
|
|
|
|
|
|
|
# Labels
|
|
|
|
labels: dict[str, str]
|
|
|
|
|
|
|
|
# Network
|
|
|
|
ipv4: NotRequired[str]
|
|
|
|
ipv6: NotRequired[str]
|
|
|
|
ipv6_network: NotRequired[str]
|
|
|
|
ipv6_network_mask: NotRequired[str]
|
|
|
|
private_ipv4: NotRequired[str]
|
|
|
|
private_networks: list[InventoryPrivateNetwork]
|
|
|
|
|
|
|
|
# Image
|
|
|
|
image_id: int
|
|
|
|
image_name: str
|
|
|
|
image_os_flavor: str
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
# Ansible
|
|
|
|
ansible_host: str
|
|
|
|
|
|
|
|
else:
|
|
|
|
InventoryServer = dict
|
|
|
|
|
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
def first_ipv6_address(network: str) -> str:
|
|
|
|
"""
|
|
|
|
Return the first address for a ipv6 network.
|
|
|
|
|
|
|
|
:param network: IPv6 Network.
|
|
|
|
"""
|
2024-04-16 09:48:03 +00:00
|
|
|
return str(next(IPv6Network(network).hosts()))
|
2023-11-24 12:43:34 +00:00
|
|
|
|
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
2023-06-27 09:50:13 +00:00
|
|
|
NAME = "hetzner.hcloud.hcloud"
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
inventory: InventoryData
|
2023-11-23 13:29:27 +00:00
|
|
|
display: Display
|
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
client: Client
|
|
|
|
|
|
|
|
network: Network | None
|
2023-08-16 09:59:17 +00:00
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
def _configure_hcloud_client(self):
|
2023-11-23 13:29:27 +00:00
|
|
|
api_token = self.get_option("api_token")
|
|
|
|
api_endpoint = self.get_option("api_endpoint")
|
|
|
|
|
|
|
|
# Resolve template string
|
|
|
|
api_token = self.templar.template(api_token)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
self.client = Client(
|
2023-11-23 13:29:27 +00:00
|
|
|
token=api_token,
|
|
|
|
api_endpoint=api_endpoint,
|
2023-06-27 09:50:13 +00:00
|
|
|
application_name="ansible-inventory",
|
2023-08-14 13:55:04 +00:00
|
|
|
application_version=version,
|
2023-06-27 09:50:13 +00:00
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
try:
|
2023-11-23 13:29:27 +00:00
|
|
|
# Ensure the api token is valid
|
|
|
|
self.client.locations.get_list()
|
2023-11-24 12:43:34 +00:00
|
|
|
except APIException as exception:
|
2023-11-23 13:29:27 +00:00
|
|
|
raise AnsibleError("Invalid Hetzner Cloud API Token.") from exception
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
def _validate_options(self) -> None:
|
2020-03-09 13:36:01 +00:00
|
|
|
if self.get_option("network"):
|
2023-11-24 12:43:34 +00:00
|
|
|
network_param: str = self.get_option("network")
|
|
|
|
network_param = self.templar.template(network_param)
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
try:
|
2023-11-24 12:43:34 +00:00
|
|
|
self.network = client_get_by_name_or_id(self.client, "networks", network_param)
|
|
|
|
except (ClientException, APIException) as exception:
|
|
|
|
raise AnsibleError(to_native(exception)) from exception
|
|
|
|
|
|
|
|
def _fetch_servers(self) -> list[Server]:
|
|
|
|
self._validate_options()
|
|
|
|
|
|
|
|
get_servers_params = {}
|
|
|
|
if self.get_option("label_selector"):
|
|
|
|
get_servers_params["label_selector"] = self.get_option("label_selector")
|
|
|
|
|
|
|
|
if self.get_option("status"):
|
|
|
|
get_servers_params["status"] = self.get_option("status")
|
|
|
|
|
|
|
|
servers = self.client.servers.get_all(**get_servers_params)
|
|
|
|
|
|
|
|
if self.get_option("network"):
|
|
|
|
servers = [s for s in servers if self.network.id in [p.network.id for p in s.private_net]]
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
if self.get_option("locations"):
|
2023-11-24 12:43:34 +00:00
|
|
|
locations: list[str] = self.get_option("locations")
|
|
|
|
servers = [s for s in servers if s.datacenter.location.name in locations]
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
if self.get_option("types"):
|
2023-11-24 12:43:34 +00:00
|
|
|
server_types: list[str] = self.get_option("types")
|
|
|
|
servers = [s for s in servers if s.server_type.name in server_types]
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
if self.get_option("images"):
|
2023-11-24 12:43:34 +00:00
|
|
|
images: list[str] = self.get_option("images")
|
|
|
|
servers = [s for s in servers if s.image is not None and s.image.os_flavor in images]
|
2022-06-13 06:57:16 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
return servers
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
def _build_inventory_server(self, server: Server) -> InventoryServer:
|
|
|
|
server_dict: InventoryServer = {}
|
|
|
|
server_dict["id"] = server.id
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["name"] = server.name
|
|
|
|
server_dict["status"] = server.status
|
2023-12-15 14:25:12 +00:00
|
|
|
|
|
|
|
# Server Type
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["type"] = server.server_type.name
|
|
|
|
server_dict["server_type"] = server.server_type.name
|
|
|
|
server_dict["architecture"] = server.server_type.architecture
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Network
|
2022-06-29 07:58:01 +00:00
|
|
|
if server.public_net.ipv4:
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["ipv4"] = server.public_net.ipv4.ip
|
2022-06-29 07:58:01 +00:00
|
|
|
|
|
|
|
if server.public_net.ipv6:
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["ipv6"] = first_ipv6_address(server.public_net.ipv6.ip)
|
|
|
|
server_dict["ipv6_network"] = server.public_net.ipv6.network
|
|
|
|
server_dict["ipv6_network_mask"] = server.public_net.ipv6.network_mask
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-08-16 09:59:17 +00:00
|
|
|
server_dict["private_networks"] = [
|
2024-03-27 13:11:30 +00:00
|
|
|
{"id": v.network.id, "name": v.network.name, "ip": v.ip} for v in server.private_net
|
2023-08-16 09:59:17 +00:00
|
|
|
]
|
2023-01-31 16:09:54 +00:00
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
if self.get_option("network"):
|
2023-11-24 12:43:34 +00:00
|
|
|
for private_net in server.private_net:
|
2023-01-31 16:09:54 +00:00
|
|
|
# Set private_ipv4 if user filtered for one network
|
2023-11-24 12:43:34 +00:00
|
|
|
if private_net.network.id == self.network.id:
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["private_ipv4"] = private_net.ip
|
2023-11-24 12:43:34 +00:00
|
|
|
break
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Datacenter
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["datacenter"] = server.datacenter.name
|
|
|
|
server_dict["location"] = server.datacenter.location.name
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Image
|
|
|
|
if server.image is not None:
|
2023-08-16 09:59:17 +00:00
|
|
|
server_dict["image_id"] = server.image.id
|
2024-03-27 13:11:30 +00:00
|
|
|
server_dict["image_os_flavor"] = server.image.os_flavor
|
|
|
|
server_dict["image_name"] = server.image.name or server.image.description
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Labels
|
2023-08-16 09:59:17 +00:00
|
|
|
server_dict["labels"] = dict(server.labels)
|
|
|
|
|
|
|
|
try:
|
|
|
|
server_dict["ansible_host"] = self._get_server_ansible_host(server)
|
2023-08-25 14:19:15 +00:00
|
|
|
except AnsibleError as exception:
|
2023-08-16 09:59:17 +00:00
|
|
|
# Log warning that for this host can not be connected to, using the
|
2023-12-21 16:49:24 +00:00
|
|
|
# method specified in 'connect_with'. Users might use 'compose' to
|
2023-08-16 09:59:17 +00:00
|
|
|
# override the connection method, or implement custom logic, so we
|
|
|
|
# do not need to abort if nothing matched.
|
2023-08-25 14:19:15 +00:00
|
|
|
self.display.v(f"[hcloud] {exception}", server.name)
|
2023-08-16 09:59:17 +00:00
|
|
|
|
|
|
|
return server_dict
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
def _get_server_ansible_host(self, server: Server):
|
2023-02-01 09:41:36 +00:00
|
|
|
if self.get_option("connect_with") == "public_ipv4":
|
|
|
|
if server.public_net.ipv4:
|
2024-03-27 13:11:30 +00:00
|
|
|
return server.public_net.ipv4.ip
|
2023-11-24 12:43:34 +00:00
|
|
|
raise AnsibleError("Server has no public ipv4, but connect_with=public_ipv4 was specified")
|
2023-02-01 09:41:36 +00:00
|
|
|
|
|
|
|
if self.get_option("connect_with") == "public_ipv6":
|
|
|
|
if server.public_net.ipv6:
|
2024-03-27 13:11:30 +00:00
|
|
|
return first_ipv6_address(server.public_net.ipv6.ip)
|
2023-11-24 12:43:34 +00:00
|
|
|
raise AnsibleError("Server has no public ipv6, but connect_with=public_ipv6 was specified")
|
2023-02-01 09:41:36 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
if self.get_option("connect_with") == "hostname":
|
2023-02-01 09:41:36 +00:00
|
|
|
# every server has a name, no need to guard this
|
2024-03-27 13:11:30 +00:00
|
|
|
return server.name
|
2023-02-01 09:41:36 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
if self.get_option("connect_with") == "ipv4_dns_ptr":
|
2023-02-01 09:41:36 +00:00
|
|
|
if server.public_net.ipv4:
|
2024-03-27 13:11:30 +00:00
|
|
|
return server.public_net.ipv4.dns_ptr
|
2023-11-24 12:43:34 +00:00
|
|
|
raise AnsibleError("Server has no public ipv4, but connect_with=ipv4_dns_ptr was specified")
|
2023-02-01 09:41:36 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
if self.get_option("connect_with") == "private_ipv4":
|
2023-02-01 09:41:36 +00:00
|
|
|
if self.get_option("network"):
|
2023-11-24 12:43:34 +00:00
|
|
|
for private_net in server.private_net:
|
|
|
|
if private_net.network.id == self.network.id:
|
2024-03-27 13:11:30 +00:00
|
|
|
return private_net.ip
|
2023-02-01 09:41:36 +00:00
|
|
|
|
|
|
|
else:
|
2023-06-27 09:50:13 +00:00
|
|
|
raise AnsibleError("You can only connect via private IPv4 if you specify a network")
|
2023-02-01 09:41:36 +00:00
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
def verify_file(self, path):
|
|
|
|
"""Return the possibly of a file being consumable by this plugin."""
|
2023-06-27 09:50:13 +00:00
|
|
|
return super().verify_file(path) and path.endswith(("hcloud.yaml", "hcloud.yml"))
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
def _get_cached_result(self, path, cache) -> tuple[list[InventoryServer], bool]:
|
2023-08-16 09:59:17 +00:00
|
|
|
# false when refresh_cache or --flush-cache is used
|
|
|
|
if not cache:
|
2023-11-24 12:43:34 +00:00
|
|
|
return [], False
|
2023-08-16 09:59:17 +00:00
|
|
|
|
|
|
|
# get the user-specified directive
|
|
|
|
if not self.get_option("cache"):
|
2023-11-24 12:43:34 +00:00
|
|
|
return [], False
|
2023-08-16 09:59:17 +00:00
|
|
|
|
|
|
|
cache_key = self.get_cache_key(path)
|
|
|
|
try:
|
|
|
|
cached_result = self._cache[cache_key]
|
|
|
|
except KeyError:
|
|
|
|
# if cache expires or cache file doesn"t exist
|
2023-11-24 12:43:34 +00:00
|
|
|
return [], False
|
2023-08-16 09:59:17 +00:00
|
|
|
|
|
|
|
return cached_result, True
|
|
|
|
|
2023-11-23 13:53:10 +00:00
|
|
|
def _update_cached_result(self, path, cache, result: list[InventoryServer]):
|
2023-08-16 09:59:17 +00:00
|
|
|
if not self.get_option("cache"):
|
|
|
|
return
|
|
|
|
|
|
|
|
cache_key = self.get_cache_key(path)
|
|
|
|
# We weren't explicitly told to flush the cache, and there's already a cache entry,
|
|
|
|
# this means that the result we're being passed came from the cache. As such we don't
|
|
|
|
# want to "update" the cache as that could reset a TTL on the cache entry.
|
|
|
|
if cache and cache_key in self._cache:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._cache[cache_key] = result
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
def parse(self, inventory, loader, path, cache=True):
|
2023-06-27 09:50:13 +00:00
|
|
|
super().parse(inventory, loader, path, cache)
|
2021-02-22 21:25:44 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
try:
|
|
|
|
client_check_required_lib()
|
|
|
|
except ClientException as exception:
|
|
|
|
raise AnsibleError(to_native(exception)) from exception
|
|
|
|
|
|
|
|
# Allow using extra variables arguments as template variables (e.g.
|
|
|
|
# '--extra-vars my_var=my_value')
|
|
|
|
self.templar.available_variables = self._vars
|
2021-02-22 21:25:44 +00:00
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
self._read_config_data(path)
|
|
|
|
self._configure_hcloud_client()
|
2023-08-16 09:59:17 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
servers, cached = self._get_cached_result(path, cache)
|
2023-08-16 09:59:17 +00:00
|
|
|
if not cached:
|
2023-11-24 12:43:34 +00:00
|
|
|
with self.client.cached_session():
|
|
|
|
servers = [self._build_inventory_server(s) for s in self._fetch_servers()]
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2022-11-08 08:01:14 +00:00
|
|
|
# Add a top group
|
|
|
|
self.inventory.add_group(group=self.get_option("group"))
|
2020-03-09 13:36:01 +00:00
|
|
|
|
feat: add `hostvars_prefix` and `hostvars_suffix` options to inventory hostvars (#423)
##### SUMMARY
Add `hostvars_prefix` and `hostvars_suffix` options to customize the
inventory host variables keys.
For example, with `hostvars_prefix: hcloud_ `, the host vars will be
stored as follows:
```json
{
"_meta": {
"hostvars": {
"tmp": {
"ansible_host": "65.109.169.27",
"hcloud_architecture": "x86",
"hcloud_datacenter": "hel1-dc2",
"hcloud_id": 40573407,
"hcloud_image_id": 114690387,
"hcloud_image_name": "debian-12",
"hcloud_image_os_flavor": "debian",
"hcloud_ipv4": "65.109.169.27",
"hcloud_ipv6_network_mask": "64",
"hcloud_ipv6_network": "2a01:4f9:c012:4377::",
"hcloud_ipv6": "2a01:4f9:c012:4377::1",
"hcloud_labels": {},
"hcloud_location": "hel1",
"hcloud_name": "tmp",
"hcloud_private_networks": [],
"hcloud_server_type": "cx11",
"hcloud_status": "running",
"hcloud_type": "cx11"
}
}
}
}
```
Related to #116
2023-12-15 14:40:37 +00:00
|
|
|
hostvars_prefix = self.get_option("hostvars_prefix")
|
|
|
|
hostvars_suffix = self.get_option("hostvars_suffix")
|
2024-02-05 13:32:01 +00:00
|
|
|
hostname_template = self.get_option("hostname")
|
feat: add `hostvars_prefix` and `hostvars_suffix` options to inventory hostvars (#423)
##### SUMMARY
Add `hostvars_prefix` and `hostvars_suffix` options to customize the
inventory host variables keys.
For example, with `hostvars_prefix: hcloud_ `, the host vars will be
stored as follows:
```json
{
"_meta": {
"hostvars": {
"tmp": {
"ansible_host": "65.109.169.27",
"hcloud_architecture": "x86",
"hcloud_datacenter": "hel1-dc2",
"hcloud_id": 40573407,
"hcloud_image_id": 114690387,
"hcloud_image_name": "debian-12",
"hcloud_image_os_flavor": "debian",
"hcloud_ipv4": "65.109.169.27",
"hcloud_ipv6_network_mask": "64",
"hcloud_ipv6_network": "2a01:4f9:c012:4377::",
"hcloud_ipv6": "2a01:4f9:c012:4377::1",
"hcloud_labels": {},
"hcloud_location": "hel1",
"hcloud_name": "tmp",
"hcloud_private_networks": [],
"hcloud_server_type": "cx11",
"hcloud_status": "running",
"hcloud_type": "cx11"
}
}
}
}
```
Related to #116
2023-12-15 14:40:37 +00:00
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
for server in servers:
|
2024-02-05 13:32:01 +00:00
|
|
|
hostvars = {}
|
2023-08-16 09:59:17 +00:00
|
|
|
for key, value in server.items():
|
feat: add `hostvars_prefix` and `hostvars_suffix` options to inventory hostvars (#423)
##### SUMMARY
Add `hostvars_prefix` and `hostvars_suffix` options to customize the
inventory host variables keys.
For example, with `hostvars_prefix: hcloud_ `, the host vars will be
stored as follows:
```json
{
"_meta": {
"hostvars": {
"tmp": {
"ansible_host": "65.109.169.27",
"hcloud_architecture": "x86",
"hcloud_datacenter": "hel1-dc2",
"hcloud_id": 40573407,
"hcloud_image_id": 114690387,
"hcloud_image_name": "debian-12",
"hcloud_image_os_flavor": "debian",
"hcloud_ipv4": "65.109.169.27",
"hcloud_ipv6_network_mask": "64",
"hcloud_ipv6_network": "2a01:4f9:c012:4377::",
"hcloud_ipv6": "2a01:4f9:c012:4377::1",
"hcloud_labels": {},
"hcloud_location": "hel1",
"hcloud_name": "tmp",
"hcloud_private_networks": [],
"hcloud_server_type": "cx11",
"hcloud_status": "running",
"hcloud_type": "cx11"
}
}
}
}
```
Related to #116
2023-12-15 14:40:37 +00:00
|
|
|
# Add hostvars prefix and suffix for variables coming from the Hetzner Cloud.
|
|
|
|
if hostvars_prefix or hostvars_suffix:
|
|
|
|
if key not in ("ansible_host",):
|
|
|
|
if hostvars_prefix:
|
|
|
|
key = hostvars_prefix + key
|
|
|
|
if hostvars_suffix:
|
|
|
|
key = key + hostvars_suffix
|
|
|
|
|
2024-02-05 13:32:01 +00:00
|
|
|
hostvars[key] = value
|
|
|
|
|
|
|
|
if hostname_template:
|
|
|
|
templar = self.templar
|
|
|
|
templar.available_variables = combine_vars(hostvars, self._vars)
|
|
|
|
hostname = templar.template(hostname_template)
|
|
|
|
else:
|
|
|
|
hostname = server["name"]
|
|
|
|
|
|
|
|
self.inventory.add_host(hostname, group=self.get_option("group"))
|
|
|
|
for key, value in hostvars.items():
|
|
|
|
self.inventory.set_variable(hostname, key, value)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Use constructed if applicable
|
2023-06-27 09:50:13 +00:00
|
|
|
strict = self.get_option("strict")
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Composed variables
|
2023-06-27 09:50:13 +00:00
|
|
|
self._set_composite_vars(
|
2023-08-16 09:59:17 +00:00
|
|
|
self.get_option("compose"),
|
2024-02-05 13:32:01 +00:00
|
|
|
self.inventory.get_host(hostname).get_vars(),
|
|
|
|
hostname,
|
2023-08-16 09:59:17 +00:00
|
|
|
strict=strict,
|
2023-06-27 09:50:13 +00:00
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
|
2023-08-16 09:59:17 +00:00
|
|
|
self._add_host_to_composed_groups(
|
|
|
|
self.get_option("groups"),
|
|
|
|
{},
|
2024-02-05 13:32:01 +00:00
|
|
|
hostname,
|
2023-08-16 09:59:17 +00:00
|
|
|
strict=strict,
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Create groups based on variable values and add the corresponding hosts to it
|
2023-08-16 09:59:17 +00:00
|
|
|
self._add_host_to_keyed_groups(
|
|
|
|
self.get_option("keyed_groups"),
|
|
|
|
{},
|
2024-02-05 13:32:01 +00:00
|
|
|
hostname,
|
2023-08-16 09:59:17 +00:00
|
|
|
strict=strict,
|
|
|
|
)
|
|
|
|
|
2023-11-24 12:43:34 +00:00
|
|
|
self._update_cached_result(path, cache, servers)
|