Skip to content

Commit

Permalink
PgBackRest: automate "backup-standby" setup (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitabaks authored Jan 4, 2024
1 parent 382415b commit 608d430
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
9 changes: 3 additions & 6 deletions roles/pgbackrest/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,10 @@
- pgbackrest_repo_host | length > 0
tags: pgbackrest, pgbackrest_conf

# if pgbackrest_repo_type: "posix" and pgbackrest_repo_host is set
# if 'pgbackrest_repo_host' or 'backup-standby' are specified
- ansible.builtin.import_tasks: ssh_keys.yml
when:
- pgbackrest_repo_type|lower == "posix"
- pgbackrest_repo_host is defined
- pgbackrest_repo_host | length > 0
- not ansible_check_mode
when: (pgbackrest_repo_host is defined and pgbackrest_repo_host | length > 0) or
(pgbackrest_conf.global | selectattr('option', 'equalto', 'backup-standby') | map(attribute='value') | list | last | default('') == 'y')
tags: pgbackrest, pgbackrest_ssh_keys

- ansible.builtin.import_tasks: cron.yml
Expand Down
17 changes: 14 additions & 3 deletions roles/pgbackrest/tasks/ssh_keys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- name: ssh_keys | Get public ssh key from pgbackrest server
ansible.builtin.slurp:
src: "~{{ pgbackrest_repo_user }}/.ssh/id_rsa.pub"
register: pgbackrest_sshkey
register: pgbackrest_server_sshkey
changed_when: false
when: "'pgbackrest' in group_names"

Expand All @@ -56,8 +56,8 @@
ansible.posix.authorized_key:
user: postgres
state: present
key: "{{ hostvars[item].pgbackrest_sshkey['content'] | b64decode }}"
loop: "{{ groups['pgbackrest'] }}"
key: "{{ hostvars[item].pgbackrest_server_sshkey['content'] | b64decode }}"
loop: "{{ groups['pgbackrest'] | default([]) }}"
when: "'postgres_cluster' in group_names"

- name: ssh_keys | Add database ssh keys in "~{{ pgbackrest_repo_user }}/.ssh/authorized_keys" on pgbackrest server
Expand All @@ -68,6 +68,17 @@
loop: "{{ groups['postgres_cluster'] }}"
when: "'pgbackrest' in group_names"

# if 'backup-standby' are specified in pgbackrest_conf.global
- name: ssh_keys | Add ssh keys in "~postgres/.ssh/authorized_keys" on database servers
ansible.posix.authorized_key:
user: postgres
state: present
key: "{{ hostvars[item].postgres_cluster_sshkey['content'] | b64decode }}"
loop: "{{ groups['postgres_cluster'] }}"
when:
- "'postgres_cluster' in group_names"
- pgbackrest_conf.global | selectattr('option', 'equalto', 'backup-standby') | map(attribute='value') | list | last | default('') == 'y'

- name: known_hosts | Get public ssh keys of hosts (ssh-keyscan)
ansible.builtin.command: "ssh-keyscan -trsa -p {{ ansible_ssh_port | default(22) }} {{ item }}"
loop: "{{ groups['all'] }}"
Expand Down
9 changes: 8 additions & 1 deletion roles/pgbackrest/templates/pgbackrest.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
{% for stanza in pgbackrest_conf.stanza %}
{{ stanza.option }}={{ stanza.value }}
{% endfor %}

{% if pgbackrest_conf.global | selectattr('option', 'equalto', 'backup-standby') | map(attribute='value') | list | last | default('') == 'y' %}
{% set pg_standby_hosts = groups['postgres_cluster'] | reject('equalto', inventory_hostname) | list %}
{% for host in pg_standby_hosts %}
pg{{ loop.index + 1 }}-host={{ host }}
pg{{ loop.index + 1 }}-port={{ postgresql_port }}
pg{{ loop.index + 1 }}-path={{ postgresql_data_dir }}
{% endfor %}
{% endif %}
7 changes: 4 additions & 3 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,13 @@ pgbackrest_conf:
- { option: "archive-async", value: "y" } # Enables asynchronous WAL archiving (details: https://pgbackrest.org/user-guide.html#async-archiving)
- { option: "archive-get-queue-max", value: "1GiB" }
# - { option: "archive-push-queue-max", value: "100GiB" }
# - { option: "backup-standby", value: "y" } # When set to 'y', standby servers will be automatically added to the stanza section.
# - { option: "", value: "" }
stanza: # [stanza_name] section
- { option: "pg1-path", value: "{{ postgresql_data_dir }}" }
- { option: "recovery-option", value: "recovery_target_action=promote" }
- { option: "log-level-console", value: "info" }
- { option: "process-max", value: "4" }
- { option: "log-level-console", value: "info" }
- { option: "recovery-option", value: "recovery_target_action=promote" }
- { option: "pg1-path", value: "{{ postgresql_data_dir }}" }
# - { option: "", value: "" }
# (optional) dedicated backup server config (if "repo_host" is set)
pgbackrest_server_conf:
Expand Down

0 comments on commit 608d430

Please sign in to comment.