From c27b5b53369f4a40add5481431204392a0ea7f5e Mon Sep 17 00:00:00 2001 From: Erik Berg Date: Mon, 22 Jul 2024 03:17:25 +0200 Subject: [PATCH] Refactor TimescaleDB setup/configuration (#1347) * Refactor TimescaleDB tasks Query the db to actually see if we've got timescale configured instead of relying on files. Use the same task for importing the hypertable schema, and just conditionally change the path to the schemas. * Run Timescale schema during an upgrade In the case that timescale has already been enabled, and we're upgrading from 6.x to 7.x, the auditlog should also get converted. (Which it was not in the 6.x schemas) --- .../zabbix_server/tasks/initialize-pgsql.yml | 79 +++++++++---------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/roles/zabbix_server/tasks/initialize-pgsql.yml b/roles/zabbix_server/tasks/initialize-pgsql.yml index 8c42afd75..feb4292f3 100644 --- a/roles/zabbix_server/tasks/initialize-pgsql.yml +++ b/roles/zabbix_server/tasks/initialize-pgsql.yml @@ -91,47 +91,44 @@ state: restore target: /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz -- name: "PostgreSQL | Create TimescaleDB hypertables Zabbix < 7.0" - ansible.builtin.shell: | - set -euxo pipefail - cd /usr/share/zabbix-sql-scripts/postgresql && - if [ -f timescaledb.sql.gz ]; then zcat timescaledb.sql.gz > /etc/timescaledb.sql ; else cp -p timescaledb.sql /etc/timescaledb.sql ; fi - cat /etc/timescaledb.sql | psql -h '{{ zabbix_server_dbhost }}' \ - -U '{{ zabbix_server_dbuser }}' \ - -d '{{ zabbix_server_dbname }}' \ - -p '{{ zabbix_server_dbport }}' - touch /etc/zabbix/timescaledb.done - rm -f /tmp/timescaledb.sql - args: - creates: /etc/zabbix/timescaledb.done - executable: /bin/bash - environment: - PGPASSWORD: "{{ zabbix_server_dbpassword }}" - become: true - when: - - zabbix_server_database_timescaledb - - zabbix_server_version is version( '7.0', '<') +- name: "Configure Timescale" + when: zabbix_server_database_timescaledb tags: - database + block: + - name: "PostgreSQL | Check if TimescaleDB has been configured" + community.postgresql.postgresql_query: + login_user: "{{ zabbix_server_dbuser }}" + login_password: "{{ zabbix_server_dbpassword }}" + login_host: "{{ zabbix_server_dbhost }}" + port: "{{ zabbix_server_dbport }}" + db: "{{ zabbix_server_dbname }}" + query: "SELECT db_extension FROM config WHERE db_extension = 'timescaledb'" + register: _config_db_extension -- name: "PostgreSQL | Create TimescaleDB hypertables Zabbix >= 7.0" - ansible.builtin.shell: | - set -euxo pipefail - cd /usr/share/zabbix-sql-scripts/postgresql/timescaledb && - cat schema.sql | psql -h '{{ zabbix_server_dbhost }}' \ - -U '{{ zabbix_server_dbuser }}' \ - -d '{{ zabbix_server_dbname }}' \ - -p '{{ zabbix_server_dbport }}' - touch /etc/zabbix/timescaledb.done - rm -f /tmp/timescaledb.sql - args: - creates: /etc/zabbix/timescaledb.done - executable: /bin/bash - environment: - PGPASSWORD: "{{ zabbix_server_dbpassword }}" - become: true - when: - - zabbix_server_database_timescaledb - - zabbix_server_version is version( '7.0', '>=') - tags: - - database + # NOTE: Zabbix 7.0 adds a hypertable for auditlog. + - name: "PostgreSQL | Check for auditlog hypertable" + community.postgresql.postgresql_query: + login_user: "{{ zabbix_server_dbuser }}" + login_password: "{{ zabbix_server_dbpassword }}" + login_host: "{{ zabbix_server_dbhost }}" + port: "{{ zabbix_server_dbport }}" + db: "{{ zabbix_server_dbname }}" + query: "SELECT hypertable_name FROM timescaledb_information.hypertables WHERE hypertable_name = 'auditlog'" + register: _hypertable_auditlog + + - name: "PostgreSQL | Create TimescaleDB hypertables" + when: (_config_db_extension.query_result | length == 0) or + (zabbix_server_version is version('7.0', '>=') and _hypertable_auditlog.query_result | length == 0) + community.postgresql.postgresql_db: + login_user: "{{ zabbix_server_dbuser }}" + login_password: "{{ zabbix_server_dbpassword }}" + login_host: "{{ zabbix_server_dbhost }}" + port: "{{ zabbix_server_dbport }}" + db: "{{ zabbix_server_dbname }}" + state: restore + target: "{{ is_legacy_version | ternary(legacy_path, modern_path) }}" + vars: + is_legacy_version: "{{ zabbix_server_version is version('7.0', '<') }}" + legacy_path: /usr/share/zabbix-sql-scripts/postgresql/timescaledb.sql + modern_path: /usr/share/zabbix-sql-scripts/postgresql/timescaledb/schema.sql