Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the the original PostgreSQL configuration file #41

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ postgresql_server_auth: []
# Recursively reset the owner/group of the postgres datadir?
postgresql_server_chown_datadir: false

postgres_config_file_contents: ''
######################################################################
# Internal role variables, do not modify
######################################################################
Expand Down Expand Up @@ -58,7 +59,7 @@ postgresql_dist_redhat:
postgresql_dist_debian:
bindir: /usr/lib/postgresql/{{ postgresql_version }}/bin
confdir: /etc/postgresql/{{ postgresql_version }}/main
conf_postgresql_src: postgresql-conf-10-ubuntu.j2
conf_postgresql_src: postgresql-conf.j2
datadir: /var/lib/postgresql/{{ postgresql_version }}/main
basename: postgresql-{{ postgresql_version }}
service: postgresql
13 changes: 11 additions & 2 deletions molecule/resources/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ def test_server_listen(host):
assert listen_addresses == "listen_addresses = localhost"


def test_backup_config_exist(host):
version = get_version(host)
if host.system_info.distribution == 'rocky':
config_backup = '/var/lib/pgsql/{version}/data/postgresql.conf.backup'
else:
config_backup = '/etc/postgresql/{version}/main/postgresql.conf.backup'
with host.sudo():
backup_file = config_backup.format(version=version)
assert host.file(backup_file).is_file


def test_psql_version(host):
ver = get_version(host)
out = host.check_output('psql --version')
assert out.startswith('psql (PostgreSQL) {}.'.format(ver))


# Create

def createdb(host, db, should_pass, password, name):
try:
host.check_output(
Expand Down
4 changes: 2 additions & 2 deletions molecule/resources/tests/test_extra_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_server_log_file_name(host):
# Check previous day too in case this is run at midnight
version = get_version(host)
if host.system_info.distribution == 'rocky':
logdir = '/var/lib/pgsql/{version}/data/pg_log'
logdir = '/var/lib/pgsql/{version}/data/log'
else:
logdir = '/var/lib/postgresql/{version}/main/pg_log'
logdir = '/var/lib/postgresql/{version}/main/log'
date1 = datetime.today()
date0 = date1 - timedelta(days=1)
logdir = logdir.format(version=version)
Expand Down
63 changes: 57 additions & 6 deletions tasks/initialise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,78 @@
PGSETUP_INITDB_OPTIONS: >-
--encoding=UTF8 --locale=en_US.UTF-8 --auth-host=md5

- name: postgres | postgresql config file
template:
- name: postgres | Check for presence of "Modified by ome postgresql ansible role" in file

Check failure on line 25 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (rockylinux9)

25:81 [line-length] line too long (94 > 80 characters)

Check failure on line 25 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu2204)

25:81 [line-length] line too long (94 > 80 characters)
ansible.builtin.lineinfile:
path: "{{ postgresql_dist_confdir }}/postgresql.conf"
line: "#Modified by ome postgresql ansible role"
sbesson marked this conversation as resolved.
Show resolved Hide resolved
state: absent
check_mode: yes

Check warning on line 30 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (rockylinux9)

30:19 [truthy] truthy value should be one of [false, true]

Check warning on line 30 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu2204)

30:19 [truthy] truthy value should be one of [false, true]
changed_when: false
register: config_file_changed
sbesson marked this conversation as resolved.
Show resolved Hide resolved

- name: postgres | Check that the postgresql.conf.backup file exists
ansible.builtin.stat:
path: "{{ postgresql_dist_confdir }}/postgresql.conf.backup"
register: backup_result


# read the default postgresql configuration file
- name: postgres | get the postgres conf file contents
become_user: "{{ postgresql_become_user }}"
ansible.builtin.slurp:
src: "{{ postgresql_dist_confdir }}/postgresql.conf"
register: postgres_config_file_contents_o
when:
- not backup_result.stat.exists

# read the default postgresql configuration file
- name: postgres | get the postgres conf file contents from backup
become_user: "{{ postgresql_become_user }}"
ansible.builtin.slurp:
src: "{{ postgresql_dist_confdir }}/postgresql.conf.backup"
register: postgres_config_file_contents_b
when: backup_result.stat.exists

- set_fact: postgres_config_file_contents={{ postgres_config_file_contents_o }}

Check failure on line 57 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (rockylinux9)

57:81 [line-length] line too long (83 > 80 characters)

Check failure on line 57 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu2204)

57:81 [line-length] line too long (83 > 80 characters)
when:
- not backup_result.stat.exists

- set_fact: postgres_config_file_contents={{ postgres_config_file_contents_b }}

Check failure on line 61 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (rockylinux9)

61:81 [line-length] line too long (83 > 80 characters)

Check failure on line 61 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu2204)

61:81 [line-length] line too long (83 > 80 characters)
when:
- backup_result.stat.exists

- name: postgres | Copy a postgresql.conf to postgresql.conf.backup
become_user: "{{ postgresql_become_user }}"
ansible.builtin.copy:
src: "{{ postgresql_dist_confdir }}/postgresql.conf"
dest: "{{ postgresql_dist_confdir }}/postgresql.conf.backup"
Copy link
Member

@sbesson sbesson Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly the logic, this is meant to create a one-off copy of the configuration file assuming the latter has not originally been modified (and templated) by Ansible.

.backup implies to me that a copy of the modified configuration. Would a different suffix e.g. .orig would better convey that this is meant to be the version of the configuration installed by the package manager?

remote_src: yes

Check warning on line 70 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (rockylinux9)

70:21 [truthy] truthy value should be one of [false, true]

Check warning on line 70 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu2204)

70:21 [truthy] truthy value should be one of [false, true]
when:
- not backup_result.stat.exists
- config_file_changed.found | default(0) == 0

- name: postgres | copy postgresql config file
become_user: "{{ postgresql_become_user }}"
ansible.builtin.template:
dest: >-
{{ postgresql_dist_confdir }}/postgresql.conf
src: "{{ postgresql_dist_conf_postgresql_src }}"
mode: 0644
owner: "{{ postgresql_become_user }}"
notify:
- restart postgresql

become_user: "{{ postgresql_become_user }}"
sbesson marked this conversation as resolved.
Show resolved Hide resolved
when:
- config_file_changed.found | default(0) == 0 or backup_result.stat.exists

Check failure on line 86 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (rockylinux9)

86:81 [line-length] line too long (82 > 80 characters)

Check failure on line 86 in tasks/initialise.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu2204)

86:81 [line-length] line too long (82 > 80 characters)

- name: postgres | configure client authorisation
become_user: "{{ postgresql_become_user }}"
template:
dest: "{{ postgresql_dist_confdir }}/pg_hba.conf"
src: pg_hba-conf.j2
mode: 0640
notify:
- restart postgresql

become_user: "{{ postgresql_become_user }}"

- name: postgres | start service
service:
enabled: true
Expand Down
Loading
Loading