Skip to content

Commit

Permalink
feat(inventory): connect via public ipv6 address (#176)
Browse files Browse the repository at this point in the history
Add a new `connect_with` option `public_ipv6` that uses the first
address from the servers publically-routed ipv6 network.
  • Loading branch information
apricote authored Dec 21, 2022
1 parent d15ad18 commit b5f2054
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelogs/fragments/inventory-connect-via-ipv6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- inventory plugin - Add new connect_with setting public_ipv6 to connect to discovered servers via public IPv6 address.
breaking_changes:
- inventory plugin - Python v3.5+ is now required.
9 changes: 8 additions & 1 deletion plugins/inventory/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- Lukas Kaemmerling (@lkaemmerling)
short_description: Ansible dynamic inventory plugin for the Hetzner Cloud.
requirements:
- python >= 2.7
- python >= 3.5
- hcloud-python >= 1.0.0
description:
- Reads inventories from the Hetzner Cloud API.
Expand Down Expand Up @@ -42,6 +42,7 @@
type: str
choices:
- public_ipv4
- public_ipv6
- hostname
- ipv4_dns_ptr
- private_ipv4
Expand Down Expand Up @@ -113,6 +114,7 @@
from ansible.module_utils._text import to_native
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
from ansible.release import __version__
from ipaddress import IPv6Network

try:
from hcloud import hcloud
Expand Down Expand Up @@ -222,6 +224,8 @@ def _set_server_attributes(self, server):

if self.get_option("connect_with") == "public_ipv4":
self.inventory.set_variable(server.name, "ansible_host", to_native(server.public_net.ipv4.ip))
if self.get_option("connect_with") == "public_ipv6":
self.inventory.set_variable(server.name, "ansible_host", to_native(self._first_ipv6_address(server.public_net.ipv6.ip)))
elif self.get_option("connect_with") == "hostname":
self.inventory.set_variable(server.name, "ansible_host", to_native(server.name))
elif self.get_option("connect_with") == "ipv4_dns_ptr":
Expand Down Expand Up @@ -259,6 +263,9 @@ def _set_server_attributes(self, server):
# Labels
self.inventory.set_variable(server.name, "labels", dict(server.labels))

def _first_ipv6_address(self, network):
return next(IPv6Network(network).hosts())

def verify_file(self, path):
"""Return the possibly of a file being consumable by this plugin."""
return (
Expand Down

0 comments on commit b5f2054

Please sign in to comment.