Dev 1063 loadbalancer ip python

qa
Hoan To 3 years ago committed by Michael Hähnel
parent 671963edcb
commit 1b42d0118c

@ -42,5 +42,4 @@ elastic_admin_username: "{{ elastic_admin_username_vault }}"
elastic_admin_password: "{{ elastic_admin_password_vault }}" elastic_admin_password: "{{ elastic_admin_password_vault }}"
shared_service_elastic_stack_01_hostname: "demompmx-connect-data.smardigo.digital:443" shared_service_elastic_stack_01_hostname: "demompmx-connect-data.smardigo.digital:443"
shared_service_loadbalancer_logstash_private_ip: "{{ demompmx_logstash_private_ip }}"
shared_service_loadbalancer_logstash_private_ip: "10.0.0.21"

@ -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"]
loadbalancerPublicIp = loadbalancer["public_net"]["ipv4"]["ip"] if loadbalancerName == self.get_option("stage_kube") + "-ingress":
if len(loadbalancer["private_net"]) > 0: loadbalancerPublicIp = loadbalancer["public_net"]["ipv4"]["ip"]
loadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"] if len(loadbalancer["private_net"]) > 0 :
loadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"]
else:
loadbalancerPrivateIp = '-'
display.display("loadbalancer:<" + loadbalancerName + ">, publicIp=<" + loadbalancerPublicIp + ">, privateIp=<" + loadbalancerPrivateIp + ">")
else: else:
loadbalancerPrivateIp = '-' extraLoadbalancers.append(loadbalancer)
if len(loadbalancer["private_net"]) > 0 :
extraLoadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"]
else:
extraLoadbalancerPrivateIp = '-'
display.display("loadbalancer:<" + loadbalancerName + ">, publicIp=<" + loadbalancerPublicIp + ">, privateIp=<" + loadbalancerPrivateIp + ">") 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,22 +237,30 @@ 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 + ">")
self.inventory.add_group(group=serverService) if len(serverService) > 0:
self.inventory.add_group(group="stage_" + serverStage) self.inventory.add_group(group=serverService)
self.inventory.add_group(group="stage_" + serverStage)
self.inventory.add_host(serverName, group="hcloud")
self.inventory.add_host(serverName, group=serverService) self.inventory.add_host(serverName, group="hcloud")
self.inventory.add_host(serverName, group="stage_" + serverStage) self.inventory.add_host(serverName, group=serverService)
self.inventory.add_host(serverName, group="stage_" + serverStage)
# should be configurable and not hard coded
if serverService == "kube_control_plane": # should be configurable and not hard coded
self.inventory.add_host(serverName, group="etcd") if serverService == "kube_control_plane":
if serverService == "kube_control_plane" or serverService == "kube_node": self.inventory.add_host(serverName, group="etcd")
self.inventory.add_host(serverName, group="k8s_cluster") if serverService == "kube_control_plane" or serverService == "kube_node":
self.inventory.add_host(serverName, group="k8s_cluster")
self.inventory.set_variable(serverName, 'stage_server_id', serverId)
self.inventory.set_variable(serverName, 'stage_server_ip', serverPublicIp) self.inventory.set_variable(serverName, 'stage_server_id', serverId)
self.inventory.set_variable(serverName, 'ansible_ssh_host', serverPublicIp) self.inventory.set_variable(serverName, 'stage_server_ip', serverPublicIp)
self.inventory.set_variable(serverName, 'stage_private_server_ip', serverPrivateIp) self.inventory.set_variable(serverName, 'ansible_ssh_host', serverPublicIp)
self.inventory.set_variable(serverName, 'stage_public_ingress_loadbalancer_ip', loadbalancerPublicIp) self.inventory.set_variable(serverName, 'stage_private_server_ip', serverPrivateIp)
self.inventory.set_variable(serverName, 'stage_private_ingress_loadbalancer_ip', loadbalancerPrivateIp) self.inventory.set_variable(serverName, 'stage_public_ingress_loadbalancer_ip', loadbalancerPublicIp)
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)

@ -151,6 +151,22 @@
when: when:
- debug - debug
- name: "Reading public ips of hetzner load balancers for stage <{{ stage_kube }}>"
set_fact:
"{{ item.name | replace('-','_') }}_public_ip": "{{ item.public_net.ipv4.ip }}"
loop: "{{ hetzner_loadbalancers.json.load_balancers | selectattr('name', 'match', '^' + stage_kube + '-.*') | list }}"
delegate_to: 127.0.0.1
tags:
- always
- name: "Reading private ips of hetzner load balancers for stage <{{ stage_kube }}>"
set_fact:
"{{ item.name | replace('-','_') }}_private_ip": "{{ item.private_net[0].ip if item.private_net | length > 0 else 'Not Available' }}"
loop: "{{ hetzner_loadbalancers.json.load_balancers | selectattr('name', 'match', '^' + stage_kube + '-.*') | list }}"
delegate_to: 127.0.0.1
tags:
- always
# 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
- name: "Reading hetzner server infos for stage <{{ stage }}/{{ stage_kube }}>" - name: "Reading hetzner server infos for stage <{{ stage }}/{{ stage_kube }}>"
set_fact: set_fact:

Loading…
Cancel
Save