Skip to content

Commit 4693f16

Browse files
authored
Merge pull request #27 from claranet/ssl_by_default_2
Enable SSL by default and change default value of postgresql_config_change_allow_restart to false
2 parents eb5d36a + 9405d4d commit 4693f16

File tree

20 files changed

+138
-22
lines changed

20 files changed

+138
-22
lines changed

.ansible-lint

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
profile: production # min, basic, moderate, safety, shared, production
3+
exclude_paths:
4+
- .github/workflows/galaxy.yml

.github/workflows/molecule.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ jobs:
3737
steps:
3838
- name: checkout
3939
uses: actions/checkout@v4
40-
with:
41-
path: "${{ github.repository }}"
4240
- name: Run ansible lint
43-
uses: ansible/ansible-lint[email protected]
41+
uses: ansible/ansible-lint@main
4442
with:
45-
path: "."
43+
args: ""
44+
setup_python: "true"
45+
working_directory: ""
46+
requirements_file: "./molecule/shared/tools/requirements.yml"
47+
4648

4749
test:
4850
name: Scenario "${{ matrix.scenario }}", pg-${{ matrix.postgresql_version }} on ${{ matrix.config.image }}:${{ matrix.config.tag }}

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ git clone <link-to-this-repository> ansible-role-postgresql
4646
cd ansible-role-postgresql
4747
python3 -m venv env # Create a virtual environnement
4848
source env/bin/activate # Activate the environnement
49-
pip3 install ansible ansible-lint molecule[docker] # Install the python packages in the virutal environnement
49+
pip3 install ansible ansible-lint molecule-plugins[docker] # Install the python packages in the virutal environnement
5050
```
5151

5252
After setting up the environnement you can use molecule to test the role as you wish. If you are not very familiar with molecule, checkout the following table for some basic commands you can run against this role.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,16 @@ postgresql_hba_raw: |
181181
postgresql_config_change_allow_restart: true
182182
# Controls running tasks handling: configuration
183183
postgresql_configure: true
184+
# Enable SSL
185+
postgresql_ca_enabled: true
186+
# Certificate file subject used during generation
187+
postgresql_ssl_cert_subj: /C=FR/ST=FR
184188
```
185189
186190
_Notes:_
187191
192+
SSL configuration (introduced in `v3.0.0`) is enabled by default. The associated key and cert files are only regenerated if they are missing on the remote host.
193+
188194
By default, this role restarts the PostgreSQL service during subsequent configuration changes after the initial engine installation, ensuring all changes are applied immediately. However, this behavior can cause potential service outages.
189195

190196
To prevent automatic restarts, you can set the variable `postgresql_config_change_allow_restart` (introduced in `v2.1.0`) to `false`. Starting with (`v3.0.0`), the default value of this variable will change to `false`, meaning the role will avoid restarting PostgreSQL by default. If you rely on the current behavior, you will need to explicitly set this variable to true in your configuration.
@@ -603,6 +609,10 @@ postgresql_tempfile_dest_path: /etc/tmpfiles.d/postgresql-common.conf
603609
postgresql_tempfile_mode: '0644'
604610
postgresql_tempfile_owner: root
605611
postgresql_tempfile_group: root
612+
# SSL cert file path, can be absolute or relative (to data dir)
613+
# postgresql_ssl_cert_file: (default is os specific. vars/<os>.yml)
614+
# SSL key file path, can be absolute or relative (to data dir)
615+
# postgresql_ssl_key_file: (default is os specific. vars/<os>.yml)
606616
607617
# Controls running tasks handling: cluster initialization
608618
postgresql_initialize: true

defaults/main.yml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ postgresql_unix_socket_directories:
3939
- /run/postgresql
4040
# Permissions for the PostgreSQL unix sockets (default is distro dependant)
4141
postgresql_unix_socket_directories_mode: ''
42-
# Allow service restart for configuration changes that require it
43-
postgresql_config_change_allow_restart: "{{ (postgresql_restarted_state | d('restarted')) == 'restarted' }}"
42+
# Allow service restart for conf changes that require it (default changed to false in v3.0.0+)
43+
postgresql_config_change_allow_restart: false
44+
# SSL related variables
45+
# Enable SSL
46+
postgresql_ca_enabled: true
47+
# SSL cert file path, can be absolute or relative
48+
# postgresql_ssl_cert_file: (default is os specific. vars/<os>.yml)
49+
# SSL key file path, can be absolute or relative
50+
# postgresql_ssl_key_file: (default is os specific. vars/<os>.yml)
51+
# Certificate file subject used during generation
52+
postgresql_ssl_cert_subj: /C=FR/ST=FR
4453

4554

4655
# Global configuration options that will be set in postgresql.conf.
@@ -61,6 +70,13 @@ postgresql_global_config_options:
6170
value: "{{ _postgresql_config_path }}/pg_hba.conf"
6271
- option: max_connections
6372
value: "{{ postgresql_max_connections }}"
73+
- option: ssl
74+
value: "{{ 'on' if postgresql_ca_enabled else 'off' }}"
75+
- option: ssl_cert_file
76+
value: '{{ postgresql_ssl_cert_file }}'
77+
- option: ssl_key_file
78+
value: '{{ postgresql_ssl_key_file }}'
79+
6480
# Extra configuration options that are always inserted inside postgresql.conf
6581
postgresql_global_config_options_extra: []
6682
# Actual postgresql log directory
@@ -370,7 +386,8 @@ postgresql_uninstall_1: false
370386
postgresql_uninstall_2: false
371387

372388

373-
# Tells the role that the PostgreSQL instance is managed by Patroni therefore automatically disabling some features initialization,auto tuning,regular configuration, actual replication configuration
389+
# Tells the role that the PostgreSQL instance is managed by Patroni therefore automatically
390+
# disabling some features initialization,auto tuning,regular configuration, actual replication configuration
374391
postgresql_is_patroni: false
375392
# When combined with postgresql_install:true, this essentially skips all remaining tasks after packages installation
376393
postgresql_only_install: false

molecule/all_features/verify.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
- name: Include default verification
77
ansible.builtin.include_tasks: tasks/verify_default.yml
88

9-
- name: Include default verification
9+
- name: Include vacuum verification
1010
ansible.builtin.include_tasks: tasks/verify_vacuum.yml

molecule/cluster/verify.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ansible.builtin.command: psql -q -A -t -c "SELECT datname FROM pg_database"
1818
register: databases
1919
changed_when: false
20+
become: true
2021
become_user: postgres
2122
vars:
2223
ansible_ssh_pipelining: true
@@ -37,15 +38,16 @@
3738
vars:
3839
ansible_ssh_pipelining: true
3940

40-
- name: Retrieve databases on replica nodes
41+
- name: Retrieve databases on replica nodes to check deletion
4142
ansible.builtin.command: psql -q -A -t -c "SELECT datname FROM pg_database"
4243
register: databases
4344
changed_when: false
45+
become: true
4446
become_user: postgres
4547
vars:
4648
ansible_ssh_pipelining: true
4749

48-
- name: Assert db created on primary is also present on the replicas
50+
- name: Assert db not created on primary is also present on the replicas
4951
ansible.builtin.assert:
5052
that: '"db2" not in databases.stdout_lines'
5153

molecule/shared/tasks/verify_default.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@
5656
msg: "Timeout waiting for 5432 to respond"
5757
register: port_check
5858
ignore_errors: true
59+
60+
- name: Retrieve ssl option value
61+
ansible.builtin.command: psql -q -A -t -c "show ssl"
62+
register: ssl_res
63+
changed_when: false
64+
become: true
65+
become_user: postgres
66+
vars:
67+
ansible_ssh_pipelining: true
68+
69+
- name: Assert ssl is on by default
70+
ansible.builtin.assert:
71+
that: '"on" == (ssl_res.stdout | trim)'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
22
- name: Check if vacuum script works
33
ansible.builtin.command: /var/scripts/pgsql_vacuumDB.sh vacuumanalyze
4+
changed_when: true
45
become: true
56
become_user: postgres
67

78
- name: Check min last vacuum date
89
ansible.builtin.command: psql db1 -c "select min(last_vacuum) from pg_stat_user_tables;"
10+
changed_when: false
911
become: true
1012
become_user: postgres

molecule/shared/vars/main_all_features.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ postgresql_global_config_options_extra:
1010
- option: log_statement
1111
value: all
1212
- option: logging_collector
13-
value: on
13+
value: 'on'
1414
postgresql_hba_entries_extra: []
1515
# - {contype: local, databases: all, users: postgres, method: peer}
1616

@@ -36,20 +36,20 @@ postgresql_users:
3636
postgresql_memberships:
3737
# Ensure the role 'user1' belongs to group 'group1'
3838
- groups:
39-
- group1
39+
- group1
4040
target_roles:
41-
- user1
41+
- user1
4242
state: present
4343
# Ensure the role 'user2' does not belong to the group 'group2'
4444
- groups:
45-
- group2
45+
- group2
4646
target_roles:
47-
- user2
47+
- user2
4848
state: absent
4949
# Ensure the role 'jdoe' does not belong to any group
5050
- groups: []
5151
target_roles:
52-
- jdoe
52+
- jdoe
5353
state: exact
5454

5555
postgresql_tablespaces:

0 commit comments

Comments
 (0)