From 830d3fc5ce34cca6aba805bf76a06b8272204b46 Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Fri, 10 Sep 2021 21:30:44 +0200 Subject: [PATCH] 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