From 2ec62b392be118d238240289c1346bbc6385f158 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 29 Oct 2024 21:01:58 +0800 Subject: [PATCH] feat: move naive assertion to common role Signed-off-by: Jack --- roles/_common/meta/argument_specs.yml | 3 +++ roles/_common/tasks/preflight.yml | 11 +++++++++++ roles/_common/vars/main.yml | 1 + roles/alertmanager/tasks/preflight.yml | 12 ++---------- roles/bind_exporter/tasks/preflight.yml | 12 ++---------- roles/blackbox_exporter/tasks/preflight.yml | 11 +---------- roles/chrony_exporter/tasks/preflight.yml | 12 ++---------- roles/fail2ban_exporter/tasks/preflight.yml | 12 ++---------- roles/influxdb_exporter/tasks/preflight.yml | 17 +++-------------- roles/ipmi_exporter/tasks/preflight.yml | 11 +---------- roles/memcached_exporter/tasks/preflight.yml | 12 ++---------- roles/mongodb_exporter/tasks/preflight.yml | 12 ++---------- roles/mysqld_exporter/tasks/preflight.yml | 12 ++---------- roles/nginx_exporter/tasks/preflight.yml | 12 ++---------- roles/node_exporter/tasks/preflight.yml | 12 ++---------- roles/postgres_exporter/tasks/preflight.yml | 12 ++---------- roles/process_exporter/tasks/preflight.yml | 7 ++----- roles/prometheus/tasks/preflight.yml | 12 ++---------- roles/pushgateway/tasks/preflight.yml | 12 ++---------- roles/redis_exporter/tasks/preflight.yml | 12 ++---------- roles/smartctl_exporter/tasks/preflight.yml | 12 ++---------- roles/smokeping_prober/tasks/preflight.yml | 12 ++---------- roles/snmp_exporter/tasks/preflight.yml | 12 ++---------- roles/systemd_exporter/tasks/preflight.yml | 12 ++---------- 24 files changed, 56 insertions(+), 209 deletions(-) diff --git a/roles/_common/meta/argument_specs.yml b/roles/_common/meta/argument_specs.yml index 7642a47c..fd2fa3e8 100644 --- a/roles/_common/meta/argument_specs.yml +++ b/roles/_common/meta/argument_specs.yml @@ -81,6 +81,9 @@ argument_specs: {{ ('python-apt' if ansible_python_version is version('3', '<') else 'python3-apt') }} {% else %}\ {% endif %}" + _common_web_listen_address: + description: "Address on which to listen" + default: "" selinux: short_description: "Internal only - common selinux configuration tasks" description: "Internal only - selinux requirements" diff --git a/roles/_common/tasks/preflight.yml b/roles/_common/tasks/preflight.yml index 4a8006e5..6ea26f62 100644 --- a/roles/_common/tasks/preflight.yml +++ b/roles/_common/tasks/preflight.yml @@ -74,3 +74,14 @@ - "{{ ansible_parent_role_names | first | regex_replace(ansible_collection_name ~ '.', '') }}_configure" - install - "{{ ansible_parent_role_names | first | regex_replace(ansible_collection_name ~ '.', '') }}_install" + +- name: Naive assertion of proper listen address + ansible.builtin.assert: + that: + - >- + [_common_web_listen_address] | + flatten | + reject('match', '.+:\\d+$') | + list | + length == 0 + when: (_common_web_listen_address) diff --git a/roles/_common/vars/main.yml b/roles/_common/vars/main.yml index dbda12bc..8d8beb8b 100644 --- a/roles/_common/vars/main.yml +++ b/roles/_common/vars/main.yml @@ -18,6 +18,7 @@ _common_binary_unarchive_opts: "" _common_tls_server_config: {} _common_http_server_config: {} _common_basic_auth_users: {} +_common_web_listen_address: "" # Variables that should not be overwritten __common_binary_basename: "{{ _common_binary_url | urlsplit('path') | basename }}" __common_github_api_headers: "{{ {'GITHUB_TOKEN': lookup('ansible.builtin.env', 'GITHUB_TOKEN')} if (lookup('ansible.builtin.env', 'GITHUB_TOKEN')) else {} }}" diff --git a/roles/alertmanager/tasks/preflight.yml b/roles/alertmanager/tasks/preflight.yml index ea6f2a33..f4b88d32 100644 --- a/roles/alertmanager/tasks/preflight.yml +++ b/roles/alertmanager/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ alertmanager_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ alertmanager_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [alertmanager_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Discover latest version ansible.builtin.set_fact: alertmanager_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _alertmanager_repo }}/releases/latest', headers=_github_api_headers, diff --git a/roles/bind_exporter/tasks/preflight.yml b/roles/bind_exporter/tasks/preflight.yml index 82ff04fa..e777f5db 100644 --- a/roles/bind_exporter/tasks/preflight.yml +++ b/roles/bind_exporter/tasks/preflight.yml @@ -3,16 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml - -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [bind_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 + vars: + _common_web_listen_address: "{{ bind_exporter_web_listen_address }}" - name: Assert that TLS config is correct when: bind_exporter_tls_server_config | length > 0 diff --git a/roles/blackbox_exporter/tasks/preflight.yml b/roles/blackbox_exporter/tasks/preflight.yml index 7f8c98e8..a4816112 100644 --- a/roles/blackbox_exporter/tasks/preflight.yml +++ b/roles/blackbox_exporter/tasks/preflight.yml @@ -5,6 +5,7 @@ tasks_from: preflight.yml vars: _common_dependencies: "{{ _blackbox_exporter_dependencies }}" + _common_web_listen_address: "{{ blackbox_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -17,16 +18,6 @@ blackbox_exporter_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [blackbox_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Discover latest version ansible.builtin.set_fact: blackbox_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _blackbox_exporter_repo }}/releases/latest', headers=_github_api_headers, diff --git a/roles/chrony_exporter/tasks/preflight.yml b/roles/chrony_exporter/tasks/preflight.yml index 77057bd6..3d5eae42 100644 --- a/roles/chrony_exporter/tasks/preflight.yml +++ b/roles/chrony_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ chrony_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ chrony_exporter_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [chrony_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert collectors are not both disabled and enabled at the same time ansible.builtin.assert: that: diff --git a/roles/fail2ban_exporter/tasks/preflight.yml b/roles/fail2ban_exporter/tasks/preflight.yml index f98df975..8cc5f9e5 100644 --- a/roles/fail2ban_exporter/tasks/preflight.yml +++ b/roles/fail2ban_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ fail2ban_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- fail2ban_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [fail2ban_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Discover latest version ansible.builtin.set_fact: fail2ban_exporter_version: "{{ (lookup('url', 'https://gitlab.com/api/v4/projects/{{ _fail2ban_exporter_repo }}/releases', diff --git a/roles/influxdb_exporter/tasks/preflight.yml b/roles/influxdb_exporter/tasks/preflight.yml index 938f1d55..9b7eb116 100644 --- a/roles/influxdb_exporter/tasks/preflight.yml +++ b/roles/influxdb_exporter/tasks/preflight.yml @@ -3,6 +3,9 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ influxdb_exporter_web_listen_address }}" + tags: - influxdb_exporter - preflight @@ -17,20 +20,6 @@ - preflight - influxdb_exporter_preflight -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [influxdb_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - tags: - - influxdb_exporter - - preflight - - influxdb_exporter_preflight - - name: Discover latest version ansible.builtin.set_fact: influxdb_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _influxdb_exporter_repo }}/releases/latest', headers=_influxdb_exporter_github_api_headers, diff --git a/roles/ipmi_exporter/tasks/preflight.yml b/roles/ipmi_exporter/tasks/preflight.yml index 4e07f019..b072d767 100644 --- a/roles/ipmi_exporter/tasks/preflight.yml +++ b/roles/ipmi_exporter/tasks/preflight.yml @@ -5,6 +5,7 @@ tasks_from: preflight.yml vars: _common_dependencies: "{{ _ipmi_exporter_dependencies }}" + _common_web_listen_address: "{{ ipmi_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -12,16 +13,6 @@ - >- ipmi_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [ipmi_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: ipmi_exporter_tls_server_config | length > 0 block: diff --git a/roles/memcached_exporter/tasks/preflight.yml b/roles/memcached_exporter/tasks/preflight.yml index 47857357..4245e3fd 100644 --- a/roles/memcached_exporter/tasks/preflight.yml +++ b/roles/memcached_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ memcached_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- memcached_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [memcached_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: memcached_exporter_tls_server_config | length > 0 block: diff --git a/roles/mongodb_exporter/tasks/preflight.yml b/roles/mongodb_exporter/tasks/preflight.yml index 35d15fe4..885ffcef 100644 --- a/roles/mongodb_exporter/tasks/preflight.yml +++ b/roles/mongodb_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ mongodb_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- mongodb_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [mongodb_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: mongodb_exporter_tls_server_config | length > 0 block: diff --git a/roles/mysqld_exporter/tasks/preflight.yml b/roles/mysqld_exporter/tasks/preflight.yml index 59ae4922..7934ee07 100644 --- a/roles/mysqld_exporter/tasks/preflight.yml +++ b/roles/mysqld_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ mysqld_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ mysqld_exporter_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [mysqld_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert collectors are not both disabled and enabled at the same time ansible.builtin.assert: that: diff --git a/roles/nginx_exporter/tasks/preflight.yml b/roles/nginx_exporter/tasks/preflight.yml index 0d1b55f8..5cf82a41 100644 --- a/roles/nginx_exporter/tasks/preflight.yml +++ b/roles/nginx_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ nginx_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- nginx_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [nginx_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: nginx_exporter_tls_server_config | length > 0 block: diff --git a/roles/node_exporter/tasks/preflight.yml b/roles/node_exporter/tasks/preflight.yml index 6431ad69..acb9da66 100644 --- a/roles/node_exporter/tasks/preflight.yml +++ b/roles/node_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ node_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ node_exporter_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [node_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert collectors are not both disabled and enabled at the same time ansible.builtin.assert: that: diff --git a/roles/postgres_exporter/tasks/preflight.yml b/roles/postgres_exporter/tasks/preflight.yml index b2e67d21..1e92ce9f 100644 --- a/roles/postgres_exporter/tasks/preflight.yml +++ b/roles/postgres_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ postgres_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ postgres_exporter_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [postgres_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert collectors are not both disabled and enabled at the same time ansible.builtin.assert: that: diff --git a/roles/process_exporter/tasks/preflight.yml b/roles/process_exporter/tasks/preflight.yml index b012d8e8..e2928a3b 100644 --- a/roles/process_exporter/tasks/preflight.yml +++ b/roles/process_exporter/tasks/preflight.yml @@ -3,11 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml - -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - "':' in process_exporter_web_listen_address" + vars: + _common_web_listen_address: "{{ process_exporter_web_listen_address }}" - name: Discover latest version ansible.builtin.set_fact: diff --git a/roles/prometheus/tasks/preflight.yml b/roles/prometheus/tasks/preflight.yml index 12155eb9..ff1775a6 100644 --- a/roles/prometheus/tasks/preflight.yml +++ b/roles/prometheus/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ prometheus_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- prometheus_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [prometheus_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert no duplicate config flags ansible.builtin.assert: that: diff --git a/roles/pushgateway/tasks/preflight.yml b/roles/pushgateway/tasks/preflight.yml index 64ae5376..105120ee 100644 --- a/roles/pushgateway/tasks/preflight.yml +++ b/roles/pushgateway/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ pushgateway_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ pushgateway_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [pushgateway_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: pushgateway_tls_server_config | length > 0 block: diff --git a/roles/redis_exporter/tasks/preflight.yml b/roles/redis_exporter/tasks/preflight.yml index 040cf5de..70291174 100644 --- a/roles/redis_exporter/tasks/preflight.yml +++ b/roles/redis_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ redis_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- redis_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [redis_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: - redis_exporter_tls_server_key_file | length > 0 diff --git a/roles/smartctl_exporter/tasks/preflight.yml b/roles/smartctl_exporter/tasks/preflight.yml index bb974c3a..b27d82bd 100644 --- a/roles/smartctl_exporter/tasks/preflight.yml +++ b/roles/smartctl_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ smartctl_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- smartctl_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [smartctl_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: smartctl_exporter_tls_server_config | length > 0 block: diff --git a/roles/smokeping_prober/tasks/preflight.yml b/roles/smokeping_prober/tasks/preflight.yml index 8a106b27..9fb5234b 100644 --- a/roles/smokeping_prober/tasks/preflight.yml +++ b/roles/smokeping_prober/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ smokeping_prober_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ smokeping_prober_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [smokeping_prober_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: smokeping_prober_tls_server_config | length > 0 block: diff --git a/roles/snmp_exporter/tasks/preflight.yml b/roles/snmp_exporter/tasks/preflight.yml index ba3ba66c..36ff833b 100644 --- a/roles/snmp_exporter/tasks/preflight.yml +++ b/roles/snmp_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ snmp_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -15,16 +17,6 @@ snmp_exporter_web_listen_address | type_debug == "list" ) -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [snmp_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Discover latest version ansible.builtin.set_fact: snmp_exporter_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _snmp_exporter_repo }}/releases/latest', headers=_github_api_headers, diff --git a/roles/systemd_exporter/tasks/preflight.yml b/roles/systemd_exporter/tasks/preflight.yml index adec0f3a..331533be 100644 --- a/roles/systemd_exporter/tasks/preflight.yml +++ b/roles/systemd_exporter/tasks/preflight.yml @@ -3,6 +3,8 @@ ansible.builtin.include_role: name: prometheus.prometheus._common tasks_from: preflight.yml + vars: + _common_web_listen_address: "{{ systemd_exporter_web_listen_address }}" - name: Assert that used version supports listen address type ansible.builtin.assert: @@ -10,16 +12,6 @@ - >- systemd_exporter_web_listen_address is string -- name: Naive assertion of proper listen address - ansible.builtin.assert: - that: - - >- - [systemd_exporter_web_listen_address] | - flatten | - reject('match', '.+:\\d+$') | - list | - length == 0 - - name: Assert that TLS config is correct when: systemd_exporter_tls_server_config | length > 0 block: