From b9f753fa92971013e38ac6f161d856cac169945c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4hnel?= Date: Mon, 20 Mar 2023 13:36:58 +0000 Subject: [PATCH] DEV-720 Recreate feature branch for new playbook --- dump-hcloud-ips.yml | 96 +++++++++++++++++++ .../tasks/_read_load_balancer_infos.yml | 31 ++++++ 2 files changed, 127 insertions(+) create mode 100644 dump-hcloud-ips.yml create mode 100644 roles/hcloud/tasks/_read_load_balancer_infos.yml diff --git a/dump-hcloud-ips.yml b/dump-hcloud-ips.yml new file mode 100644 index 0000000..aadf22b --- /dev/null +++ b/dump-hcloud-ips.yml @@ -0,0 +1,96 @@ + +--- + +# This playbook dumps all ip addresses from Hetzner Cloud saving it into local file. +# A prefix "route" will be added, to easily use it within custom ovpn configuration. +# +# Parameters: +# playbook inventory +# stage := the name of the stage (e.g. dev, int, qa, prod) +# Example: +# STAGE=ext && ansible-playbook dump-hcloud-ips.yml -e "stage=${STAGE}" --vault-password-file=~/.ansible-vault-pass-${STAGE} + +############################################################# +# Creating inventory dynamically for given parameters # +############################################################# + +- hosts: localhost + connection: local + gather_facts: false + + pre_tasks: + - name: "Check if ansible version is at least 2.10.x" + assert: + that: + - ansible_version.major >= 2 + - ansible_version.minor >= 10 + msg: "The ansible version has to be at least ({{ ansible_version.full }})" + +# Add virtual server to load stage specific variables as context + - name: "Add <{{ stage }}-virtual-host-to-read-groups-vars> to hosts" + add_host: + name: "{{ stage }}-virtual-host-to-read-groups-vars" + groups: + - "stage_{{ stage }}" + changed_when: False + +############################################################# +# Dumping ip addresses from hcloud with given stage # +############################################################# + +- hosts: "{{ stage }}-virtual-host-to-read-groups-vars" + serial: "{{ serial_number | default(1) }}" + gather_facts: false + connection: local + + pre_tasks: + - name: "Reading current server groups from hetzner" + include_role: + name: hcloud + tasks_from: _read_server_infos + with_items: [ + { + name: "all", + label_selector: "stage={{ stage }}", + } + ] + loop_control: + loop_var: current_server_group + + - name: "Reading info about current loadbalancers from hetzner" + include_role: + name: hcloud + tasks_from: _read_load_balancer_infos + with_items: [ + { + name: "all", + #label_selector: "stage={{ stage }}", # There are no useful labels at the moment. Todo: Create labels for load balancers like stage=dev + } + ] + loop_control: + loop_var: current_load_balancer_group + + tasks: + - name: 'Save Hetzner Server ip adresses in ~/hcloud_ip_addresses.txt' + blockinfile: + marker: "## {mark} managed by ansible (hosts config for {{ stage }}) ##" + path: '~/hcloud_ip_addresses.txt' + state: present + create: yes + block: | + {% for host in server_group_infos_all %} + # {{ host.name }} + route {{ host.ip }} + {% endfor %} + + - name: 'Save Hetzner loadbalancer ip adresses in ~/hcloud_ip_addresses.txt' + blockinfile: + marker: "## {mark} managed by ansible (load balancer config for {{ stage }}) ##" + path: '~/hcloud_ip_addresses.txt' + state: present + create: yes + block: | + {% for host in load_balancer_group_infos_all %} + # {{ host.name }} + route {{ host.ip }} + {% endfor %} diff --git a/roles/hcloud/tasks/_read_load_balancer_infos.yml b/roles/hcloud/tasks/_read_load_balancer_infos.yml new file mode 100644 index 0000000..f14c516 --- /dev/null +++ b/roles/hcloud/tasks/_read_load_balancer_infos.yml @@ -0,0 +1,31 @@ +--- + +- name: "Gathering current load_balancer infos from hetzner" + hetzner.hcloud.hcloud_load_balancer_info: + api_token: "{{ hetzner_authentication_ansible }}" + #label_selector: "{{ current_load_balancer_group.label_selector }}" + register: current_load_balancer_infos + delegate_to: 127.0.0.1 + become: false + tags: + - update_config + +- name: "Setting loadbalancer group as fact: load_balancer_group_infos_{{ current_load_balancer_group.name }}" + set_fact: + load_balancer_group_infos_{{ current_load_balancer_group.name }}: "{{ current_load_balancer_infos.hcloud_load_balancer_info | json_query(querystr) }}" # noqa var-naming + vars: + querystr: "[*].{id: id, name: name, ip: ipv4_address}" + delegate_to: 127.0.0.1 + become: false + tags: + - update_config + +- name: "Printing load_balancer infos {{ current_load_balancer_infos }}" + debug: + msg: "{{ current_load_balancer_infos }}" + delegate_to: 127.0.0.1 + become: false + tags: + - update_config + when: + - debug