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