From b6e14c2de3246f238b1fd36ec49f2106c692d38b Mon Sep 17 00:00:00 2001 From: ace Date: Sun, 26 May 2024 17:56:35 +0300 Subject: [PATCH] refactor, add postgresql 16 as supported and fix pitr --- defaults/main.yaml | 32 ++++++++++++++++++++------------ tasks/Debian/config.yaml | 21 +++++++-------------- tasks/Debian/data_dir.yaml | 13 +++++++++++++ tasks/Debian/main.yaml | 9 ++++++--- tasks/RedHat/config.yaml | 9 ++++++++- tasks/main.yaml | 2 +- tasks/restore.yaml | 25 ++++++++----------------- vars/Ubuntu-24.04.yaml | 24 ++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 48 deletions(-) create mode 100644 tasks/Debian/data_dir.yaml create mode 100644 vars/Ubuntu-24.04.yaml diff --git a/defaults/main.yaml b/defaults/main.yaml index eb7a394..ed00920 100644 --- a/defaults/main.yaml +++ b/defaults/main.yaml @@ -1,4 +1,4 @@ -postgresql_version: "15.7" +postgresql_version: "16.3" # Options postgresql_install_official_repo: true @@ -23,19 +23,22 @@ postgresql_cacert_ca_trust_anchors_update: true ## WAL-G backup and restore options postgresql_wal_g_install: false +postgresql_wal_g_backup_enable: false postgresql_wal_g_restore_from_backup: false postgresql_wal_g_force_delete_and_restore: false postgresql_wal_g_config_name: ".walg.json" postgresql_wal_g_restore_config_name: ".walg-restore.json" -postgresql_wal_g_pitr: 'latest' -postgresql_wal_g_restore_backup_name: 'LATEST' -postgresql_wal_g_binary_name: "wal-g-pg" -#postgresql_postgresql_archive_command: "wal-g-pg wal-push %p" -#postgresql_postgresql_restore_command: "wal-g-pg wal-fetch %f %p" -#postgresql_restore_dbs_from_backup_command: "wal-g-pg backup-fetch {{ postgresql_wal_g_restore_backup_name }}" -#postgresql_restore_wals_from_backup_command: "wal-g-pg wal-fetch %f %p" +postgresql_wal_g_pitr: "" +postgresql_wal_g_restore_backup_name: "LATEST" +postgresql_wal_g_binary: "/usr/bin/wal-g-pg" +postgresql_archive_command: "{{ postgresql_wal_g_binary }} wal-push %p" +postgresql_restore_command: "{{ postgresql_wal_g_binary }} wal-fetch %f %p" +postgresql_restore_dbs_from_backup_command: "{{ postgresql_wal_g_binary }} backup-fetch {{ postgresql_wal_g_restore_backup_name }}" +postgresql_restore_wals_from_backup_command: "{{ postgresql_wal_g_binary }} wal-fetch %f %p" postgresql_recovery_target_time: "" +postgresql_recovery_target_timeline: "latest" +postgresql_recovery_target_action: "promote" postgresql_promote_trigger_file: "{{ postgresql_data_dir }}/promote.trigger" postgresql_password_encryption_algorithm: "scram-sha-256" @@ -64,7 +67,7 @@ postgresql_default_parameters: wal_buffers: '16MB' log_destination: 'stderr' logging_collector: 'on' - log_directory: 'log' + log_directory: '/var/log/postgresql' log_filename: 'postgresql-%a.log' log_rotation_age: '1d' log_rotation_size: '0' @@ -78,13 +81,17 @@ postgresql_default_parameters: default_text_search_config: 'pg_catalog.english' password_encryption: '{{ postgresql_password_encryption_algorithm }}' hot_standby: 'off' - promote_trigger_file: '{{ postgresql_promote_trigger_file }}' postgresql_backup_parameters: - archive_command: "{{ postgresql_archive_command | default('cd .')}}" + archive_command: "{{ postgresql_archive_command if postgresql_wal_g_backup_enable else 'cd .' }}" postgresql_restore_parameters: - restore_command: "{{ postgresql_restore_command | default('cd .') }}" + restore_command: "{{ postgresql_restore_command if postgresql_wal_g_backup_enable else 'cd .' }}" + +postgresql_restore_pitr_parameters: + recovery_target_time: "{{ postgresql_wal_g_pitr }}" + recovery_target_timeline: "{{ postgresql_recovery_target_timeline }}" + recovery_target_action: "{{ postgresql_recovery_target_action }}" postgresql_log_dir: "/var/log/postgresql" @@ -93,3 +100,4 @@ postgresql_supported_versions: - 13 - 14 - 15 + - 16 diff --git a/tasks/Debian/config.yaml b/tasks/Debian/config.yaml index ff1f854..284a444 100644 --- a/tasks/Debian/config.yaml +++ b/tasks/Debian/config.yaml @@ -1,18 +1,4 @@ --- -- name: Create data directory - file: - path: "{{ postgresql_data_dir }}" - owner: "{{ postgresql_user }}" - group: "{{ postgresql_group }}" - state: directory - -- name: Create configuration directory - file: - path: "{{ postgresql_config_dir }}/{{ postgresql_major_version }}/{{ postgresql_cluster_name }}" - owner: "{{ postgresql_user }}" - group: "{{ postgresql_group }}" - state: directory - - name: Merge user options for PostgreSQL config set_fact: postgresql_combined_parameters: "{{ postgresql_default_parameters | combine(postgresql_custom_parameters|default({}), recursive=true) }}" @@ -25,6 +11,13 @@ set_fact: postgresql_combined_parameters: "{{ postgresql_combined_parameters | combine(postgresql_restore_parameters, recursive=true) }}" +- name: Merge PITR restore options for PostgreSQL config + set_fact: + postgresql_combined_parameters: "{{ postgresql_combined_parameters | combine(postgresql_restore_pitr_parameters, recursive=true) }}" + when: + - postgresql_wal_g_restore_from_backup + - postgresql_wal_g_pitr | length > 0 + - name: Propagate PostgreSQL configs block: - name: Template PostgreSQL pg_hba configuration diff --git a/tasks/Debian/data_dir.yaml b/tasks/Debian/data_dir.yaml new file mode 100644 index 0000000..0c3bbdc --- /dev/null +++ b/tasks/Debian/data_dir.yaml @@ -0,0 +1,13 @@ +- name: Create data directory + file: + path: "{{ postgresql_data_dir }}" + owner: "{{ postgresql_user }}" + group: "{{ postgresql_group }}" + state: directory + +- name: Create configuration directory + file: + path: "{{ postgresql_config_dir }}/{{ postgresql_major_version }}/{{ postgresql_cluster_name }}" + owner: "{{ postgresql_user }}" + group: "{{ postgresql_group }}" + state: directory diff --git a/tasks/Debian/main.yaml b/tasks/Debian/main.yaml index 62cb15f..9760680 100644 --- a/tasks/Debian/main.yaml +++ b/tasks/Debian/main.yaml @@ -2,9 +2,8 @@ include_tasks: install.yaml tags: postgresql_installation -- name: "Configure PostgreSQL" - include_tasks: config.yaml - tags: postgresql_configuration +- name: Include data direcory + include_tasks: data_dir.yaml - name: "Manage TLS/SSL certificates" #include_tasks: cacert.yaml @@ -25,6 +24,10 @@ include_tasks: restore.yaml when: postgresql_wal_g_restore_from_backup +- name: "Configure PostgreSQL" + include_tasks: config.yaml + tags: postgresql_configuration + - name: "PostgreSQL initdb" become_user: "{{ postgresql_superuser_username }}" shell: "/usr/lib/postgresql/{{ postgresql_major_version }}/bin/initdb -D {{ postgresql_data_dir }} --username {{ postgresql_superuser_username }} --auth-local peer --auth-host {{ postgresql_password_encryption_algorithm }} --no-instructions" diff --git a/tasks/RedHat/config.yaml b/tasks/RedHat/config.yaml index 5a0db06..e2bc223 100644 --- a/tasks/RedHat/config.yaml +++ b/tasks/RedHat/config.yaml @@ -9,7 +9,14 @@ - name: Merge restore options for PostgreSQL config set_fact: - postgresql_combined_parameters: "{{ postgresql_combined_parameters | combine(postgresql_restore_parameters|default({}), recursive=true) }}" + postgresql_combined_parameters: "{{ postgresql_combined_parameters | combine(postgresql_restore_parameters, recursive=true) }}" + +- name: Merge PITR restore options for PostgreSQL config + set_fact: + postgresql_combined_parameters: "{{ postgresql_combined_parameters | combine(postgresql_restore_pitr_parameters, recursive=true) }}" + when: + - postgresql_wal_g_restore_from_backup + - postgresql_wal_g_pitr | length > 0 - name: Propagate PostgreSQL configs block: diff --git a/tasks/main.yaml b/tasks/main.yaml index c7b2bd8..5b48798 100644 --- a/tasks/main.yaml +++ b/tasks/main.yaml @@ -37,6 +37,6 @@ register: postgresql_enable_and_start tags: postgresql, postgresql_start -- name: "Configure PostgreSQL" +- name: "Configure initial PostgreSQL user" include_tasks: user.yaml tags: postgresql_postgres_user diff --git a/tasks/restore.yaml b/tasks/restore.yaml index b8fedde..343ca18 100644 --- a/tasks/restore.yaml +++ b/tasks/restore.yaml @@ -36,6 +36,7 @@ vars: postgresql_archive_command: "cd ." postgresql_restore_command: "{{ postgresql_restore_wals_from_backup_command }}" + recovery_target_time: "{{ postgresql_wal_g_pitr }}" include_tasks: config.yaml - name: Create recovery.signal file @@ -44,12 +45,6 @@ state: touch become_user: postgres - #- name: Create standby.signal file - # file: - # name: "{{ postgresql_data_dir }}/standby.signal" - # state: touch - # become_user: postgres - - name: Start PostgreSQL for recovery systemd: daemon_reload: true @@ -59,21 +54,23 @@ register: postgresql_start_for_recovery - name: Ping PostgreSQL server - vars: - ansible_python_interpreter: '/usr/bin/env python3' community.postgresql.postgresql_ping: db: "{{ postgresql_superuser_db }}" - login_user: "{{ postgresql_superuser_username }}" - login_password: "{{ postgresql_superuser_password }}" register: postgresql_ping until: postgresql_ping.is_available - retries: 60 + retries: 100 delay: 20 - name: debug debug: msg: "{{ postgresql_ping }}" + - name: Reset {{ postgresql_superuser_username }} password after restore + become_user: "{{ postgresql_user }}" + community.postgresql.postgresql_query: + db: "{{ postgresql_superuser_db }}" + query: ALTER USER {{ postgresql_superuser_username }} with password '{{ postgresql_superuser_password }}'; + - name: Stop PostgreSQL systemd: daemon_reload: true @@ -82,11 +79,5 @@ masked: no register: postgresql_stop - - name: Create promote trigger file - file: - name: "{{ postgresql_promote_trigger_file }}" - state: touch - become_user: postgres - - name: Include PostgreSQL configuration include_tasks: config.yaml diff --git a/vars/Ubuntu-24.04.yaml b/vars/Ubuntu-24.04.yaml new file mode 100644 index 0000000..b1b6152 --- /dev/null +++ b/vars/Ubuntu-24.04.yaml @@ -0,0 +1,24 @@ +postgresql_apt_key: + - name: org.postgresql.gpg + url: "https://www.postgresql.org/media/keys/ACCC4CF8.asc" + keyring: /etc/apt/trusted.gpg.d/org.postgresql.gpg +postgresql_apt_repository: + - repo: deb http://apt.postgresql.org/pub/repos/apt {{ ansible_distribution_release }}-pgdg main + filename: postgresql + +postgresql_deps_packages: + - gnupg2 + - python3-psycopg2 + +postgresql_package_name: "postgresql-{{ postgresql_major_version }}" +postgresql_package: "{{ postgresql_package_name }}={{ postgresql_version }}-{{ postgresql_version_build }}" +postgresql_version_build: "1.pgdg{{ ansible_distribution_version }}+1" +postgresql_config_dir: "/etc/postgresql" +postgresql_home_dir: "/var/lib/postgresql" +postgresql_data_dir: "{{ postgresql_home_dir }}/{{ postgresql_major_version }}/{{ postgresql_cluster_name }}" +postgresql_ssl_path: "{{ postgresql_config_dir }}/{{ postgresql_major_version }}/{{ postgresql_cluster_name }}" +postgresql_package_name_regex: "{{ postgresql_package_name }}-{{ postgresql_version }}-{{ postgresql_version_build }}" +postgresql_cluster_name: "main" +postgresql_cacert_update_ca_trust_command: "update-ca-certificates --fresh" +postgresql_cacert_ca_trust_dir: "/usr/local/share/ca-certificates" +postgresql_unit_name: "postgresql@{{ postgresql_major_version}}-{{ postgresql_cluster_name }}"