From 830d3fc5ce34cca6aba805bf76a06b8272204b46 Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Fri, 10 Sep 2021 21:30:44 +0200 Subject: [PATCH 1/6] Ensure that log dir exists because Postgres does not create it by itself if you change it from the default value --- README.md | 4 +++- defaults/main.yml | 2 ++ tasks/initialize.yml | 8 ++++++++ tasks/variables.yml | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a30ec22..621152b 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,10 @@ Control the state of the postgresql service and whether it should start at boot postgresql_global_config_options: - option: unix_socket_directories value: '{{ postgresql_unix_socket_directories | join(",") }}' + - option: log_directory + value: 'log' -Global configuration options that will be set in `postgresql.conf`. Note that for RHEL/CentOS 6 (or very old versions of PostgreSQL), you need to at least override this variable and set the `option` to `unix_socket_directory`. +Global configuration options that will be set in `postgresql.conf`. Note that if you override 'log_directory' with another, absolute path, this role will create it for you. postgresql_hba_entries: - { type: local, database: all, user: postgres, auth_method: peer } diff --git a/defaults/main.yml b/defaults/main.yml index 4e16779..104dd1b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -23,6 +23,8 @@ postgresql_service_enabled: true postgresql_global_config_options: - option: unix_socket_directories value: '{{ postgresql_unix_socket_directories | join(",") }}' + - option: log_directory + value: 'log' # Host based authentication (hba) entries to be added to the pg_hba.conf. This # variable's defaults reflect the defaults that come with a fresh installation. diff --git a/tasks/initialize.yml b/tasks/initialize.yml index 0183121..eec71e1 100644 --- a/tasks/initialize.yml +++ b/tasks/initialize.yml @@ -14,6 +14,14 @@ state: directory mode: 0700 +- name: Ensure PostgreSQL log directory exists. + file: + path: "{{ postgresql_effective_log_dir }}" + owner: "{{ postgresql_user }}" + group: "{{ postgresql_group }}" + state: directory + mode: 0700 + - name: Check if PostgreSQL database is initialized. stat: path: "{{ postgresql_data_dir }}/PG_VERSION" diff --git a/tasks/variables.yml b/tasks/variables.yml index 5758972..cec21d3 100644 --- a/tasks/variables.yml +++ b/tasks/variables.yml @@ -49,3 +49,21 @@ postgresql_unix_socket_directories_mode: >- {{ __postgresql_unix_socket_directories_mode | default('02775') }} when: postgresql_unix_socket_directories_mode is not defined + +- name: Define postgresql_log_dir. + set_fact: + postgresql_log_dir: "{{ (postgresql_global_config_options | items2dict(key_name='option', value_name='value')).log_directory }}" + +- name: Define postgresql_log_dir_is_absolute. + set_fact: + postgresql_log_dir_is_absolute: '{{ postgresql_log_dir | regex_search("^/.*") != "" }}' + +- name: Define postgresql_effective_log_dir, if postgresql_log_dir_is_absolute + set_fact: + postgresql_effective_log_dir: '{{ postgresql_log_dir }}' + when: postgresql_log_dir_is_absolute + +- name: Define postgresql_effective_log_dir, if not postgresql_log_dir_is_absolute + set_fact: + postgresql_effective_log_dir: '{{ postgresql_data_dir }}/{{ postgresql_log_dir }}' + when: not postgresql_log_dir_is_absolute From 4c8e5e445217e92a1743ac9887f1e3b4e1d60e5e Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Wed, 22 Sep 2021 13:35:25 +0200 Subject: [PATCH 2/6] We create non-absolute log dir too --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 621152b..c7b4a6e 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Control the state of the postgresql service and whether it should start at boot - option: log_directory value: 'log' -Global configuration options that will be set in `postgresql.conf`. Note that if you override 'log_directory' with another, absolute path, this role will create it for you. +Global configuration options that will be set in `postgresql.conf`. Note that if you override 'log_directory' with another path, this role will create it for you. postgresql_hba_entries: - { type: local, database: all, user: postgres, auth_method: peer } From bdff17d22f1c9cf3f88f0629a06549c8e9ab55d3 Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Fri, 17 Dec 2021 17:52:31 +0100 Subject: [PATCH 3/6] Restore info about unix_socket_directories and make it more precise. (Info about RHEL/Centos 6 can be removed as it is not supported by this module anymore.) --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c7b4a6e..70c86cf 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,9 @@ Control the state of the postgresql service and whether it should start at boot value: '{{ postgresql_unix_socket_directories | join(",") }}' - option: log_directory value: 'log' - -Global configuration options that will be set in `postgresql.conf`. Note that if you override 'log_directory' with another path, this role will create it for you. +Global configuration options that will be set in `postgresql.conf`. +For PostgreSQL versions older than 9.3 you need to at least override this variable and set the `option` to `unix_socket_directory`. +If you override the value of `option: log_directory` with another path, relative or absolute, then this role will create it for you. postgresql_hba_entries: - { type: local, database: all, user: postgres, auth_method: peer } From b4a84c41f9a331296551419d3ec0baa5122f6681 Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Sun, 10 Jul 2022 10:07:56 +0200 Subject: [PATCH 4/6] Simplify and explain the code --- tasks/variables.yml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tasks/variables.yml b/tasks/variables.yml index cec21d3..cb5610b 100644 --- a/tasks/variables.yml +++ b/tasks/variables.yml @@ -52,18 +52,16 @@ - name: Define postgresql_log_dir. set_fact: + # postgresql_global_config_options is an array but its keys are unique, so it can be converted to dict, + # to easily get the value under the 'log_directory' key postgresql_log_dir: "{{ (postgresql_global_config_options | items2dict(key_name='option', value_name='value')).log_directory }}" -- name: Define postgresql_log_dir_is_absolute. - set_fact: - postgresql_log_dir_is_absolute: '{{ postgresql_log_dir | regex_search("^/.*") != "" }}' - -- name: Define postgresql_effective_log_dir, if postgresql_log_dir_is_absolute +- name: Define postgresql_effective_log_dir, if postgresql_log_dir is absolute set_fact: postgresql_effective_log_dir: '{{ postgresql_log_dir }}' - when: postgresql_log_dir_is_absolute + when: postgresql_log_dir is regex("^/.*") -- name: Define postgresql_effective_log_dir, if not postgresql_log_dir_is_absolute +- name: Define postgresql_effective_log_dir, if postgresql_log_dir is relative set_fact: postgresql_effective_log_dir: '{{ postgresql_data_dir }}/{{ postgresql_log_dir }}' - when: not postgresql_log_dir_is_absolute + when: postgresql_log_dir is not regex("^/.*") From 590df03eb0c369bceb0c30c571e6ae0d763da4da Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Sun, 10 Jul 2022 10:14:30 +0200 Subject: [PATCH 5/6] Simplify more as we need to match only the beginning of the string --- tasks/variables.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/variables.yml b/tasks/variables.yml index cb5610b..9120dc2 100644 --- a/tasks/variables.yml +++ b/tasks/variables.yml @@ -59,9 +59,9 @@ - name: Define postgresql_effective_log_dir, if postgresql_log_dir is absolute set_fact: postgresql_effective_log_dir: '{{ postgresql_log_dir }}' - when: postgresql_log_dir is regex("^/.*") + when: postgresql_log_dir is match("/") - name: Define postgresql_effective_log_dir, if postgresql_log_dir is relative set_fact: postgresql_effective_log_dir: '{{ postgresql_data_dir }}/{{ postgresql_log_dir }}' - when: postgresql_log_dir is not regex("^/.*") + when: postgresql_log_dir is not match("/") From 15076b64a21e4b3597f894ce2723984916ee6eec Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Mon, 11 Jul 2022 18:19:16 +0200 Subject: [PATCH 6/6] Fix: initdb requires target dir to be empty --- tasks/initialize.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tasks/initialize.yml b/tasks/initialize.yml index eec71e1..8636a40 100644 --- a/tasks/initialize.yml +++ b/tasks/initialize.yml @@ -14,14 +14,6 @@ state: directory mode: 0700 -- name: Ensure PostgreSQL log directory exists. - file: - path: "{{ postgresql_effective_log_dir }}" - owner: "{{ postgresql_user }}" - group: "{{ postgresql_group }}" - state: directory - mode: 0700 - - name: Check if PostgreSQL database is initialized. stat: path: "{{ postgresql_data_dir }}/PG_VERSION" @@ -35,3 +27,11 @@ # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509 vars: ansible_ssh_pipelining: true + +- name: Ensure PostgreSQL log directory exists. + file: + path: "{{ postgresql_effective_log_dir }}" + owner: "{{ postgresql_user }}" + group: "{{ postgresql_group }}" + state: directory + mode: 0700