chore: new playbook to update local ssh config file

{for each host on stage}
Host dev-...
  HostName 1.2.3.4
{endfor}
master
Sven Ketelsen 4 years ago
parent 79a423f7b7
commit ebad4eade4

@ -1,7 +1,6 @@
--- ---
debug: false debug: false
local_ssh_config: false
send_status_messages: false send_status_messages: false
use_ssl: true use_ssl: true

@ -2,7 +2,6 @@
# This playbook contains common plays that will be run on all nodes. # This playbook contains common plays that will be run on all nodes.
### tags: ### tags:
### local_ssh_config
### users ### users
### install ### install
### config ### config
@ -20,22 +19,6 @@
when: when:
- send_status_messages - 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 }}>" - name: "Set hostname to <{{ inventory_hostname }}>"
hostname: hostname:
name: "{{ inventory_hostname }}" name: "{{ inventory_hostname }}"

@ -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

@ -10,9 +10,9 @@
tags: tags:
- update_config - update_config
- name: "Setting server group as fact: server_group_{{ current_server_group.name }}" - name: "Setting server group as fact: server_group_names_{{ current_server_group.name }}"
set_fact: set_fact:
server_group_{{ current_server_group.name }}: "{{ current_server_infos.hcloud_server_info | json_query(querystr) }}" server_group_names_{{ current_server_group.name }}: "{{ current_server_infos.hcloud_server_info | json_query(querystr) }}"
vars: vars:
querystr: "[*].name" querystr: "[*].name"
delegate_to: 127.0.0.1 delegate_to: 127.0.0.1
@ -22,8 +22,10 @@
- name: "Printing server group {{ current_server_group.name }}" - name: "Printing server group {{ current_server_group.name }}"
debug: 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 delegate_to: 127.0.0.1
become: false become: false
tags: tags:
- update_config - update_config
when:
- debug

@ -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"

@ -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"

@ -49,7 +49,7 @@ scrape_configs:
metrics_path: '/metrics' metrics_path: '/metrics'
static_configs: static_configs:
- targets: [ - 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 }}', '{{ host }}.{{ domain }}:{{ monitor_port_service }}',
{% endfor %} {% endfor %}
] ]
@ -71,7 +71,7 @@ scrape_configs:
static_configs: static_configs:
# Scrape metrics from the Harbor exporter component # Scrape metrics from the Harbor exporter component
- targets: [ - targets: [
{% for host in server_group_harbor | default([]) %} {% for host in server_group_names_harbor | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_harbor }}', '{{ host }}.{{ domain }}:{{ monitor_port_harbor }}',
{% endfor %} {% endfor %}
] ]
@ -93,7 +93,7 @@ scrape_configs:
comp: ['core'] comp: ['core']
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_harbor | default([]) %} {% for host in server_group_names_harbor | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_harbor }}', '{{ host }}.{{ domain }}:{{ monitor_port_harbor }}',
{% endfor %} {% endfor %}
] ]
@ -115,7 +115,7 @@ scrape_configs:
comp: ['registry'] comp: ['registry']
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_harbor | default([]) %} {% for host in server_group_names_harbor | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_harbor }}', '{{ host }}.{{ domain }}:{{ monitor_port_harbor }}',
{% endfor %} {% endfor %}
] ]
@ -139,7 +139,7 @@ scrape_configs:
password: {{ awx_admin_password }} password: {{ awx_admin_password }}
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_awx | default([]) %} {% for host in server_group_names_awx | default([]) %}
'{{ host }}.{{ domain }}', '{{ host }}.{{ domain }}',
{% endfor %} {% endfor %}
] ]
@ -160,7 +160,7 @@ scrape_configs:
metrics_path: '/management/prometheus' metrics_path: '/management/prometheus'
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_connect | default([]) %} {% for host in server_group_names_connect | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_service }}', '{{ host }}.{{ domain }}:{{ monitor_port_service }}',
{% endfor %} {% endfor %}
] ]
@ -179,7 +179,7 @@ scrape_configs:
metrics_path: '/management/prometheus' metrics_path: '/management/prometheus'
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_iam | default([]) %} {% for host in server_group_names_iam | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_service }}', '{{ host }}.{{ domain }}:{{ monitor_port_service }}',
{% endfor %} {% endfor %}
] ]
@ -198,7 +198,7 @@ scrape_configs:
metrics_path: '/management/prometheus' metrics_path: '/management/prometheus'
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_webdav | default([]) %} {% for host in server_group_names_webdav | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_service }}', '{{ host }}.{{ domain }}:{{ monitor_port_service }}',
{% endfor %} {% endfor %}
] ]
@ -221,7 +221,7 @@ scrape_configs:
metrics_path: '/metrics' metrics_path: '/metrics'
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_all | default([]) %} {% for host in server_group_names_all | default([]) %}
'{{ host }}.{{ domain }}:{{ monitor_port_system }}', '{{ host }}.{{ domain }}:{{ monitor_port_system }}',
{% endfor %} {% endfor %}
] ]
@ -246,7 +246,7 @@ scrape_configs:
metrics_path: '/metrics' metrics_path: '/metrics'
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_elastic | default([]) %} {% for host in server_group_names_elastic | default([]) %}
'{{ host }}.{{ domain }}:9083', '{{ host }}.{{ domain }}:9083',
{% endfor %} {% endfor %}
] ]
@ -267,7 +267,7 @@ scrape_configs:
metrics_path: '/metrics' metrics_path: '/metrics'
static_configs: static_configs:
- targets: [ - targets: [
{% for host in server_group_postgres | default([]) %} {% for host in server_group_names_postgres | default([]) %}
'{{ host }}.{{ domain }}:9187', '{{ host }}.{{ domain }}:9187',
{% endfor %} {% endfor %}
] ]

@ -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 %}
Loading…
Cancel
Save