Skip to content

Commit a2ad28c

Browse files
authored
Merge pull request #221 from Icinga/feature/pgsql-schemas
Adds PostgresQL support for Icingaweb2 modules
2 parents 0426b95 + ecf9f53 commit a2ad28c

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- Added support for PostgresQL databases for Icingaweb2 modules that support it
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
- name: Check Database Credentials
3+
ansible.builtin.assert:
4+
that:
5+
- _db['user'] is defined
6+
- _db['password'] is defined
7+
fail_msg: "No database credentials defined."
8+
9+
- name: Build pgsql command
10+
ansible.builtin.set_fact:
11+
_tmp_pgsqlcmd: >-
12+
PGPASSWORD="{{ _db['password'] }}"
13+
psql
14+
"{% if _db['host'] | default('localhost') != 'localhost' %} host={{ _db['host'] }}{%- endif %}
15+
{% if _db['port'] is defined %} port={{ _db['port'] }}{%- endif %}
16+
user={{ _db['user'] }}
17+
dbname={{ _db['name'] }}
18+
{% if _db['ssl_mode'] is defined %} sslmode={{ _db['ssl_mode'] | default('require') }}{%- endif %}
19+
{% if _db['ssl_cert'] is defined %} sslcert={{ _db['ssl_cert'] }}{%- endif %}
20+
{% if _db['ssl_key'] is defined %} sslkey={{ _db['ssl_key'] }}{%- endif %}
21+
{% if _db['ssl_extra_options'] is defined %} {{ _db['ssl_extra_options'] }}{%- endif %}"
22+
23+
- name: PostgresQL check for db schema
24+
ansible.builtin.shell: >
25+
{{ _tmp_pgsqlcmd }}
26+
-tq -c "{{ _db['select_query'] }}"
27+
failed_when: false
28+
changed_when: false
29+
check_mode: false
30+
register: _db_schema
31+
32+
- name: PostgresQL import db schema
33+
ansible.builtin.shell: >
34+
{{ _tmp_pgsqlcmd }}
35+
< {{ _db['schema_path_pgsql'] }}
36+
when: _db_schema.rc != 0
37+
run_once: yes

roles/icingaweb2/tasks/modules/x509.yml

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,56 @@
2121
- sni
2222

2323
- name: Module x509 | Manage Schema
24+
when: vars['icingaweb2_modules'][_module]['database']['import_schema'] | default(false)
25+
vars:
26+
_module: "{{ item.key }}"
2427
block:
2528
- name: Module x509 | Prepare _db informations
2629
ansible.builtin.set_fact:
2730
_db:
28-
host: "{{ icingaweb2_modules[_module].database.host | default('localhost') }}"
29-
port: "{{ icingaweb2_modules[_module].database.port | default('3306') }}"
30-
user: "{{ icingaweb2_modules[_module].database.user | default('x509') }}"
31-
password: "{{ icingaweb2_modules[_module].database.password | default(omit) }}"
32-
name: "{{ icingaweb2_modules[_module].database.name | default('x509') }}"
33-
ssl_mode: "{{ icingaweb2_modules[_module].database.ssl_mode | default(omit) }}"
34-
ssl_ca: "{{ icingaweb2_modules[_module].database.ssl_ca | default(omit) }}"
35-
ssl_cert: "{{ icingaweb2_modules[_module].database.ssl_cert | default(omit) }}"
36-
ssl_key: "{{ icingaweb2_modules[_module].database.ssl_key | default(omit) }}"
37-
ssl_cipher: "{{ icingaweb2_modules[_module].database.ssl_cipher | default(omit) }}"
38-
ssl_extra_options: "{{ icingaweb2_modules[_module].database.ssl_extra_options | default(omit) }}"
39-
schema_path: /usr/share/icingaweb2/modules/x509/schema/mysql.schema.sql
31+
host: "{{ vars['icingaweb2_modules'][_module]['database']['host'] | default('localhost') }}"
32+
port: "{{ vars['icingaweb2_modules'][_module]['database']['port'] | default('3306') }}"
33+
user: "{{ vars['icingaweb2_modules'][_module]['database']['user'] | default('x509') }}"
34+
password: "{{ vars['icingaweb2_modules'][_module]['database']['password'] | default(omit) }}"
35+
name: "{{ vars['icingaweb2_modules'][_module]['database']['name'] | default('x509') }}"
36+
ssl_mode: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_mode'] | default(omit) }}"
37+
ssl_ca: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_ca'] | default(omit) }}"
38+
ssl_cert: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_cert'] | default(omit) }}"
39+
ssl_key: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_key'] | default(omit) }}"
40+
ssl_cipher: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_cipher'] | default(omit) }}"
41+
ssl_extra_options: "{{ vars['icingaweb2_modules'][_module]['database']['ssl_extra_options'] | default(omit) }}"
42+
schema_path_mysql: /usr/share/icingaweb2/modules/x509/schema/mysql.schema.sql
43+
schema_path_pgsql: /usr/share/icingaweb2/modules/x509/schema/pgsql.schema.sql
4044
select_query: "select * from x509_certificate"
41-
when: icingaweb2_modules[_module].database.type | default('mysql') == 'mysql'
45+
type: "{{ vars['icingaweb2_modules'][_module]['database']['type'] | default(omit) }}"
46+
47+
- ansible.builtin.fail:
48+
fail_msg: No database type was provided
49+
when: vars['icingaweb2_modules'][_module]['database']['type'] is not defined
4250

4351
- ansible.builtin.fail:
44-
fail_msg: "The Database type select is not supported, {{ icingaweb2_modules[_module].database.type }} [Supported=mysql]"
45-
when: vars['icingaweb2_modules'][_module]['database']['type'] is defined and icingaweb2_modules[_module].database.type != 'mysql'
52+
fail_msg: "Invalid database type was provided. [Supported: mysql, pgsql]"
53+
when: _db.type not in ['mysql', 'pgsql']
4654

47-
- name: Module x509 | Import Schema
55+
- name: Module x509 | Import MySQL Schema
4856
ansible.builtin.include_tasks: ../manage_mysql_imports.yml
57+
when: _db.type == 'mysql'
58+
59+
- name: Module x509 | Import PostgresQL Schema
60+
ansible.builtin.include_tasks: ../manage_pgsql_imports.yml
61+
when: _db.type == 'pgsql'
4962

5063
- name: Module x509 | empty _db var
5164
ansible.builtin.set_fact:
5265
_db: {}
53-
when: icingaweb2_modules[_module].database.import_schema | default(false)
54-
vars:
55-
_module: "{{ item.key }}"
5666

5767
- name: Module x509 | Import Certificates
5868
ansible.builtin.shell: >
5969
icingacli {{ _module }} import --file {{ _file }}
60-
loop: "{{ icingaweb2_modules[_module].certificate_files }}"
70+
loop: "{{ vars['icingaweb2_modules'][_module]['certificate_files'] }}"
6171
loop_control:
6272
loop_var: _file
6373
vars:
6474
_module: "{{ item.key }}"
65-
when: icingaweb2_modules[_module].certificate_files is defined
75+
when: vars['icingaweb2_modules'][_module]['certificate_files'] is defined
6676
changed_when: false

0 commit comments

Comments
 (0)