Skip to content

Commit a843d2b

Browse files
committed
Custom configuration options and server listen addresses
It is now possible to customize PostgreSQL configuration with role vars. The listen_addresses configuration can be set as a separate variable.
1 parent 451496a commit a843d2b

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed

.github/workflows/molecule.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ jobs:
1717
run: pip3 install -r .dev_requirements.txt
1818

1919
- name: Test playbook
20-
run: molecule test -- -e opencast_postgresql_password=123
20+
run: molecule test
2121
env:
2222
PY_COLORS: '1'

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@ Role Variables
2020
- `opencast_postgresql_version`
2121
- PostgreSQL major version to install (default: `12`)
2222
- Enables CentOS AppStream
23-
- `opencast_postgresql_user:`
23+
- `opencast_postgresql_user`
2424
- Database user to create (default: `opencast`)
2525
- `opencast_postgresql_password`
2626
- Databse password for user (_required_)
2727
- `opencast_postgresql_database`
2828
- Database name (default: `opencast`)
29+
- `opencast_postgresql_listen_addresses`
30+
- List of IP addresses the server should listen on (default: `["localhost"]`).
31+
- Use `*` to listen on all IP addresses.
32+
- For more information please consult PostgreSQL documentation for the configuration `listen_addresses`
2933
- `opencast_postgresql_connection_hosts`
30-
- List of hosts allowed to connect to database (default: `[127.0.0.1/32, ::1/128]`)
34+
- List of IP ranges allowed to connect to database (default: `[127.0.0.1/32, ::1/128]`)
35+
- `opencast_postgresql_extra_configs`
36+
- Additional configurations as dictionary (default: `{}`)
37+
- Please consult PostgreSQL documentation for available configurations
3138

3239

3340
Example Playbook
@@ -42,3 +49,22 @@ Example of how to configure and use the role:
4249
- role: elan.opencast_postgresql
4350
opencast_postgresql_password: secret
4451
```
52+
53+
More complex example with custom configurations and listening on all IP addresses is shown here:
54+
55+
```yaml
56+
- hosts: servers
57+
become: true
58+
roles:
59+
- role: elan.opencast_postgresql
60+
opencast_postgresql_password: secret
61+
opencast_postgresql_extra_configs:
62+
max_connections: 1000 # Increased value for production use
63+
log_destination: "'syslog'" # Log to syslog
64+
opencast_postgresql_listen_addresses:
65+
- "*" # Listen on all IP addresses
66+
opencast_postgresql_connection_hosts:
67+
- "127.0.0.1/32"
68+
- "::1/128"
69+
- "10.10.10.1/24" # Clients IP range
70+
```

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
opencast_postgresql_version: 16
44
opencast_postgresql_user: opencast
55
opencast_postgresql_database: opencast
6+
opencast_postgresql_listen_addresses:
7+
- "localhost"
68
opencast_postgresql_connection_hosts:
79
- 127.0.0.1/32
810
- ::1/128
11+
opencast_postgresql_extra_configs: {}

molecule/default/converge.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
---
2+
23
- name: Converge
34
hosts: all
4-
tasks:
5-
- name: "Include opencast_postgresql"
6-
ansible.builtin.include_role:
7-
name: elan.opencast_postgresql
5+
roles:
6+
- role: elan.opencast_postgresql
7+
opencast_postgresql_password: secret
8+
opencast_postgresql_extra_configs:
9+
max_connections: 1000 # Increase value for production use
10+
log_destination: "'syslog'" # Log to syslog
11+
opencast_postgresql_listen_addresses:
12+
- "*" # Listen on all IP addresses
13+
opencast_postgresql_connection_hosts:
14+
- "127.0.0.1/32"
15+
- "::1/128"
16+
- "10.10.10.1/24" # Clients IP range

molecule/default/verify.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,34 @@
3939
ansible.builtin.debug:
4040
msg: "PostgreSQL version on {{ inventory_hostname }} (Debian): {{ psql_version_debian.stdout }}"
4141
when: ansible_os_family == "Debian"
42+
43+
- name: Find configuration file
44+
ansible.builtin.set_fact:
45+
config_file_dir: "{{ paths[ansible_os_family] }}"
46+
vars:
47+
paths:
48+
RedHat: "/var/lib/pgsql/{{ opencast_postgresql_version }}/data"
49+
Debian: "/etc/postgresql/{{ opencast_postgresql_version }}/main"
50+
51+
- name: Read configuration file
52+
ansible.builtin.slurp:
53+
src: "{{ config_file_dir }}/postgresql.conf"
54+
register: config_file
55+
56+
- name: Test config set
57+
ansible.builtin.assert:
58+
that:
59+
- '"listen_addresses = ''*''" in (config_file.content | b64decode)'
60+
- '"max_connections = 1000" in (config_file.content | b64decode)'
61+
- '"log_destination = ''syslog''" in (config_file.content | b64decode)'
62+
63+
- name: Read pg_hba.conf configuration file
64+
ansible.builtin.slurp:
65+
src: "{{ config_file_dir }}/pg_hba.conf"
66+
register: hba_config_file
67+
68+
- name: Test config set
69+
ansible.builtin.assert:
70+
that:
71+
- '"host all all 127.0.0.1/32 scram-sha-256" in (hba_config_file.content | b64decode)'
72+
- '"host all all 10.10.10.1/24 scram-sha-256" in (hba_config_file.content | b64decode)'

tasks/main.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@
5050
notify: Restart Postgresql On CentOS
5151
when: ansible_os_family == "RedHat"
5252

53+
- name: Set listen_addresses (CentOS/RHEL)
54+
ansible.builtin.lineinfile:
55+
path: "/var/lib/pgsql/{{ opencast_postgresql_version }}/data/postgresql.conf"
56+
regexp: '#?\s*listen_addresses\s*='
57+
line: "listen_addresses = '{{ opencast_postgresql_listen_addresses | join(', ') }}'"
58+
notify: Restart Postgresql On CentOS
59+
when: ansible_os_family == "RedHat"
60+
61+
- name: Set extra configs (CentOS/RHEL)
62+
ansible.builtin.lineinfile:
63+
path: "/var/lib/pgsql/{{ opencast_postgresql_version }}/data/postgresql.conf"
64+
regexp: '#?\s*{{ item.key }}\s*='
65+
line: "{{ item.key }} = {{ item.value }}"
66+
loop: "{{ opencast_postgresql_extra_configs | dict2items }}"
67+
notify: Restart Postgresql On CentOS
68+
when: ansible_os_family == "RedHat"
69+
5370
- name: Start and enable PostgreSQL (CentOS/RHEL)
5471
ansible.builtin.service:
5572
name: "postgresql-{{ opencast_postgresql_version }}"
@@ -119,6 +136,23 @@
119136
notify: Restart Postgresql On Debian/Ubuntu
120137
when: ansible_os_family == "Debian"
121138

139+
- name: Set listen_addresses (Debian/Ubuntu)
140+
ansible.builtin.lineinfile:
141+
path: "/etc/postgresql/{{ opencast_postgresql_version }}/main/postgresql.conf"
142+
regexp: '#?\s*listen_addresses\s*='
143+
line: "listen_addresses = '{{ opencast_postgresql_listen_addresses | join(', ') }}'"
144+
notify: Restart Postgresql On Debian/Ubuntu
145+
when: ansible_os_family == "Debian"
146+
147+
- name: Set extra configs (Debian/Ubuntu)
148+
ansible.builtin.lineinfile:
149+
path: "/etc/postgresql/{{ opencast_postgresql_version }}/main/postgresql.conf"
150+
regexp: '#?\s*{{ item.key }}\s*='
151+
line: "{{ item.key }} = {{ item.value }}"
152+
loop: "{{ opencast_postgresql_extra_configs | dict2items }}"
153+
notify: Restart Postgresql On Debian/Ubuntu
154+
when: ansible_os_family == "Debian"
155+
122156
- name: Ensure PostgreSQL is started and enabled (Debian/Ubuntu)
123157
ansible.builtin.service:
124158
name: postgresql

0 commit comments

Comments
 (0)