Skip to content

Commit

Permalink
feat(inventory): add all private networks as variables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apricote committed Jan 31, 2023
1 parent ccfa51b commit b491741
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/inventory-private-network-info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- inventory plugin - Add list of all private networks to server variables.
11 changes: 11 additions & 0 deletions plugins/inventory/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,19 @@ def _set_server_attributes(self, server):
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))

Expand Down

0 comments on commit b491741

Please sign in to comment.