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-06-27 09:50:13 +00:00
|
|
|
DOCUMENTATION = r"""
|
2020-03-09 13:36:01 +00:00
|
|
|
name: hcloud
|
|
|
|
author:
|
|
|
|
- Lukas Kaemmerling (@lkaemmerling)
|
|
|
|
short_description: Ansible dynamic inventory plugin for the Hetzner Cloud.
|
|
|
|
requirements:
|
2022-12-21 10:10:58 +00:00
|
|
|
- python >= 3.5
|
2020-03-09 13:36:01 +00:00
|
|
|
- hcloud-python >= 1.0.0
|
|
|
|
description:
|
|
|
|
- Reads inventories from the Hetzner Cloud API.
|
|
|
|
- Uses a YAML configuration file that ends with hcloud.(yml|yaml).
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- constructed
|
|
|
|
options:
|
|
|
|
plugin:
|
|
|
|
description: marks this as an instance of the "hcloud" plugin
|
|
|
|
required: true
|
2021-05-27 06:14:13 +00:00
|
|
|
choices: ["hcloud", "hetzner.hcloud.hcloud"]
|
2020-03-09 13:36:01 +00:00
|
|
|
token:
|
|
|
|
description: The Hetzner Cloud API Token.
|
2020-12-01 07:39:08 +00:00
|
|
|
required: false
|
2022-11-08 08:01:14 +00:00
|
|
|
group:
|
|
|
|
description: The group all servers are automatically added to.
|
|
|
|
default: hcloud
|
|
|
|
type: str
|
|
|
|
required: false
|
2020-12-01 07:39:08 +00:00
|
|
|
token_env:
|
|
|
|
description: Environment variable to load the Hetzner Cloud API Token from.
|
|
|
|
default: HCLOUD_TOKEN
|
|
|
|
type: str
|
|
|
|
required: false
|
2020-03-09 13:36:01 +00:00
|
|
|
connect_with:
|
2023-02-01 09:41:36 +00:00
|
|
|
description: |
|
|
|
|
Connect to the server using the value from this field. This sets the `ansible_host`
|
|
|
|
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,
|
|
|
|
you can use `compose` top-level key.
|
2020-03-09 13:36:01 +00:00
|
|
|
default: public_ipv4
|
|
|
|
type: str
|
|
|
|
choices:
|
|
|
|
- public_ipv4
|
2022-12-21 10:10:58 +00:00
|
|
|
- public_ipv6
|
2020-03-09 13:36:01 +00:00
|
|
|
- hostname
|
|
|
|
- ipv4_dns_ptr
|
2020-08-31 10:50:12 +00:00
|
|
|
- private_ipv4
|
2020-03-09 13:36:01 +00:00
|
|
|
locations:
|
|
|
|
description: Populate inventory with instances in this location.
|
|
|
|
default: []
|
|
|
|
type: list
|
2022-04-22 07:35:17 +00:00
|
|
|
elements: str
|
2020-03-09 13:36:01 +00:00
|
|
|
required: false
|
|
|
|
types:
|
|
|
|
description: Populate inventory with instances with this type.
|
|
|
|
default: []
|
|
|
|
type: list
|
2022-04-22 07:35:17 +00:00
|
|
|
elements: str
|
2020-03-09 13:36:01 +00:00
|
|
|
required: false
|
|
|
|
images:
|
|
|
|
description: Populate inventory with instances with this image name, only available for system images.
|
|
|
|
default: []
|
|
|
|
type: list
|
2022-04-22 07:35:17 +00:00
|
|
|
elements: str
|
2020-03-09 13:36:01 +00:00
|
|
|
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
|
2022-06-13 06:57:16 +00:00
|
|
|
status:
|
|
|
|
description: Populate inventory with instances with this status.
|
|
|
|
default: []
|
|
|
|
type: list
|
|
|
|
elements: str
|
|
|
|
required: false
|
2023-06-27 09:50:13 +00:00
|
|
|
"""
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
EXAMPLES = r"""
|
|
|
|
# Minimal example. `HCLOUD_TOKEN` is exposed in environment.
|
|
|
|
plugin: hcloud
|
|
|
|
|
2023-01-30 11:08:09 +00:00
|
|
|
# Example with templated token, e.g. provided through extra vars.
|
|
|
|
plugin: hcloud
|
|
|
|
token: "{{ hetzner_apitoken }}"
|
|
|
|
|
2022-06-13 06:57:16 +00:00
|
|
|
# Example with locations, types, status and token
|
2020-03-09 13:36:01 +00:00
|
|
|
plugin: hcloud
|
|
|
|
token: foobar
|
|
|
|
locations:
|
|
|
|
- nbg1
|
|
|
|
types:
|
|
|
|
- cx11
|
2022-06-13 06:57:16 +00:00
|
|
|
status:
|
|
|
|
- running
|
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"
|
|
|
|
plugin: hcloud
|
|
|
|
keyed_groups:
|
|
|
|
- key: location
|
|
|
|
prefix: hcloud_location
|
|
|
|
- key: image_os_flavor
|
|
|
|
separator: ""
|
|
|
|
- key: status
|
|
|
|
prefix: server_status
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
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-06-27 11:17:21 +00:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 13:36:01 +00:00
|
|
|
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
|
|
|
|
from ansible.release import __version__
|
|
|
|
|
|
|
|
try:
|
2023-06-27 09:50:13 +00:00
|
|
|
from hcloud import APIException, hcloud
|
|
|
|
|
2021-02-22 21:25:44 +00:00
|
|
|
HAS_HCLOUD = True
|
2020-03-09 13:36:01 +00:00
|
|
|
except ImportError:
|
2021-02-22 21:25:44 +00:00
|
|
|
HAS_HCLOUD = False
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InventoryModule(BaseInventoryPlugin, Constructable):
|
2023-06-27 09:50:13 +00:00
|
|
|
NAME = "hetzner.hcloud.hcloud"
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def _configure_hcloud_client(self):
|
2020-12-01 07:39:08 +00:00
|
|
|
self.token_env = self.get_option("token_env")
|
2023-01-30 11:08:09 +00:00
|
|
|
self.templar.available_variables = self._vars
|
2023-06-27 09:50:13 +00:00
|
|
|
self.api_token = self.templar.template(self.get_option("token"), fail_on_undefined=False) or os.getenv(
|
|
|
|
self.token_env
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
if self.api_token is None:
|
|
|
|
raise AnsibleError(
|
2020-12-01 07:39:08 +00:00
|
|
|
"Please specify a token, via the option token, via environment variable HCLOUD_TOKEN "
|
|
|
|
"or via custom environment variable set by token_env option."
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
self.endpoint = os.getenv("HCLOUD_ENDPOINT") or "https://api.hetzner.cloud/v1"
|
|
|
|
|
2023-06-27 09:50:13 +00:00
|
|
|
self.client = hcloud.Client(
|
|
|
|
token=self.api_token,
|
|
|
|
api_endpoint=self.endpoint,
|
|
|
|
application_name="ansible-inventory",
|
|
|
|
application_version=__version__,
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def _test_hcloud_token(self):
|
|
|
|
try:
|
|
|
|
# We test the API Token against the location API, because this is the API with the smallest result
|
|
|
|
# and not controllable from the customer.
|
|
|
|
self.client.locations.get_all()
|
2020-11-24 05:39:21 +00:00
|
|
|
except APIException:
|
2020-03-09 13:36:01 +00:00
|
|
|
raise AnsibleError("Invalid Hetzner Cloud API Token.")
|
|
|
|
|
|
|
|
def _get_servers(self):
|
|
|
|
if len(self.get_option("label_selector")) > 0:
|
|
|
|
self.servers = self.client.servers.get_all(label_selector=self.get_option("label_selector"))
|
|
|
|
else:
|
|
|
|
self.servers = self.client.servers.get_all()
|
|
|
|
|
|
|
|
def _filter_servers(self):
|
|
|
|
if self.get_option("network"):
|
2023-06-27 09:50:13 +00:00
|
|
|
network = self.templar.template(self.get_option("network"), fail_on_undefined=False) or self.get_option(
|
|
|
|
"network"
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
try:
|
2021-08-25 08:57:03 +00:00
|
|
|
self.network = self.client.networks.get_by_name(network)
|
2020-03-09 13:36:01 +00:00
|
|
|
if self.network is None:
|
2021-08-25 08:57:03 +00:00
|
|
|
self.network = self.client.networks.get_by_id(network)
|
2020-11-24 05:39:21 +00:00
|
|
|
except APIException:
|
2023-06-27 09:50:13 +00:00
|
|
|
raise AnsibleError("The given network is not found.")
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
tmp = []
|
|
|
|
for server in self.servers:
|
|
|
|
for server_private_network in server.private_net:
|
|
|
|
if server_private_network.network.id == self.network.id:
|
|
|
|
tmp.append(server)
|
|
|
|
self.servers = tmp
|
|
|
|
|
|
|
|
if self.get_option("locations"):
|
|
|
|
tmp = []
|
|
|
|
for server in self.servers:
|
|
|
|
if server.datacenter.location.name in self.get_option("locations"):
|
|
|
|
tmp.append(server)
|
|
|
|
self.servers = tmp
|
|
|
|
|
|
|
|
if self.get_option("types"):
|
|
|
|
tmp = []
|
|
|
|
for server in self.servers:
|
|
|
|
if server.server_type.name in self.get_option("types"):
|
|
|
|
tmp.append(server)
|
|
|
|
self.servers = tmp
|
|
|
|
|
|
|
|
if self.get_option("images"):
|
|
|
|
tmp = []
|
|
|
|
for server in self.servers:
|
|
|
|
if server.image is not None and server.image.os_flavor in self.get_option("images"):
|
|
|
|
tmp.append(server)
|
|
|
|
self.servers = tmp
|
2022-06-13 06:57:16 +00:00
|
|
|
|
|
|
|
if self.get_option("status"):
|
|
|
|
tmp = []
|
|
|
|
for server in self.servers:
|
|
|
|
if server.status in self.get_option("status"):
|
|
|
|
tmp.append(server)
|
|
|
|
self.servers = tmp
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
def _set_server_attributes(self, server):
|
|
|
|
self.inventory.set_variable(server.name, "id", to_native(server.id))
|
|
|
|
self.inventory.set_variable(server.name, "name", to_native(server.name))
|
|
|
|
self.inventory.set_variable(server.name, "status", to_native(server.status))
|
|
|
|
self.inventory.set_variable(server.name, "type", to_native(server.server_type.name))
|
2023-04-12 08:43:19 +00:00
|
|
|
self.inventory.set_variable(server.name, "architecture", to_native(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:
|
|
|
|
self.inventory.set_variable(server.name, "ipv4", to_native(server.public_net.ipv4.ip))
|
|
|
|
|
|
|
|
if server.public_net.ipv6:
|
|
|
|
self.inventory.set_variable(server.name, "ipv6_network", to_native(server.public_net.ipv6.network))
|
2023-06-27 09:50:13 +00:00
|
|
|
self.inventory.set_variable(
|
|
|
|
server.name, "ipv6_network_mask", to_native(server.public_net.ipv6.network_mask)
|
|
|
|
)
|
|
|
|
self.inventory.set_variable(
|
|
|
|
server.name, "ipv6", to_native(self._first_ipv6_address(server.public_net.ipv6.ip))
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
2023-01-31 16:09:54 +00:00
|
|
|
self.inventory.set_variable(
|
|
|
|
server.name,
|
|
|
|
"private_networks",
|
2023-06-27 09:50:13 +00:00
|
|
|
[{"name": n.network.name, "id": n.network.id, "ip": n.ip} for n in server.private_net],
|
2023-01-31 16:09:54 +00:00
|
|
|
)
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
if self.get_option("network"):
|
|
|
|
for server_private_network in server.private_net:
|
2023-01-31 16:09:54 +00:00
|
|
|
# Set private_ipv4 if user filtered for one network
|
2020-03-09 13:36:01 +00:00
|
|
|
if server_private_network.network.id == self.network.id:
|
|
|
|
self.inventory.set_variable(server.name, "private_ipv4", to_native(server_private_network.ip))
|
|
|
|
|
2023-02-01 09:41:36 +00:00
|
|
|
try:
|
|
|
|
self.inventory.set_variable(server.name, "ansible_host", self._get_server_ansible_host(server))
|
|
|
|
except AnsibleError as e:
|
|
|
|
# Log warning that for this host can not be connected to, using the
|
|
|
|
# method specified in `connect_with`. Users might use `compose` to
|
|
|
|
# override the connection method, or implement custom logic, so we
|
|
|
|
# do not need to abort if nothing matched.
|
|
|
|
self.display.v("[hcloud] %s" % e, server.name)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Server Type
|
2021-04-14 07:57:35 +00:00
|
|
|
if server.server_type is not None:
|
|
|
|
self.inventory.set_variable(server.name, "server_type", to_native(server.server_type.name))
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Datacenter
|
|
|
|
self.inventory.set_variable(server.name, "datacenter", to_native(server.datacenter.name))
|
|
|
|
self.inventory.set_variable(server.name, "location", to_native(server.datacenter.location.name))
|
|
|
|
|
|
|
|
# Image
|
|
|
|
if server.image is not None:
|
|
|
|
self.inventory.set_variable(server.name, "image_id", to_native(server.image.id))
|
|
|
|
self.inventory.set_variable(server.name, "image_os_flavor", to_native(server.image.os_flavor))
|
|
|
|
if server.image.name is not None:
|
|
|
|
self.inventory.set_variable(server.name, "image_name", to_native(server.image.name))
|
|
|
|
else:
|
|
|
|
self.inventory.set_variable(server.name, "image_name", to_native(server.image.description))
|
|
|
|
else:
|
|
|
|
self.inventory.set_variable(server.name, "image_id", to_native("No Image ID found"))
|
|
|
|
self.inventory.set_variable(server.name, "image_name", to_native("No Image Name found"))
|
|
|
|
self.inventory.set_variable(server.name, "image_os_flavor", to_native("No Image OS Flavor found"))
|
|
|
|
|
|
|
|
# Labels
|
|
|
|
self.inventory.set_variable(server.name, "labels", dict(server.labels))
|
|
|
|
|
2023-02-01 09:41:36 +00:00
|
|
|
def _get_server_ansible_host(self, server):
|
|
|
|
if self.get_option("connect_with") == "public_ipv4":
|
|
|
|
if server.public_net.ipv4:
|
|
|
|
return to_native(server.public_net.ipv4.ip)
|
|
|
|
else:
|
|
|
|
raise AnsibleError("Server has no public ipv4, but connect_with=public_ipv4 was specified")
|
|
|
|
|
|
|
|
if self.get_option("connect_with") == "public_ipv6":
|
|
|
|
if server.public_net.ipv6:
|
|
|
|
return to_native(self._first_ipv6_address(server.public_net.ipv6.ip))
|
|
|
|
else:
|
|
|
|
raise AnsibleError("Server has no public ipv6, but connect_with=public_ipv6 was specified")
|
|
|
|
|
|
|
|
elif self.get_option("connect_with") == "hostname":
|
|
|
|
# every server has a name, no need to guard this
|
|
|
|
return to_native(server.name)
|
|
|
|
|
|
|
|
elif self.get_option("connect_with") == "ipv4_dns_ptr":
|
|
|
|
if server.public_net.ipv4:
|
|
|
|
return to_native(server.public_net.ipv4.dns_ptr)
|
|
|
|
else:
|
|
|
|
raise AnsibleError("Server has no public ipv4, but connect_with=ipv4_dns_ptr was specified")
|
|
|
|
|
|
|
|
elif self.get_option("connect_with") == "private_ipv4":
|
|
|
|
if self.get_option("network"):
|
|
|
|
for server_private_network in server.private_net:
|
|
|
|
if server_private_network.network.id == self.network.id:
|
|
|
|
return to_native(server_private_network.ip)
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-12-21 10:10:58 +00:00
|
|
|
def _first_ipv6_address(self, network):
|
|
|
|
return next(IPv6Network(network).hosts())
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if not HAS_HCLOUD:
|
|
|
|
raise AnsibleError("The Hetzner Cloud dynamic inventory plugin requires hcloud-python.")
|
|
|
|
|
2020-03-09 13:36:01 +00:00
|
|
|
self._read_config_data(path)
|
|
|
|
self._configure_hcloud_client()
|
|
|
|
self._test_hcloud_token()
|
|
|
|
self._get_servers()
|
|
|
|
self._filter_servers()
|
|
|
|
|
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
|
|
|
|
|
|
|
for server in self.servers:
|
2022-11-08 08:01:14 +00:00
|
|
|
self.inventory.add_host(server.name, group=self.get_option("group"))
|
2020-03-09 13:36:01 +00:00
|
|
|
self._set_server_attributes(server)
|
|
|
|
|
|
|
|
# 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(
|
|
|
|
self.get_option("compose"), self.inventory.get_host(server.name).get_vars(), server.name, strict=strict
|
|
|
|
)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
|
2023-06-27 09:50:13 +00:00
|
|
|
self._add_host_to_composed_groups(self.get_option("groups"), {}, server.name, strict=strict)
|
2020-03-09 13:36:01 +00:00
|
|
|
|
|
|
|
# Create groups based on variable values and add the corresponding hosts to it
|
2023-06-27 09:50:13 +00:00
|
|
|
self._add_host_to_keyed_groups(self.get_option("keyed_groups"), {}, server.name, strict=strict)
|