From 1b42d0118cedac74a386cf5d1a86796dbbd9fb8a Mon Sep 17 00:00:00 2001 From: Hoan To Date: Thu, 8 Jun 2023 11:32:55 +0000 Subject: [PATCH] Dev 1063 loadbalancer ip python --- group_vars/stage_demompmx/plain.yml | 3 +- inventory_plugins/netgo-hcloud.py | 75 +++++++++++++++++++---------- tasks/autodiscover_pre_tasks.yml | 16 ++++++ 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/group_vars/stage_demompmx/plain.yml b/group_vars/stage_demompmx/plain.yml index 7a5b969..041d372 100644 --- a/group_vars/stage_demompmx/plain.yml +++ b/group_vars/stage_demompmx/plain.yml @@ -42,5 +42,4 @@ elastic_admin_username: "{{ elastic_admin_username_vault }}" elastic_admin_password: "{{ elastic_admin_password_vault }}" shared_service_elastic_stack_01_hostname: "demompmx-connect-data.smardigo.digital:443" - -shared_service_loadbalancer_logstash_private_ip: "10.0.0.21" +shared_service_loadbalancer_logstash_private_ip: "{{ demompmx_logstash_private_ip }}" diff --git a/inventory_plugins/netgo-hcloud.py b/inventory_plugins/netgo-hcloud.py index b687a61..8dddeae 100644 --- a/inventory_plugins/netgo-hcloud.py +++ b/inventory_plugins/netgo-hcloud.py @@ -46,6 +46,7 @@ DOCUMENTATION = r""" import json import os +import re from ansible.errors import AnsibleError from ansible.module_utils.urls import open_url from ansible.module_utils._text import to_native @@ -174,7 +175,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def populate(self, servers, networks, loadbalancers): display = Display() - + temp_stage = self.get_option("stage_kube") # Add a default top group 'hcloud' self.inventory.add_group(group="hcloud") self.inventory.add_group(group="etcd") @@ -182,22 +183,36 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): loadbalancerPublicIp = "-" loadbalancerPrivateIp = "-" + extraLoadbalancers = [] # 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')) for loadbalancer in loadbalancers: loadbalancerId = loadbalancer["id"] loadbalancerName = loadbalancer["name"] loadbalancerLabels = loadbalancer["labels"] - loadbalancerPublicIp = loadbalancer["public_net"]["ipv4"]["ip"] - if len(loadbalancer["private_net"]) > 0: - loadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"] + if loadbalancerName == self.get_option("stage_kube") + "-ingress": + loadbalancerPublicIp = loadbalancer["public_net"]["ipv4"]["ip"] + if len(loadbalancer["private_net"]) > 0 : + loadbalancerPrivateIp = loadbalancer["private_net"][0]["ip"] + else: + loadbalancerPrivateIp = '-' + + display.display("loadbalancer:<" + loadbalancerName + ">, publicIp=<" + loadbalancerPublicIp + ">, privateIp=<" + loadbalancerPrivateIp + ">") 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 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 + ">") - 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="stage_" + serverStage) - - # should be configurable and not hard coded - if serverService == "kube_control_plane": - self.inventory.add_host(serverName, group="etcd") - 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, 'ansible_ssh_host', serverPublicIp) - 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_private_ingress_loadbalancer_ip', loadbalancerPrivateIp) + if len(serverService) > 0: + 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="stage_" + serverStage) + + # should be configurable and not hard coded + if serverService == "kube_control_plane": + self.inventory.add_host(serverName, group="etcd") + 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, 'ansible_ssh_host', serverPublicIp) + 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_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) diff --git a/tasks/autodiscover_pre_tasks.yml b/tasks/autodiscover_pre_tasks.yml index 16917b6..2c79fdc 100644 --- a/tasks/autodiscover_pre_tasks.yml +++ b/tasks/autodiscover_pre_tasks.yml @@ -151,6 +151,22 @@ when: - 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 - name: "Reading hetzner server infos for stage <{{ stage }}/{{ stage_kube }}>" set_fact: