From 83094a121d3f73f7bf31fcc48f820555122ada65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Tue, 31 Jan 2023 17:09:54 +0100 Subject: [PATCH] feat(inventory): add all private networks as variables (#186) This can be used in the `compose`, `groups` and `keyed_groups` settings to dynamically build the inventory. It also makes it possible for the user to override the `ansible_host` in `compose`, based on wether or not the server has a public ipv4 address, with fallback to the address in a specific private network. --- .../fragments/inventory-private-network-info.yml | 2 ++ plugins/inventory/hcloud.py | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 changelogs/fragments/inventory-private-network-info.yml diff --git a/changelogs/fragments/inventory-private-network-info.yml b/changelogs/fragments/inventory-private-network-info.yml new file mode 100644 index 0000000..1c4a545 --- /dev/null +++ b/changelogs/fragments/inventory-private-network-info.yml @@ -0,0 +1,2 @@ +minor_changes: + - inventory plugin - Add list of all private networks to server variables. diff --git a/plugins/inventory/hcloud.py b/plugins/inventory/hcloud.py index ab2d79f..bba015d 100644 --- a/plugins/inventory/hcloud.py +++ b/plugins/inventory/hcloud.py @@ -222,8 +222,18 @@ class InventoryModule(BaseInventoryPlugin, Constructable): self.inventory.set_variable(server.name, "ipv6_network", to_native(server.public_net.ipv6.network)) self.inventory.set_variable(server.name, "ipv6_network_mask", to_native(server.public_net.ipv6.network_mask)) + self.inventory.set_variable( + server.name, + "private_networks", + [ + {"name": n.network.name, "id": n.network.id, "ip": n.ip} + for n in server.private_net + ], + ) + if self.get_option("network"): for server_private_network in server.private_net: + # Set private_ipv4 if user filtered for one network if server_private_network.network.id == self.network.id: self.inventory.set_variable(server.name, "private_ipv4", to_native(server_private_network.ip))