From ebad4eade4d08a116fabb32a009174b7f8127f50 Mon Sep 17 00:00:00 2001 From: Sven Ketelsen Date: Mon, 30 Aug 2021 19:24:44 +0200 Subject: [PATCH] chore: new playbook to update local ssh config file {for each host on stage} Host dev-... HostName 1.2.3.4 {endfor} --- group_vars/all/plain.yml | 1 - roles/common/tasks/main.yml | 17 ------- roles/hcloud/tasks/_read_server_infos.yml | 31 ++++++++++++ roles/hcloud/tasks/_read_server_names.yml | 12 +++-- start.yml | 21 --------- stop.yml | 21 --------- .../config/prometheus/prometheus.yml.j2 | 22 ++++----- update-ssh-config-file.yml | 47 +++++++++++++++++++ 8 files changed, 96 insertions(+), 76 deletions(-) create mode 100644 roles/hcloud/tasks/_read_server_infos.yml delete mode 100644 start.yml delete mode 100644 stop.yml create mode 100644 update-ssh-config-file.yml diff --git a/group_vars/all/plain.yml b/group_vars/all/plain.yml index 86cdc0c..0c4fbb5 100644 --- a/group_vars/all/plain.yml +++ b/group_vars/all/plain.yml @@ -1,7 +1,6 @@ --- debug: false -local_ssh_config: false send_status_messages: false use_ssl: true diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml index 6237ab9..1ee368f 100644 --- a/roles/common/tasks/main.yml +++ b/roles/common/tasks/main.yml @@ -2,7 +2,6 @@ # This playbook contains common plays that will be run on all nodes. ### tags: -### local_ssh_config ### users ### install ### config @@ -20,22 +19,6 @@ when: - send_status_messages -- name: 'Insert/Update ssh config in ~/.ssh/config' - blockinfile: - marker: '# {mark} managed by ansible (ssh config for {{ inventory_hostname }})' - path: '~/.ssh/config' - create: yes - block: | - Host {{ inventory_hostname }} - HostName {{ stage_server_ip }} - when: - - local_ssh_config - delegate_to: 127.0.0.1 - become: false - throttle: 1 - tags: - - local_ssh_config - - name: "Set hostname to <{{ inventory_hostname }}>" hostname: name: "{{ inventory_hostname }}" diff --git a/roles/hcloud/tasks/_read_server_infos.yml b/roles/hcloud/tasks/_read_server_infos.yml new file mode 100644 index 0000000..1b1df45 --- /dev/null +++ b/roles/hcloud/tasks/_read_server_infos.yml @@ -0,0 +1,31 @@ +--- + +- name: "Gathering current server infos for group {{ current_server_group.name }} from hetzner" + hcloud_server_info: + api_token: "{{ hetzner_authentication_token }}" + label_selector: "{{ current_server_group.label_selector }}" + register: current_server_infos + delegate_to: 127.0.0.1 + become: false + tags: + - update_config + +- name: "Setting server group as fact: server_group_infos_{{ current_server_group.name }}" + set_fact: + server_group_infos_{{ current_server_group.name }}: "{{ current_server_infos.hcloud_server_info | json_query(querystr) }}" + vars: + querystr: "[*].{id: id, name: name, ip: ipv4_address}" + delegate_to: 127.0.0.1 + become: false + tags: + - update_config + +- name: "Printing server group {{ current_server_group.name }}" + debug: + msg: "{{ lookup('vars', 'server_group_infos_' + current_server_group.name) }}" + delegate_to: 127.0.0.1 + become: false + tags: + - update_config + when: + - debug \ No newline at end of file diff --git a/roles/hcloud/tasks/_read_server_names.yml b/roles/hcloud/tasks/_read_server_names.yml index e296101..9dd88d6 100644 --- a/roles/hcloud/tasks/_read_server_names.yml +++ b/roles/hcloud/tasks/_read_server_names.yml @@ -10,9 +10,9 @@ tags: - update_config -- name: "Setting server group as fact: server_group_{{ current_server_group.name }}" - set_fact: - server_group_{{ current_server_group.name }}: "{{ current_server_infos.hcloud_server_info | json_query(querystr) }}" +- name: "Setting server group as fact: server_group_names_{{ current_server_group.name }}" + set_fact: + server_group_names_{{ current_server_group.name }}: "{{ current_server_infos.hcloud_server_info | json_query(querystr) }}" vars: querystr: "[*].name" delegate_to: 127.0.0.1 @@ -22,8 +22,10 @@ - name: "Printing server group {{ current_server_group.name }}" debug: - msg: "{{ lookup('vars', 'server_group_' + current_server_group.name) }}" + msg: "{{ lookup('vars', 'server_group_names_' + current_server_group.name) }}" delegate_to: 127.0.0.1 become: false tags: - - update_config \ No newline at end of file + - update_config + when: + - debug \ No newline at end of file diff --git a/start.yml b/start.yml deleted file mode 100644 index f8dd0e3..0000000 --- a/start.yml +++ /dev/null @@ -1,21 +0,0 @@ ---- - -- name: 'apply setup to {{ host | default("all") }}' - hosts: '{{ host | default("all") }}' - serial: "{{ serial_number | default(5) }}" - gather_facts: no - become: no - - 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 }})" - - roles: - - role: hetzner-state - vars: - hetzner_state: 'started' - when: "'hcloud' in group_names" diff --git a/stop.yml b/stop.yml deleted file mode 100644 index 3711047..0000000 --- a/stop.yml +++ /dev/null @@ -1,21 +0,0 @@ ---- - -- name: 'apply setup to {{ host | default("all") }}' - hosts: '{{ host | default("all") }}' - serial: "{{ serial_number | default(5) }}" - gather_facts: no - become: no - - 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 }})" - - roles: - - role: hetzner-state - vars: - hetzner_state: 'stopped' - when: "'hcloud' in group_names" diff --git a/templates/prometheus/config/prometheus/prometheus.yml.j2 b/templates/prometheus/config/prometheus/prometheus.yml.j2 index 08d5794..d980df9 100644 --- a/templates/prometheus/config/prometheus/prometheus.yml.j2 +++ b/templates/prometheus/config/prometheus/prometheus.yml.j2 @@ -49,7 +49,7 @@ scrape_configs: metrics_path: '/metrics' static_configs: - targets: [ -{% for host in server_group_all | difference(['dev-awx-01','dev-prometheus-01','dev-maria-01']) | default([]) %} +{% for host in server_group_names_all | difference(['dev-awx-01','dev-prometheus-01','dev-maria-01']) | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_service }}', {% endfor %} ] @@ -71,7 +71,7 @@ scrape_configs: static_configs: # Scrape metrics from the Harbor exporter component - targets: [ -{% for host in server_group_harbor | default([]) %} +{% for host in server_group_names_harbor | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_harbor }}', {% endfor %} ] @@ -93,7 +93,7 @@ scrape_configs: comp: ['core'] static_configs: - targets: [ -{% for host in server_group_harbor | default([]) %} +{% for host in server_group_names_harbor | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_harbor }}', {% endfor %} ] @@ -115,7 +115,7 @@ scrape_configs: comp: ['registry'] static_configs: - targets: [ -{% for host in server_group_harbor | default([]) %} +{% for host in server_group_names_harbor | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_harbor }}', {% endfor %} ] @@ -139,7 +139,7 @@ scrape_configs: password: {{ awx_admin_password }} static_configs: - targets: [ -{% for host in server_group_awx | default([]) %} +{% for host in server_group_names_awx | default([]) %} '{{ host }}.{{ domain }}', {% endfor %} ] @@ -160,7 +160,7 @@ scrape_configs: metrics_path: '/management/prometheus' static_configs: - targets: [ -{% for host in server_group_connect | default([]) %} +{% for host in server_group_names_connect | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_service }}', {% endfor %} ] @@ -179,7 +179,7 @@ scrape_configs: metrics_path: '/management/prometheus' static_configs: - targets: [ -{% for host in server_group_iam | default([]) %} +{% for host in server_group_names_iam | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_service }}', {% endfor %} ] @@ -198,7 +198,7 @@ scrape_configs: metrics_path: '/management/prometheus' static_configs: - targets: [ -{% for host in server_group_webdav | default([]) %} +{% for host in server_group_names_webdav | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_service }}', {% endfor %} ] @@ -221,7 +221,7 @@ scrape_configs: metrics_path: '/metrics' static_configs: - targets: [ -{% for host in server_group_all | default([]) %} +{% for host in server_group_names_all | default([]) %} '{{ host }}.{{ domain }}:{{ monitor_port_system }}', {% endfor %} ] @@ -246,7 +246,7 @@ scrape_configs: metrics_path: '/metrics' static_configs: - targets: [ -{% for host in server_group_elastic | default([]) %} +{% for host in server_group_names_elastic | default([]) %} '{{ host }}.{{ domain }}:9083', {% endfor %} ] @@ -267,7 +267,7 @@ scrape_configs: metrics_path: '/metrics' static_configs: - targets: [ -{% for host in server_group_postgres | default([]) %} +{% for host in server_group_names_postgres | default([]) %} '{{ host }}.{{ domain }}:9187', {% endfor %} ] diff --git a/update-ssh-config-file.yml b/update-ssh-config-file.yml new file mode 100644 index 0000000..34b880d --- /dev/null +++ b/update-ssh-config-file.yml @@ -0,0 +1,47 @@ +--- + +# Parameters: +# playbook inventory +# stage := the type of the stage (e.g. dev, int, qa, prod) + +############################################################# +# Updating ssh config for servers with given stage +############################################################# + +- 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 }})" + + - 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 + + tasks: + - name: 'Insert/Update ssh config in ~/.ssh/config' + blockinfile: + marker: "# {mark} managed by ansible (hosts config for {{ stage }})" + path: '~/.ssh/config' + state: present + create: yes + block: | + {% for host in server_group_infos_all %} + Host {{ host.name }} + HostName {{ host.ip }} + {% endfor %} \ No newline at end of file