refactor, add postgresql 16 as supported and fix pitr

This commit is contained in:
ace 2024-05-26 17:56:35 +03:00
parent 0b74982f90
commit b6e14c2de3
Signed by: ace
GPG Key ID: 2C08973DD37A76FD
8 changed files with 87 additions and 48 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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:

View File

@ -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

View File

@ -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

24
vars/Ubuntu-24.04.yaml Normal file
View File

@ -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 }}"