From 9ba86d7d209512a9de9dada54054e99de5bf28cb Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 13:53:16 +0200 Subject: [PATCH 01/24] Fixed GUC_LIST_QUOTE parameters --- changelogs/fragments/-postgresql_set.yml | 2 + plugins/modules/postgresql_set.py | 50 ++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/-postgresql_set.yml diff --git a/changelogs/fragments/-postgresql_set.yml b/changelogs/fragments/-postgresql_set.yml new file mode 100644 index 000000000..8ed23b3db --- /dev/null +++ b/changelogs/fragments/-postgresql_set.yml @@ -0,0 +1,2 @@ +bugfixes: +- postgresql_set - fixed GUC_LIST_QUOTE parameters (https://github.com/ansible-collections/community.postgresql/pull/). diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 6314c541c..fe9ef35f7 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -194,12 +194,40 @@ # To allow to set value like 1mb instead of 1MB, etc: LOWERCASE_SIZE_UNITS = ("mb", "gb", "tb") +# GUC_LIST_QUOTE parameters list for each version where they changed (from PG_REQ_VER). +# It is a tuple of tuples as we need to iterate it in order. +PARAMETERS_GUC_LIST_QUOTE = ( + (140000, ( + 'local_preload_libraries', + 'search_path', + 'session_preload_libraries', + 'shared_preload_libraries', + 'temp_tablespaces', + 'unix_socket_directories' + )), + (90400, ( + 'local_preload_libraries', + 'search_path', + 'session_preload_libraries', + 'shared_preload_libraries', + 'temp_tablespaces' + )), +) + + # =========================================== # PostgreSQL module specific support methods. # -def param_get(cursor, module, name): +def param_is_guc_list_quote(server_version, name): + for guc_list_quote_ver, guc_list_quote_params in PARAMETERS_GUC_LIST_QUOTE: + if server_version >= guc_list_quote_ver: + return name in guc_list_quote_params + return False + + +def param_get(cursor, module, name, is_guc_list_quote): query = ("SELECT name, setting, unit, context, boot_val " "FROM pg_settings WHERE name = %(name)s") try: @@ -242,6 +270,19 @@ def param_get(cursor, module, name): unit = 'b' + if is_guc_list_quote: + # unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) + raw_vals_unquoted = [] + for v in raw_val.split(','): + v = v.strip() # strip whitespaces at start/end + if v[0] == v[-1] == '"': + # is quoted -> strip quotes + raw_vals_unquoted.append(v[1:-1]) + else: + # is not quoted -> no changes + raw_vals_unquoted.append(v) + raw_val = ', '.join(raw_vals_unquoted) + return { 'current_val': val[name], 'raw_val': raw_val, @@ -409,6 +450,9 @@ def main(): db_connection.close() module.exit_json(**kw) + # Check parameter is GUC_LIST_QUOTE (done once as depend only on server version) + is_guc_list_quote = param_is_guc_list_quote(ver, name) + # Set default returned values: restart_required = False changed = False @@ -416,7 +460,7 @@ def main(): kw['restart_required'] = False # Get info about param state: - res = param_get(cursor, module, name) + res = param_get(cursor, module, name, is_guc_list_quote) current_val = res['current_val'] raw_val = res['raw_val'] unit = res['unit'] @@ -483,7 +527,7 @@ def main(): db_connection, dummy = connect_to_db(module, conn_params, autocommit=True) cursor = db_connection.cursor(cursor_factory=DictCursor) - res = param_get(cursor, module, name) + res = param_get(cursor, module, name, is_guc_list_quote) # f_ means 'final' f_value = res['current_val'] f_raw_val = res['raw_val'] From 8adca06d73993e17a42184ff807c04764c56e0b9 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 14:38:39 +0200 Subject: [PATCH 02/24] Added tests --- .../postgresql_set/tasks/options_coverage.yml | 50 +++++++++++++++++-- .../setup_postgresql_db/tasks/main.yml | 4 +- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index c4598d2a9..f02ad252e 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -28,8 +28,9 @@ wal_level: replica log_statement: mod track_functions: none - shared_preload_libraries: 'pg_stat_statements, pgaudit' + shared_preload_libraries: 'pg_stat_statements, pgcrypto' log_line_prefix: 'db=%d,user=%u,app=%a,client=%h ' + unix_socket_directories: '/var/run/postgresql, /tmp' # Check mode: - name: Set settings in check mode @@ -51,16 +52,33 @@ with_dict: '{{ setting_map }}' # https://github.com/ansible-collections/community.postgresql/issues/78 - - name: Test param with comma containing values + - name: Test param with comma containing values but no quotes <<: *task_parameters shell: "grep shared_preload_libraries {{ pg_auto_conf }}" register: result - assert: that: - - result.stdout == "shared_preload_libraries = 'pg_stat_statements, pgaudit'" - - # Test for single-value params with commas and spaces in value + - result.stdout == "shared_preload_libraries = 'pg_stat_statements, pgcrypto'" + + # https://github.com/ansible-collections/community.postgresql/pull/ + # unix_socket_directories is a GUC_LIST_QUOTE parameter only from PostgreSQL 14 + - name: Test param with comma containing values and quotes + <<: *task_parameters + shell: "grep unix_socket_directories {{ pg_auto_conf }}" + register: result + + - assert: + that: + - result.stdout == "unix_socket_directories = '/var/run/postgresql, /tmp'" + - when: postgres_version_resp.stdout is version('14', '<') + + - assert: + that: + - result.stdout == "unix_socket_directories = '\"/var/run/postgresql\", \"/tmp\"'" + - when: postgres_version_resp.stdout is version('14', '>=') + + # https://github.com/ansible-collections/community.postgresql/pull/400 - name: Test single-value param with commas and spaces in value <<: *task_parameters shell: "grep log_line_prefix {{ pg_auto_conf }}" @@ -69,3 +87,25 @@ - assert: that: - result.stdout == "log_line_prefix = 'db=%d,user=%u,app=%a,client=%h '" + + # Restart PostgreSQL: + - name: Restart PostgreSQL + become: true + service: + name: "{{ postgresql_service }}" + state: restarted + + # Idempotence: + - name: Set settings in actual mode again after restart for idempotence + <<: *task_parameters + postgresql_set: + <<: *pg_parameters + name: '{{ item.key }}' + value: '{{ item.value }}' + register: test_idempotence + with_dict: '{{ setting_map }}' + + - name: Check idempotence after restart + assert: + that: not item.changed + with_items: test_idempotence.results diff --git a/tests/integration/targets/setup_postgresql_db/tasks/main.yml b/tests/integration/targets/setup_postgresql_db/tasks/main.yml index f5c39ad8b..35e06d2bd 100644 --- a/tests/integration/targets/setup_postgresql_db/tasks/main.yml +++ b/tests/integration/targets/setup_postgresql_db/tasks/main.yml @@ -75,7 +75,7 @@ become: true apt: autoremove: true - + - name: Create the file repository configuration lineinfile: create: true @@ -127,7 +127,7 @@ when: ansible_os_family == "RedHat" and ansible_service_mgr != "systemd" - name: Initialize postgres (Debian) - shell: . /usr/share/postgresql-common/maintscripts-functions && set_system_locale && /usr/bin/pg_createcluster -u postgres {{ pg_verĀ }} main + shell: . /usr/share/postgresql-common/maintscripts-functions && set_system_locale && /usr/bin/pg_createcluster -u postgres {{ pg_ver }} main args: creates: /etc/postgresql/{{ pg_ver }}/ when: ansible_os_family == 'Debian' From 3c4c8db50c36b6df72775f79e04c686c984fa103 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 15:28:56 +0200 Subject: [PATCH 03/24] Fixed tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index f02ad252e..760b13833 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -71,12 +71,12 @@ - assert: that: - result.stdout == "unix_socket_directories = '/var/run/postgresql, /tmp'" - - when: postgres_version_resp.stdout is version('14', '<') + when: postgres_version_resp.stdout is version('14', '<') - assert: that: - result.stdout == "unix_socket_directories = '\"/var/run/postgresql\", \"/tmp\"'" - - when: postgres_version_resp.stdout is version('14', '>=') + when: postgres_version_resp.stdout is version('14', '>=') # https://github.com/ansible-collections/community.postgresql/pull/400 - name: Test single-value param with commas and spaces in value From 65250e420698aea5a53e16b292afb8b7de1dbdf8 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 15:31:30 +0200 Subject: [PATCH 04/24] Added PR number --- .../fragments/{-postgresql_set.yml => 521-postgresql_set.yml} | 2 +- .../targets/postgresql_set/tasks/options_coverage.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename changelogs/fragments/{-postgresql_set.yml => 521-postgresql_set.yml} (55%) diff --git a/changelogs/fragments/-postgresql_set.yml b/changelogs/fragments/521-postgresql_set.yml similarity index 55% rename from changelogs/fragments/-postgresql_set.yml rename to changelogs/fragments/521-postgresql_set.yml index 8ed23b3db..8cbd1a370 100644 --- a/changelogs/fragments/-postgresql_set.yml +++ b/changelogs/fragments/521-postgresql_set.yml @@ -1,2 +1,2 @@ bugfixes: -- postgresql_set - fixed GUC_LIST_QUOTE parameters (https://github.com/ansible-collections/community.postgresql/pull/). +- postgresql_set - fixed GUC_LIST_QUOTE parameters (https://github.com/ansible-collections/community.postgresql/pull/521). diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 760b13833..7ade12409 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -61,7 +61,7 @@ that: - result.stdout == "shared_preload_libraries = 'pg_stat_statements, pgcrypto'" - # https://github.com/ansible-collections/community.postgresql/pull/ + # https://github.com/ansible-collections/community.postgresql/pull/521 # unix_socket_directories is a GUC_LIST_QUOTE parameter only from PostgreSQL 14 - name: Test param with comma containing values and quotes <<: *task_parameters From 9ef7316cd8c52262197ef520738584129e402223 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 15:56:03 +0200 Subject: [PATCH 05/24] Lint fix --- plugins/modules/postgresql_set.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index fe9ef35f7..6882e8cfb 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -205,7 +205,7 @@ 'temp_tablespaces', 'unix_socket_directories' )), - (90400, ( + (90400, ( 'local_preload_libraries', 'search_path', 'session_preload_libraries', @@ -274,7 +274,7 @@ def param_get(cursor, module, name, is_guc_list_quote): # unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) raw_vals_unquoted = [] for v in raw_val.split(','): - v = v.strip() # strip whitespaces at start/end + v = v.strip() # strip whitespaces at start/end if v[0] == v[-1] == '"': # is quoted -> strip quotes raw_vals_unquoted.append(v[1:-1]) From c62e9ed6e0d8afb0ab65e4f05fde10b511a02d0f Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:16:02 +0200 Subject: [PATCH 06/24] Fix --- plugins/modules/postgresql_set.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 6882e8cfb..694db52f4 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -275,7 +275,7 @@ def param_get(cursor, module, name, is_guc_list_quote): raw_vals_unquoted = [] for v in raw_val.split(','): v = v.strip() # strip whitespaces at start/end - if v[0] == v[-1] == '"': + if v and v[0] == v[-1] == '"': # is quoted -> strip quotes raw_vals_unquoted.append(v[1:-1]) else: From 1561c7094510324bba43012274bf9efbfdf3469a Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:45:15 +0200 Subject: [PATCH 07/24] Fixed tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 7ade12409..fd9f89f2d 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -30,7 +30,7 @@ track_functions: none shared_preload_libraries: 'pg_stat_statements, pgcrypto' log_line_prefix: 'db=%d,user=%u,app=%a,client=%h ' - unix_socket_directories: '/var/run/postgresql, /tmp' + unix_socket_directories: '/tmp, /var/run/postgresql' # Check mode: - name: Set settings in check mode @@ -108,4 +108,4 @@ - name: Check idempotence after restart assert: that: not item.changed - with_items: test_idempotence.results + with_items: '{{ test_idempotence.results }}' From 87c61bb24639e019fc510970a99fe3b9d37afe19 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:54:16 +0200 Subject: [PATCH 08/24] Fixed unquote --- plugins/modules/postgresql_set.py | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 694db52f4..75a5767a0 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -227,6 +227,20 @@ def param_is_guc_list_quote(server_version, name): return False +def param_guc_list_unquote(value): + # unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) + value_unquoted = [] + for v in value.split(','): + v = v.strip() # strip whitespaces at start/end + if v and v[0] == v[-1] == '"': + # is quoted -> strip quotes + value_unquoted.append(v[1:-1]) + else: + # is not quoted -> no changes + value_unquoted.append(v) + return ', '.join(value_unquoted) + + def param_get(cursor, module, name, is_guc_list_quote): query = ("SELECT name, setting, unit, context, boot_val " "FROM pg_settings WHERE name = %(name)s") @@ -244,15 +258,16 @@ def param_get(cursor, module, name, is_guc_list_quote): "Please check its spelling or presence in your PostgreSQL version " "(https://www.postgresql.org/docs/current/runtime-config.html)" % name) + current_val = val[name] raw_val = info['setting'] unit = info['unit'] context = info['context'] boot_val = info['boot_val'] - if val[name] == 'True': - val[name] = 'on' - elif val[name] == 'False': - val[name] = 'off' + if current_val == 'True': + current_val = 'on' + elif current_val == 'False': + current_val = 'off' if unit == 'kB': if int(raw_val) > 0: @@ -271,20 +286,11 @@ def param_get(cursor, module, name, is_guc_list_quote): unit = 'b' if is_guc_list_quote: - # unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) - raw_vals_unquoted = [] - for v in raw_val.split(','): - v = v.strip() # strip whitespaces at start/end - if v and v[0] == v[-1] == '"': - # is quoted -> strip quotes - raw_vals_unquoted.append(v[1:-1]) - else: - # is not quoted -> no changes - raw_vals_unquoted.append(v) - raw_val = ', '.join(raw_vals_unquoted) + current_val = param_guc_list_unquote(current_val) + raw_val = param_guc_list_unquote(raw_val) return { - 'current_val': val[name], + 'current_val': current_val, 'raw_val': raw_val, 'unit': unit, 'boot_val': boot_val, From 9d5a10f53a82d848dc0e077430e57023d0792530 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 17:22:48 +0200 Subject: [PATCH 09/24] Fix unix_socket_directories --- plugins/modules/postgresql_set.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 75a5767a0..0559b34f1 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -364,16 +364,21 @@ def pretty_to_bytes(pretty_val): return pretty_val -def param_set(cursor, module, name, value, context): +def param_set(cursor, module, name, value, context, server_version): try: if str(value).lower() == 'default': query = "ALTER SYSTEM SET %s = DEFAULT" % name else: - if isinstance(value, str) and ',' in value and not name.endswith(('_command', '_prefix')): + if isinstance(value, str) and \ + ',' in value and \ + not name.endswith(('_command', '_prefix')) and \ + not (server_version < 140000 and name == 'unix_socket_directories'): # Issue https://github.com/ansible-collections/community.postgresql/issues/78 # Change value from 'one, two, three' -> "'one','two','three'" # PR https://github.com/ansible-collections/community.postgresql/pull/400 # Parameter names ends with '_command' or '_prefix' can contains commas but are not lists + # PR https://github.com/ansible-collections/community.postgresql/pull/521 + # unix_socket_directories up to PostgreSQL 13 lacks GUC_LIST_INPUT and GUC_LIST_QUOTE options so it is a single value parameter value = ','.join(["'" + elem.strip() + "'" for elem in value.split(',')]) query = "ALTER SYSTEM SET %s = %s" % (name, value) else: @@ -509,7 +514,7 @@ def main(): # Set param (value can be an empty string): if value is not None and value != current_val: - changed = param_set(cursor, module, name, value, context) + changed = param_set(cursor, module, name, value, context, ver) kw['value_pretty'] = value @@ -523,7 +528,7 @@ def main(): ) module.exit_json(**kw) - changed = param_set(cursor, module, name, boot_val, context) + changed = param_set(cursor, module, name, boot_val, context, ver) cursor.close() db_connection.close() From dbdfd3bdfabaf1eeab389b1dd150566595aec24e Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 17:26:28 +0200 Subject: [PATCH 10/24] Fixed tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index fd9f89f2d..3531ba616 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -70,12 +70,12 @@ - assert: that: - - result.stdout == "unix_socket_directories = '/var/run/postgresql, /tmp'" + - result.stdout == "unix_socket_directories = '/tmp, /var/run/postgresql'" when: postgres_version_resp.stdout is version('14', '<') - assert: that: - - result.stdout == "unix_socket_directories = '\"/var/run/postgresql\", \"/tmp\"'" + - result.stdout == "unix_socket_directories = '"/tmp", "/var/run/postgresql"'" when: postgres_version_resp.stdout is version('14', '>=') # https://github.com/ansible-collections/community.postgresql/pull/400 From c8df01079abde6900a4b571eeb7dc5691f967b90 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 17:46:34 +0200 Subject: [PATCH 11/24] Lint fix --- plugins/modules/postgresql_set.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 0559b34f1..b9284bbcc 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -370,9 +370,9 @@ def param_set(cursor, module, name, value, context, server_version): query = "ALTER SYSTEM SET %s = DEFAULT" % name else: if isinstance(value, str) and \ - ',' in value and \ - not name.endswith(('_command', '_prefix')) and \ - not (server_version < 140000 and name == 'unix_socket_directories'): + ',' in value and \ + not name.endswith(('_command', '_prefix')) and \ + not (server_version < 140000 and name == 'unix_socket_directories'): # Issue https://github.com/ansible-collections/community.postgresql/issues/78 # Change value from 'one, two, three' -> "'one','two','three'" # PR https://github.com/ansible-collections/community.postgresql/pull/400 From 0fed5561c38199fcc338e5240aed52159313be9d Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 18:13:02 +0200 Subject: [PATCH 12/24] Fixed tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 3531ba616..befcc4385 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -75,7 +75,7 @@ - assert: that: - - result.stdout == "unix_socket_directories = '"/tmp", "/var/run/postgresql"'" + - result.stdout == "unix_socket_directories = '\"/tmp\", \"/var/run/postgresql\"'" when: postgres_version_resp.stdout is version('14', '>=') # https://github.com/ansible-collections/community.postgresql/pull/400 From 8d556722677e97bcf7bfc2aa1d2a0eac75b63028 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 18:44:30 +0200 Subject: [PATCH 13/24] Fixed tests --- .../postgresql_set/tasks/options_coverage.yml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index befcc4385..953be7a68 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -30,7 +30,16 @@ track_functions: none shared_preload_libraries: 'pg_stat_statements, pgcrypto' log_line_prefix: 'db=%d,user=%u,app=%a,client=%h ' - unix_socket_directories: '/tmp, /var/run/postgresql' + unix_socket_directories: '/var/run/postgresql, /tmp/postgresql' + + - name: Ensure /tmp/postgresql directory exists + file: + state: directory + path: /tmp/postgresql + owner: "{{ pg_user }}" + group: "{{ pg_group }}" + mode: 770 + become: true # Check mode: - name: Set settings in check mode @@ -70,12 +79,12 @@ - assert: that: - - result.stdout == "unix_socket_directories = '/tmp, /var/run/postgresql'" + - result.stdout == "unix_socket_directories = '/var/run/postgresql, /tmp/postgresql'" when: postgres_version_resp.stdout is version('14', '<') - assert: that: - - result.stdout == "unix_socket_directories = '\"/tmp\", \"/var/run/postgresql\"'" + - result.stdout == "unix_socket_directories = '\"/var/run/postgresql\", \"/tmp/postgresql\"'" when: postgres_version_resp.stdout is version('14', '>=') # https://github.com/ansible-collections/community.postgresql/pull/400 From cc8728b503d4f581c99ea2759c7d892cb5a92d6d Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:08:24 +0200 Subject: [PATCH 14/24] Fixed test --- .../targets/postgresql_set/tasks/options_coverage.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 953be7a68..1d6fb734b 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -32,14 +32,17 @@ log_line_prefix: 'db=%d,user=%u,app=%a,client=%h ' unix_socket_directories: '/var/run/postgresql, /tmp/postgresql' - - name: Ensure /tmp/postgresql directory exists + - name: Ensure unix_socket_directories exist file: state: directory - path: /tmp/postgresql + path: "{{ item }}" owner: "{{ pg_user }}" group: "{{ pg_group }}" mode: 770 become: true + with_list: + - /var/run/postgresql + - /tmp/postgresql # Check mode: - name: Set settings in check mode From 5c6dbb9ffa207cbf836eb938df1823a57fcaa5af Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:24:33 +0200 Subject: [PATCH 15/24] Fixed tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 1d6fb734b..29e2c913a 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -37,8 +37,8 @@ state: directory path: "{{ item }}" owner: "{{ pg_user }}" - group: "{{ pg_group }}" - mode: 770 + group: "{{ pg_user }}" + mode: 777 become: true with_list: - /var/run/postgresql From 3fa0e82f03872658616c64c9a89011e0b171021c Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:26:50 +0200 Subject: [PATCH 16/24] Fixed tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 29e2c913a..63c4c298e 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -38,7 +38,7 @@ path: "{{ item }}" owner: "{{ pg_user }}" group: "{{ pg_user }}" - mode: 777 + mode: '0777' become: true with_list: - /var/run/postgresql From 325667df1a787e5c6931d1ed6cb82421d62c46a2 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:38:56 +0200 Subject: [PATCH 17/24] debug --- .../postgresql_set/tasks/options_coverage.yml | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 63c4c298e..eef5c873d 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -11,6 +11,19 @@ login_db: postgres block: + + - name: Restart PostgreSQL + become: true + service: + name: "{{ postgresql_service }}" + state: restarted + ignore_errors: true + + - name: Check journalctl + become: true + shell: "journalctl -x -e --no-pager" + register: journalctl_res + - name: Define a test setting map set_fact: setting_map: @@ -30,7 +43,7 @@ track_functions: none shared_preload_libraries: 'pg_stat_statements, pgcrypto' log_line_prefix: 'db=%d,user=%u,app=%a,client=%h ' - unix_socket_directories: '/var/run/postgresql, /tmp/postgresql' + unix_socket_directories: '/var/run/postgresql, /var/run/postgresql2' - name: Ensure unix_socket_directories exist file: @@ -42,7 +55,7 @@ become: true with_list: - /var/run/postgresql - - /tmp/postgresql + - /var/run/postgresql2 # Check mode: - name: Set settings in check mode @@ -82,12 +95,12 @@ - assert: that: - - result.stdout == "unix_socket_directories = '/var/run/postgresql, /tmp/postgresql'" + - result.stdout == "unix_socket_directories = '/var/run/postgresql, /var/run/postgresql2'" when: postgres_version_resp.stdout is version('14', '<') - assert: that: - - result.stdout == "unix_socket_directories = '\"/var/run/postgresql\", \"/tmp/postgresql\"'" + - result.stdout == "unix_socket_directories = '\"/var/run/postgresql\", \"/var/run/postgresql2\"'" when: postgres_version_resp.stdout is version('14', '>=') # https://github.com/ansible-collections/community.postgresql/pull/400 @@ -106,6 +119,12 @@ service: name: "{{ postgresql_service }}" state: restarted + ignore_errors: true + + - name: Check journalctl + become: true + shell: "journalctl -x -e --no-pager" + register: journalctl_res # Idempotence: - name: Set settings in actual mode again after restart for idempotence From 8911967982d974694a70eabfd75ac55a4f7e7f9a Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:12:54 +0200 Subject: [PATCH 18/24] removed partially debug --- .../postgresql_set/tasks/options_coverage.yml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index eef5c873d..15f0c8933 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -11,19 +11,6 @@ login_db: postgres block: - - - name: Restart PostgreSQL - become: true - service: - name: "{{ postgresql_service }}" - state: restarted - ignore_errors: true - - - name: Check journalctl - become: true - shell: "journalctl -x -e --no-pager" - register: journalctl_res - - name: Define a test setting map set_fact: setting_map: From 4dce79f10c2d7847629925984761214193e4270e Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:13:15 +0200 Subject: [PATCH 19/24] Added missing postgresql-contrib RHEL package --- .../integration/targets/setup_postgresql_db/vars/RedHat-py3.yml | 1 + tests/integration/targets/setup_postgresql_db/vars/RedHat.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/integration/targets/setup_postgresql_db/vars/RedHat-py3.yml b/tests/integration/targets/setup_postgresql_db/vars/RedHat-py3.yml index 72041a3d7..2deb1244c 100644 --- a/tests/integration/targets/setup_postgresql_db/vars/RedHat-py3.yml +++ b/tests/integration/targets/setup_postgresql_db/vars/RedHat-py3.yml @@ -1,5 +1,6 @@ postgresql_packages: - "postgresql-server" + - "postgresql-contrib" - "python3-psycopg2" - "bzip2" - "xz" diff --git a/tests/integration/targets/setup_postgresql_db/vars/RedHat.yml b/tests/integration/targets/setup_postgresql_db/vars/RedHat.yml index 30720f8fe..1980ee069 100644 --- a/tests/integration/targets/setup_postgresql_db/vars/RedHat.yml +++ b/tests/integration/targets/setup_postgresql_db/vars/RedHat.yml @@ -1,5 +1,6 @@ postgresql_packages: - "postgresql-server" + - "postgresql-contrib" - "python-psycopg2" - "bzip2" From 885ae8fcc38e298133ee18d113efd5c64c569659 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:14:12 +0200 Subject: [PATCH 20/24] removed debug --- .../targets/postgresql_set/tasks/options_coverage.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 15f0c8933..1b6a138c4 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -106,12 +106,6 @@ service: name: "{{ postgresql_service }}" state: restarted - ignore_errors: true - - - name: Check journalctl - become: true - shell: "journalctl -x -e --no-pager" - register: journalctl_res # Idempotence: - name: Set settings in actual mode again after restart for idempotence From a5095498209201e7b21a2c076e4de126f819178d Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Mon, 10 Jul 2023 19:03:15 +0200 Subject: [PATCH 21/24] Updated tests --- .../targets/postgresql_set/tasks/options_coverage.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml index 1b6a138c4..19622d8af 100644 --- a/tests/integration/targets/postgresql_set/tasks/options_coverage.yml +++ b/tests/integration/targets/postgresql_set/tasks/options_coverage.yml @@ -32,7 +32,7 @@ log_line_prefix: 'db=%d,user=%u,app=%a,client=%h ' unix_socket_directories: '/var/run/postgresql, /var/run/postgresql2' - - name: Ensure unix_socket_directories exist + - name: Ensure all unix_socket_directories directories exist file: state: directory path: "{{ item }}" @@ -40,9 +40,7 @@ group: "{{ pg_user }}" mode: '0777' become: true - with_list: - - /var/run/postgresql - - /var/run/postgresql2 + with_list: "{{ setting_map['unix_socket_directories'].split(',') | map('trim') | list }}" # Check mode: - name: Set settings in check mode From 57cf1b5d2c3110495aa0805488c0e3de2bc42371 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:55:29 +0200 Subject: [PATCH 22/24] Update plugins/modules/postgresql_set.py Co-authored-by: Andrew Klychkov --- plugins/modules/postgresql_set.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index b9284bbcc..3f823ee37 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -378,7 +378,8 @@ def param_set(cursor, module, name, value, context, server_version): # PR https://github.com/ansible-collections/community.postgresql/pull/400 # Parameter names ends with '_command' or '_prefix' can contains commas but are not lists # PR https://github.com/ansible-collections/community.postgresql/pull/521 - # unix_socket_directories up to PostgreSQL 13 lacks GUC_LIST_INPUT and GUC_LIST_QUOTE options so it is a single value parameter + # unix_socket_directories up to PostgreSQL 13 lacks GUC_LIST_INPUT and + # GUC_LIST_QUOTE options so it is a single value parameter value = ','.join(["'" + elem.strip() + "'" for elem in value.split(',')]) query = "ALTER SYSTEM SET %s = %s" % (name, value) else: From c1ae45eec8789e4f0823e60cfc166ee7064cef27 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:57:27 +0200 Subject: [PATCH 23/24] Update plugins/modules/postgresql_set.py Co-authored-by: Andrew Klychkov --- plugins/modules/postgresql_set.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 3f823ee37..759f68334 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -229,16 +229,7 @@ def param_is_guc_list_quote(server_version, name): def param_guc_list_unquote(value): # unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) - value_unquoted = [] - for v in value.split(','): - v = v.strip() # strip whitespaces at start/end - if v and v[0] == v[-1] == '"': - # is quoted -> strip quotes - value_unquoted.append(v[1:-1]) - else: - # is not quoted -> no changes - value_unquoted.append(v) - return ', '.join(value_unquoted) + return ', '.join([v.strip('" ') for v in value.split(',')]) def param_get(cursor, module, name, is_guc_list_quote): From 928e4c5ec6bd9c610e98cecc4cf83d1fe9e4baa1 Mon Sep 17 00:00:00 2001 From: RealGreenDragon <14246920+RealGreenDragon@users.noreply.github.com> Date: Wed, 12 Jul 2023 12:00:00 +0200 Subject: [PATCH 24/24] Update postgresql_set.py --- plugins/modules/postgresql_set.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/modules/postgresql_set.py b/plugins/modules/postgresql_set.py index 759f68334..54ce76e26 100644 --- a/plugins/modules/postgresql_set.py +++ b/plugins/modules/postgresql_set.py @@ -228,7 +228,8 @@ def param_is_guc_list_quote(server_version, name): def param_guc_list_unquote(value): - # unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) + # Unquote GUC_LIST_QUOTE parameter (each element can be quoted or not) + # Assume the parameter is GUC_LIST_QUOTE (check in param_is_guc_list_quote function) return ', '.join([v.strip('" ') for v in value.split(',')])