mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
feat(inventory): connect via public ipv6 address (#176)
Add a new `connect_with` option `public_ipv6` that uses the first address from the servers publically-routed ipv6 network.
This commit is contained in:
parent
d15ad18803
commit
b5f205485a
2 changed files with 12 additions and 1 deletions
4
changelogs/fragments/inventory-connect-via-ipv6.yml
Normal file
4
changelogs/fragments/inventory-connect-via-ipv6.yml
Normal file
|
|
@ -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.
|
||||
|
|
@ -11,7 +11,7 @@ DOCUMENTATION = r'''
|
|||
- 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.
|
||||
|
|
@ -42,6 +42,7 @@ DOCUMENTATION = r'''
|
|||
type: str
|
||||
choices:
|
||||
- public_ipv4
|
||||
- public_ipv6
|
||||
- hostname
|
||||
- ipv4_dns_ptr
|
||||
- private_ipv4
|
||||
|
|
@ -113,6 +114,7 @@ from ansible.errors import AnsibleError
|
|||
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
|
||||
|
|
@ -222,6 +224,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
|||
|
||||
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":
|
||||
|
|
@ -259,6 +263,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
|||
# 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 (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue