Skip to content

Commit

Permalink
fix pgbackrest configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahms committed Nov 28, 2023
1 parent 8a6a66a commit 4de1649
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 201 deletions.
4 changes: 2 additions & 2 deletions roles/setup_pgbackrest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ The rest of the variables can be configured and are available in the:

Below is the documentation of the rest of the main variables:

### `replication_user`
### `pg_replication_user`

Dedicated replication user used in WAL streaming replication. Default: `repuser`

Example:
```yaml
replication_user: 'repuser'
pg_replication_user: 'repuser'
```
### `pgbackrest_spool_directory`
Expand Down
4 changes: 2 additions & 2 deletions roles/setup_pgbackrest/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
pgbackrest_package_name: "pgbackrest"

# replication user setup in postgresql.conf
replication_user: "repuser"
pg_replication_user: "repuser"

# spool directory - only used with archive-async
pgbackrest_spool_directory: "/var/spool/pgbackrest"
use_patroni: false

# number of parallel processes to be used during backup and recovery
# recommended to not use more than 25% of available CPU for the backup command
Expand All @@ -18,6 +17,7 @@ process_max_recovery: 3
etc_hosts_lists: []
use_hostname: true
update_etc_file: true
use_patroni: false

# setting validate_only to true allows you to validate setup on an existing node
# use_validation flag applies to deployment configuration and validation after setup
Expand Down
8 changes: 4 additions & 4 deletions roles/setup_pgbackrest/tasks/configure_pg_backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
when:
- _pgbackrest_server_info|length == 0

- name: Set _pgbackrest_server_public_ip
- name: Set _pgbackrest_server_private_ip
ansible.builtin.set_fact:
_pgbackrest_server_public_ip: "{{ _pgbackrest_server_info[0].ansible_host }}"
_pgbackrest_server_private_ip: "{{ _pgbackrest_server_info[0].private_ip }}"

# only need to be run when standby_present is defined because they have already been done on just the primary node
- name: Initialize the Postgres parameters list
Expand Down Expand Up @@ -45,13 +45,13 @@
ansible.builtin.command:
cmd: "pgbackrest --stanza={{ pg_instance_name }} --type=full backup"
become_user: "{{ pgbackrest_user }}"
delegate_to: "{{ _pgbackrest_server_public_ip }}"
delegate_to: "{{ _pgbackrest_server_private_ip }}"
become: true
run_once: true
changed_when: true

- name: Reset local variables
ansible.builtin.set_fact:
_pgbackrest_server_info: ""
_pgbackrest_server_public_ip: null
_pgbackrest_server_private_ip: ""
_pg_postgres_conf_params: ""
30 changes: 8 additions & 22 deletions roles/setup_pgbackrest/tasks/configure_pgbackrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,14 @@
ansible.builtin.set_fact:
_pgbackrest_server_info: "{{ lookup('edb_devops.edb_postgres.pgbackrest_server', wantlist=True) }}"

- name: Fail if pgbackrest server informations are not found
ansible.builtin.fail:
msg: "Unable to find pgBackRest server informations"
when:
- _pgbackrest_server_info|length == 0

- name: Set _pgbackrest_host when not using hostname
ansible.builtin.set_fact:
_pgbackrest_host: "{{ _pgbackrest_server_info[0].ansible_host }}"
when:
- "not use_hostname|bool"

- name: Set _pgbackrest_host when using hostname
ansible.builtin.set_fact:
_pgbackrest_host: "{{ _pgbackrest_server_info[0].inventory_hostname }}"
when:
- use_hostname|bool

- name: Set _standby_index_var for each standby node
ansible.builtin.set_fact:
_standby_index_var: "{{ item.index_var }}"
loop: "{{ standby_node_info }}"
when:
- standby_present is defined
- "'standby' in group_names"
- item.inventory_hostname == inventory_hostname
- item.inventory_hostname == inventory_hostname

- name: Ensure lock directory pgbackrest_lock_path exists
ansible.builtin.file:
Expand Down Expand Up @@ -61,10 +43,14 @@
mode: "0700"
become: true
vars:
pgbackrest_host: "{{ _pgbackrest_host }}"
pgbackrest_host: >-
{% if use_hostname %}
{{ _pgbackrest_server_info[0].inventory_hostname }}
{% else %}
{{ _pgbackrest_server_info[0].private_ip }}
{% endif %}
- name: Reset local variables
ansible.builtin.set_fact:
_pgbackrest_server_info: ""
_pgbackrest_host: null
_standby_index_var: null
_standby_index_var: ""
32 changes: 2 additions & 30 deletions roles/setup_pgbackrest/tasks/define_node_variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

- name: Get primary node informations
ansible.builtin.set_fact:
primary_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'primary') | map(attribute='ansible_host') }}"
primary_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'primary') | map(attribute='private_ip') }}"
when:
- "not use_hostname|bool"

Expand All @@ -33,39 +33,11 @@

- name: Get standby node informations
ansible.builtin.set_fact:
standby_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'standby') | map(attribute='ansible_host') | list | flatten }}"
standby_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'standby') | map(attribute='private_ip') | list | flatten }}"
when:
- "not use_hostname|bool"
- standby_present is defined

- name: Get primary node inventory hostname informations
ansible.builtin.set_fact:
primary_node_inventory_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'primary') | map(attribute='inventory_hostname') }}"

- name: Define postgres user on primary node when pg_type = PG
ansible.builtin.set_fact:
primary_host_user: "postgres"
when: pg_type == 'PG'

- name: Define postgres user on primary node when pg_type = EPAS
ansible.builtin.set_fact:
primary_host_user: "enterprisedb"
when: pg_type == 'EPAS'

- name: Define EPAS DB information
ansible.builtin.set_fact:
pg_unix_socket_epas: "/var/run/edb/as{{ pg_version }}"
pg_port_epas: "5444"
pg_database_name: "edb"
when:
- pg_type == 'EPAS'
- ansible_os_family == 'RedHat'

- name: Define EPAS DB information
ansible.builtin.set_fact:
pg_unix_socket_epas: "/var/run/edb-as"
pg_port_epas: "5444"
pg_database_name: "postgres"
when:
- pg_type == 'EPAS'
- ansible_os_family == 'Debian'
1 change: 0 additions & 1 deletion roles/setup_pgbackrest/tasks/generate_ssh_keys.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

- name: Ensure the directory pg_user_home/.ssh exists
ansible.builtin.file:
state: directory
Expand Down
22 changes: 9 additions & 13 deletions roles/setup_pgbackrest/tasks/post_configure_pgbackrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
when:
- _pgbackrest_server_info|length == 0

- name: Set _pgbackrest_host when not using hostname
- name: Set _pgbackrest_host
ansible.builtin.set_fact:
_pgbackrest_host: "{{ _pgbackrest_server_info[0].ansible_host }}"
when:
- "not use_hostname|bool"

- name: Set _pgbackrest_host when using hostname
ansible.builtin.set_fact:
_pgbackrest_host: "{{ _pgbackrest_server_info[0].inventory_hostname }}"
when:
- use_hostname|bool
_pgbackrest_host: >-
{% if use_hostname %}
{{ _pgbackrest_server_info[0].inventory_hostname }}
{% else %}
{{ _pgbackrest_server_info[0].private_ip }}
{% endif %}
# get a list of nodes to update backup-standby setting in pgbackrest.conf
# only pgbackrest and standby nodes set backup-standby=y
Expand Down Expand Up @@ -58,6 +55,5 @@
- name: Reset local variables
ansible.builtin.set_fact:
_pgbackrest_server_info: ""
_pgbackrest_server_public_ip: null
_pgbackrest_host: Null
_update_config: Null
_pgbackrest_host: ""
_update_config: ""
4 changes: 2 additions & 2 deletions roles/setup_pgbackrest/tasks/setup_pgbackrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
- name: Check pgbackrest_archive_method value
ansible.builtin.fail:
msg: "pgBackRest archive method {{ pgbackrest_archive_method }} not supported."
when: >
pgbackrest_archive_method not in ['standard', 'async']
when:
- pgbackrest_archive_method not in ['standard', 'async']

- name: Update /etc/hosts based on use_hostname
become: true
Expand Down
24 changes: 15 additions & 9 deletions roles/setup_pgbackrest/tasks/validate_setup_pgbackrest.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
---
- name: Get pgBackRest node informations
- name: Get pgBackRest server informations
ansible.builtin.set_fact:
pgbackrest_server_node_info: "{{ lookup('edb_devops.edb_postgres.pgbackrest_server', wantlist=True) }}"
_pgbackrest_server_info: "{{ lookup('edb_devops.edb_postgres.pgbackrest_server', wantlist=True) }}"

- name: Set _pgbackrest_server_private_ip
ansible.builtin.set_fact:
_pgbackrest_server_private_ip: "{{ _pgbackrest_server_info[0].private_ip }}"

# validate command for configuration validation is successful
- name: Run command to check if pgbackrest server has been configured correctly
ansible.builtin.command:
cmd: pgbackrest --stanza={{ pg_instance_name }} check
become: true
cmd: "pgbackrest --stanza={{ pg_instance_name }} check"
become_user: "{{ pgbackrest_user }}"
delegate_to: "{{ pgbackrest_server_node_info[0].inventory_hostname }}"
delegate_to: "{{ _pgbackrest_server_private_ip }}"
become: true
run_once: true
register: pgbackrest_config_check_res
changed_when: pgbackrest_config_check_res.rc == '0'
Expand All @@ -25,10 +29,10 @@
# validate backup exists on pgbackrest
- name: Run command to check if backup for pgbackrest cluster was successful
ansible.builtin.command:
cmd: pgbackrest --stanza={{ pg_instance_name }} info
cmd: "pgbackrest --stanza={{ pg_instance_name }} info"
become: true
become_user: "{{ pgbackrest_user }}"
delegate_to: "{{ pgbackrest_server_node_info[0].inventory_hostname }}"
delegate_to: "{{ _pgbackrest_server_private_ip }}"
run_once: true
register: pgbackrest_config_info_res
changed_when: pgbackrest_config_info_res.rc == '0'
Expand All @@ -44,10 +48,10 @@
# test archive repo for pgbackrest exists
- name: Run command to check if pgbackrest archive repo was successfully configured
ansible.builtin.command:
cmd: pgbackrest repo-ls
cmd: "pgbackrest repo-ls"
become: true
become_user: "{{ pgbackrest_user }}"
delegate_to: "{{ pgbackrest_server_node_info[0].inventory_hostname }}"
delegate_to: "{{ _pgbackrest_server_private_ip }}"
run_once: true
register: pgbackrest_config_repo_res
changed_when: pgbackrest_config_repo_res.rc == '0'
Expand All @@ -62,6 +66,8 @@

- name: Reset variables
ansible.builtin.set_fact:
_pgbackrest_server_info: ""
_pgbackrest_server_private_ip: ""
pgbackrest_config_check_res: null
pgbackrest_config_info_res: null
pgbackrest_config_repo_res: null
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ repo1-host-user={{ pgbackrest_user }}
repo1-host-type=ssh
repo1-cipher-type={{ repo_cipher_type }}
repo1-cipher-pass={{ repo_cipher_password }}
start-fast=y
log-level-console={{ pgbackrest_log_level_console }}
log-level-file={{ pgbackrest_log_level_file }}
lock-path={{ pgbackrest_lock_path }}
start-fast=y

[global:archive-get]
process-max={{ process_max_recovery }}
Expand All @@ -19,4 +19,4 @@ process-max={{ process_max_recovery }}
process-max={{ process_max_backup }}

[{{ pg_instance_name }}]
pg1-path={{ pg_data }}
pg1-path={{ pg_default_data }}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ lock-path={{ pgbackrest_lock_path }}
start-fast=y

[{{ pg_instance_name }}]
pg1-path={{ pg_data }}
pg1-path={{ pg_default_data }}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ process-max={{ process_max_recovery }}
[global:archive-push]
process-max={{ process_max_backup }}


[{{ pg_instance_name }}]
pg{{ _standby_index_var }}-path={{ pg_data }}
pg{{ _standby_index_var }}-path={{ pg_default_data }}
{% if pg_type == 'PG' %}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} user={{ replication_user }}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} user={{ pg_replication_user }}
{% endif %}
{% if pg_type == 'EPAS' %}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} port={{ pg_port_epas }} user={{ replication_user }}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} port={{ pg_port }} user={{ pg_replication_user }}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ lock-path={{ pgbackrest_lock_path }}
start-fast=y

[{{ pg_instance_name }}]
pg{{ _standby_index_var }}-path={{ pg_data }}
pg{{ _standby_index_var }}-path={{ pg_default_data }}
{% if pg_type == 'PG' %}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} user={{ replication_user }}
{% endif %}
{% if pg_type == 'EPAS' %}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} port={{ pg_port_epas }} user={{ replication_user }}
recovery-option=primary_conninfo=host={{ primary_node_hostname[0] }} port={{ pg_port }} user={{ replication_user }}
{% endif %}
3 changes: 3 additions & 0 deletions roles/setup_pgbackrestserver/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ primary_pgbr_config_file: "/etc/pgbackrest.conf"
# postgres instance name
pg_instance_name: "main"

pg_replication_user: "repuser"

etc_hosts_lists: []
use_hostname: true
update_etc_file: true
use_patroni: false

supported_os:
- CentOS7
Expand Down
34 changes: 3 additions & 31 deletions roles/setup_pgbackrestserver/tasks/define_node_variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,20 @@

- name: Get primary node informations if not use_hostname
ansible.builtin.set_fact:
primary_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'primary') | map(attribute='ansible_host') }}"
primary_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'primary') | map(attribute='private_ip') }}"
when:
- "not use_hostname|bool"

- name: Get standby node informations if any, if use_hostname
ansible.builtin.set_fact:
standby_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'standby') | map(attribute='inventory_hostname') }}"
standby_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'standby') | map(attribute='inventory_hostname') | list | flatten }}"
when:
- use_hostname|bool
- standby_present is defined

- name: Get standby node informations if any, if not use_hostname
ansible.builtin.set_fact:
standby_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'standby') | map(attribute='ansible_host') }}"
standby_node_hostname: "{{ pgbackrest_node_info | selectattr('node_type', 'equalto', 'standby') | map(attribute='private_ip') | list | flatten }}"
when:
- "not use_hostname|bool"
- standby_present is defined

- name: Define postgres user on primary node when pg_type = PG
ansible.builtin.set_fact:
primary_host_user: "postgres"
when: pg_type == 'PG'

- name: Define postgres user on primary node when pg_type = EPAS
ansible.builtin.set_fact:
primary_host_user: "enterprisedb"
when: pg_type == 'EPAS'

- name: Define EPAS DB information if RedHat
ansible.builtin.set_fact:
pg_unix_socket_epas: "/var/run/edb/as{{ pg_version }}"
pg_port_epas: "5444"
pg_database_name: "edb"
when:
- pg_type == 'EPAS'
- ansible_os_family == 'RedHat'

- name: Define EPAS DB information if Debian
ansible.builtin.set_fact:
pg_unix_socket_epas: "/var/run/edb-as"
pg_port_epas: "5444"
pg_database_name: "postgres"
when:
- pg_type == 'EPAS'
- ansible_os_family == 'Debian'
Loading

0 comments on commit 4de1649

Please sign in to comment.