|
|
|
@ -46,6 +46,7 @@ DOCUMENTATION = r"""
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
import re
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.module_utils.urls import open_url
|
|
|
|
from ansible.module_utils.urls import open_url
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
from ansible.module_utils._text import to_native
|
|
|
|
@ -174,7 +175,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|
|
|
|
|
|
|
|
|
|
|
def populate(self, servers, networks, loadbalancers):
|
|
|
|
def populate(self, servers, networks, loadbalancers):
|
|
|
|
display = Display()
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
temp_stage = self.get_option("stage_kube")
|
|
|
|
# Add a default top group 'hcloud'
|
|
|
|
# Add a default top group 'hcloud'
|
|
|
|
self.inventory.add_group(group="hcloud")
|
|
|
|
self.inventory.add_group(group="hcloud")
|
|
|
|
self.inventory.add_group(group="etcd")
|
|
|
|
self.inventory.add_group(group="etcd")
|
|
|
|
@ -182,22 +183,36 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|
|
|
|
|
|
|
|
|
|
|
loadbalancerPublicIp = "-"
|
|
|
|
loadbalancerPublicIp = "-"
|
|
|
|
loadbalancerPrivateIp = "-"
|
|
|
|
loadbalancerPrivateIp = "-"
|
|
|
|
|
|
|
|
extraLoadbalancers = []
|
|
|
|
|
|
|
|
|
|
|
|
# filter all loadbalancers by naming convention -> {{ stage_kube }}-ingress
|
|
|
|
# filter all loadbalancers by naming convention -> {{ stage_kube }}-ingress
|
|
|
|
loadbalancers = [x for x in loadbalancers if x["name"] == self.get_option("stage_kube") + "-ingress"]
|
|
|
|
#loadbalancers = [x for x in loadbalancers if x["name"] == self.get_option("stage_kube") + "-ingress"]
|
|
|
|
|
|
|
|
pattern = rf'{temp_stage}-.*'
|
|
|
|
|
|
|
|
loadbalancers = [x for x in loadbalancers if re.match(pattern, x["name"])]
|
|
|
|
loadbalancers.sort(key=lambda x: x.get('name'))
|
|
|
|
loadbalancers.sort(key=lambda x: x.get('name'))
|
|
|
|
|
|
|
|
|
|
|
|
for loadbalancer in loadbalancers:
|
|
|
|
for loadbalancer in loadbalancers:
|
|
|
|
loadbalancerId = loadbalancer["id"]
|
|
|
|
loadbalancerId = loadbalancer["id"]
|
|
|
|
loadbalancerName = loadbalancer["name"]
|
|
|
|
loadbalancerName = loadbalancer["name"]
|
|
|
|
loadbalancerLabels = loadbalancer["labels"]
|
|
|
|
loadbalancerLabels = loadbalancer["labels"]
|
|
|
|
|
|
|
|
if loadbalancerName == self.get_option("stage_kube") + "-ingress":
|
|
|
|
loadbalancerPublicIp = loadbalancer["public_net"]["ipv4"]["ip"]
|
|
|
|
loadbalancerPublicIp = loadbalancer["public_net"]["ipv4"]["ip"]
|
|
|
|
if len(loadbalancer["private_net"]) > 0:
|
|
|
|
if len(loadbalancer["private_net"]) > 0 :
|
|
|
|
loadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"]
|
|
|
|
loadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
loadbalancerPrivateIp = '-'
|
|
|
|
loadbalancerPrivateIp = '-'
|
|
|
|
|
|
|
|
|
|
|
|
display.display("loadbalancer:<" + loadbalancerName + ">, publicIp=<" + loadbalancerPublicIp + ">, privateIp=<" + loadbalancerPrivateIp + ">")
|
|
|
|
display.display("loadbalancer:<" + loadbalancerName + ">, publicIp=<" + loadbalancerPublicIp + ">, privateIp=<" + loadbalancerPrivateIp + ">")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
extraLoadbalancers.append(loadbalancer)
|
|
|
|
|
|
|
|
if len(loadbalancer["private_net"]) > 0 :
|
|
|
|
|
|
|
|
extraLoadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
extraLoadbalancerPrivateIp = '-'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
display.display("loadbalancer:<" + loadbalancerName + ">, publicIp=<" + loadbalancerPublicIp + ">, privateIp=<" + extraLoadbalancerPrivateIp + ">")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# due to a hetzner api bug for label selector: only last given selector is used - label_selector=stage=XXX,!manual not working correctly
|
|
|
|
# due to a hetzner api bug for label selector: only last given selector is used - label_selector=stage=XXX,!manual not working correctly
|
|
|
|
servers = [x for x in servers if 'manual' not in x["labels"]]
|
|
|
|
servers = [x for x in servers if 'manual' not in x["labels"]]
|
|
|
|
@ -222,6 +237,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|
|
|
|
|
|
|
|
|
|
|
display.display("id: <" + str(serverId) + ">, server:<" + serverName + ">, stage=<" + serverStage + ">, service=<" + serverService + ">, publicIp=<" + serverPublicIp + ">, privateIp=<" + serverPrivateIp + ">, publicIngressLBIp=<" + loadbalancerPublicIp + ">, privateIngressLBIp=<" + loadbalancerPrivateIp + ">")
|
|
|
|
display.display("id: <" + str(serverId) + ">, server:<" + serverName + ">, stage=<" + serverStage + ">, service=<" + serverService + ">, publicIp=<" + serverPublicIp + ">, privateIp=<" + serverPrivateIp + ">, publicIngressLBIp=<" + loadbalancerPublicIp + ">, privateIngressLBIp=<" + loadbalancerPrivateIp + ">")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(serverService) > 0:
|
|
|
|
self.inventory.add_group(group=serverService)
|
|
|
|
self.inventory.add_group(group=serverService)
|
|
|
|
self.inventory.add_group(group="stage_" + serverStage)
|
|
|
|
self.inventory.add_group(group="stage_" + serverStage)
|
|
|
|
|
|
|
|
|
|
|
|
@ -241,3 +257,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|
|
|
self.inventory.set_variable(serverName, 'stage_private_server_ip', serverPrivateIp)
|
|
|
|
self.inventory.set_variable(serverName, 'stage_private_server_ip', serverPrivateIp)
|
|
|
|
self.inventory.set_variable(serverName, 'stage_public_ingress_loadbalancer_ip', loadbalancerPublicIp)
|
|
|
|
self.inventory.set_variable(serverName, 'stage_public_ingress_loadbalancer_ip', loadbalancerPublicIp)
|
|
|
|
self.inventory.set_variable(serverName, 'stage_private_ingress_loadbalancer_ip', loadbalancerPrivateIp)
|
|
|
|
self.inventory.set_variable(serverName, 'stage_private_ingress_loadbalancer_ip', loadbalancerPrivateIp)
|
|
|
|
|
|
|
|
for extraLoadbalancer in extraLoadbalancers:
|
|
|
|
|
|
|
|
self.inventory.set_variable(serverName, f'stage_public_{extraLoadbalancer["name"]}_loadbalancer_ip', extraLoadbalancer["public_net"]["ipv4"]["ip"])
|
|
|
|
|
|
|
|
if len(loadbalancer["private_net"]) > 0 :
|
|
|
|
|
|
|
|
extraLoadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
extraLoadbalancerPrivateIp = '-'
|
|
|
|
|
|
|
|
self.inventory.set_variable(serverName, f'stage_private_{extraLoadbalancer["name"]}_loadbalancer_ip', extraLoadbalancerPrivateIp)
|
|
|
|
|