Skip to content

Commit

Permalink
Refactor PostgreSQL schema import
Browse files Browse the repository at this point in the history
We take advantage of the community.postgres modules, and do a query
for 'mandatory' in 'dbversion', as is done in the mysql tasks. If
the query fails, we're going to assume the database has not been
populated yet, and populate it in the rescue section.

The query and schema import happens from the zabbix server, it
serves as a good test of verifying permissions previously created.
  • Loading branch information
eb4x authored and pyrodie18 committed May 12, 2024
1 parent fbe8baf commit e4590a0
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions roles/zabbix_server/tasks/postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,33 @@
db: "{{ zabbix_server_dbname }}"
name: timescaledb

- name: "PostgreSQL | Create schema"
ansible.builtin.shell: |
set -euxo pipefail
FILE={{ is_legacy_version | ternary('create', 'server') }}.sql
cd {{ is_legacy_version | ternary(legacy_path, modern_path) }}
if [ -f ${FILE}.gz ]
then zcat ${FILE}.gz > /tmp/create.sql
else
cp ${FILE} /tmp/create.sql
fi
cat /tmp/create.sql | psql -h '{{ zabbix_server_dbhost }}' \
-U '{{ zabbix_server_dbuser }}' \
-d '{{ zabbix_server_dbname }}' \
-p '{{ zabbix_server_dbport }}'
touch /etc/zabbix/schema.done
rm -f /tmp/create.sql
args:
creates: /etc/zabbix/schema.done
executable: /bin/bash
warn: "{{ produce_warn | default(omit) }}"
environment:
PGPASSWORD: "{{ zabbix_server_dbpassword }}"
become: true
vars:
is_legacy_version: "{{ zabbix_server_version is version('6.0', '<') }}"
legacy_path: /usr/share/doc/zabbix-server-pgsql
modern_path: /usr/share/zabbix-sql-scripts/postgresql
when:
- zabbix_server_database_sqlload
- name: "PostgreSQL verify or create schema"
when: zabbix_server_database_sqlload
tags:
- database
block:
- name: "PostgreSQL | Get current database version"
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 mandatory FROM dbversion'
rescue:
- name: "PostgreSQL | Import schema"
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('6.0', '<') }}"
legacy_path: /usr/share/doc/zabbix-server-pgsql/create.sql.gz
modern_path: /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz

- name: "PostgreSQL | Create TimescaleDB hypertables"
ansible.builtin.shell: |
Expand Down

0 comments on commit e4590a0

Please sign in to comment.