Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repmgr extension fixes #2

Open
wants to merge 8 commits into
base: add-repmgr-extension
Choose a base branch
from
File renamed without changes.
File renamed without changes.
15 changes: 8 additions & 7 deletions tasks/extensions/configure_repmgr.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- name: Repmgr | Update configuration (repmgr.conf)
template:
src: "repmgr.conf-{{ repmgr_version }}.j2"
dest: "{{postgresql_conf_directory}}/repmgr.conf"
dest: "{{repmgr_config_directory}}/repmgr.conf"
owner: "{{ postgresql_service_user }}"
group: "{{ postgresql_service_group }}"
mode: 0640
Expand Down Expand Up @@ -31,6 +31,7 @@
group: "{{ postgresql_service_group }}"
mode: 0400
trim_blocks: no
no_log: true

- name: Repmgr | Generate ssh keypair for postgres user
command: "ssh-keygen -b {{repmgr_ssh_key_bits}} -t {{repmgr_ssh_key_type}} -f {{repmgr_private_key_path}} -N ''"
Expand Down Expand Up @@ -75,15 +76,15 @@
mode: 0600

- name: Repmgr | Check cluster status
command: "{{postgresql_bin_directory}}/repmgr -f {{postgresql_conf_directory}}/repmgr.conf cluster show"
command: "{{postgresql_bin_directory}}/repmgr -f {{repmgr_config_directory}}/repmgr.conf cluster show"
become: yes
become_user: "{{ postgresql_service_user }}"
changed_when: repmgr_cluster_show.rc != 0
register: repmgr_cluster_show
ignore_errors: True

- name: Repmgr | Register as primary
command: "{{postgresql_bin_directory}}/repmgr -f {{postgresql_conf_directory}}/repmgr.conf primary register"
command: "{{postgresql_bin_directory}}/repmgr -f {{repmgr_config_directory}}/repmgr.conf primary register"
become: yes
become_user: "{{ postgresql_service_user }}"
when: repmgr_primary and not ansible_hostname in repmgr_cluster_show.stdout and not "primary" in repmgr_cluster_show.stdout
Expand All @@ -95,7 +96,7 @@
when: not repmgr_primary and not ansible_hostname in repmgr_cluster_show.stdout and not "standby" in repmgr_cluster_show.stdout

- name: Repmgr | Clone standby
command: "{{postgresql_bin_directory}}/repmgr -F -h {% for host, vars in hostvars.items() if 'repmgr_primary' in vars and vars['repmgr_primary'] == True %}{{ host }}{% endfor %} -U repmgr -d repmgr -f {{postgresql_conf_directory}}/repmgr.conf standby clone"
command: "{{postgresql_bin_directory}}/repmgr -F -h {% for host, vars in hostvars.items() if 'repmgr_primary' in vars and vars['repmgr_primary'] == True %}{{ host }}{% endfor %} -p {{ postgresql_port }} -U repmgr -d repmgr -f {{repmgr_config_directory}}/repmgr.conf standby clone"
become: yes
become_user: "{{ postgresql_service_user }}"
when: not repmgr_primary and not ansible_hostname in repmgr_cluster_show.stdout and not "standby" in repmgr_cluster_show.stdout
Expand All @@ -112,13 +113,13 @@
delegate_to: localhost

- name: Repmgr | Register standby
command: "{{postgresql_bin_directory}}/repmgr -F -h {% for host, vars in hostvars.items() if 'repmgr_primary' in vars and vars['repmgr_primary'] == True %}{{ host }}{% endfor %} -U repmgr -d repmgr -f {{postgresql_conf_directory}}/repmgr.conf standby register"
command: "{{postgresql_bin_directory}}/repmgr -F -h {% for host, vars in hostvars.items() if 'repmgr_primary' in vars and vars['repmgr_primary'] == True %}{{ host }}{% endfor %} -U repmgr -d repmgr -f {{repmgr_config_directory}}/repmgr.conf standby register"
become: yes
become_user: "{{ postgresql_service_user }}"
when: not repmgr_primary and not ansible_hostname in repmgr_cluster_show.stdout and not "standby" in repmgr_cluster_show.stdout

- name: Repmgr | Verify cluster functionality
command: "{{postgresql_bin_directory}}/repmgr -F -h {% for host, vars in hostvars.items() if 'repmgr_primary' in vars and vars['repmgr_primary'] == True %}{{ host }}{% endfor %} -U repmgr -d repmgr -f {{postgresql_conf_directory}}/repmgr.conf cluster crosscheck"
command: "{{postgresql_bin_directory}}/repmgr -F -h {% for host, vars in hostvars.items() if 'repmgr_primary' in vars and vars['repmgr_primary'] == True %}{{ host }}{% endfor %} -U repmgr -d repmgr -f {{repmgr_config_directory}}/repmgr.conf cluster crosscheck"
become: yes
become_user: "{{ postgresql_service_user }}"

Expand All @@ -142,7 +143,7 @@

- name: Repmgr | Setup cluster monitoring history cleanup
copy:
content: "{{postgresql_bin_directory}}/repmgr -f {{postgresql_conf_directory}}/repmgr.conf cluster cleanup --keep-history={{repmgr_keep_history_days}}"
content: "{{postgresql_bin_directory}}/repmgr -f {{repmgr_config_directory}}/repmgr.conf cluster cleanup --keep-history={{repmgr_keep_history_days}}"
dest: "/etc/cron.daily/repmgr_cleanup"
mode: 0755
when: repmgr_monitoring_history == "true" or repmgr_failover == "automatic"
7 changes: 5 additions & 2 deletions tasks/extensions/detect_repmgr_primary.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: Repmgr | Detect primary
shell: "set -o pipefail ; {{ postgresql_bin_directory }}/repmgr -f {{ postgresql_conf_directory }}/repmgr.conf cluster show | grep primary | cut -d '|' -f 2"
shell: "set -o pipefail ; {{ postgresql_bin_directory }}/repmgr -f {{ repmgr_config_directory }}/repmgr.conf cluster show | grep primary | cut -d '|' -f 2"
register: repmgr_primary_check
become: yes
become_user: postgres
Expand All @@ -13,11 +13,14 @@

- name: Repmgr | Declare as primary for new installation
set_fact:
repmgr_primary: "{{ True if ansible_hostname == ansible_play_hosts[0] or inventory_hostname == ansible_play_hosts[0] else False }}"
repmgr_primary: "{{ True if ansible_hostname == repmgr_master or inventory_hostname == repmgr_master else False }}"
when: "repmgr_existing_installation_detected is skipped"

- debug:
var: repmgr_primary

- debug:
var: repmgr_master

- debug:
var: ansible_hostname
6 changes: 4 additions & 2 deletions templates/pgpass.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% for server in groups[repmgr_target_group] | sort %}
{{ server }}:{{postgresql_port}}:{{repmgr_database}}:{{repmgr_user}}:{% for user in postgresql_users if user['name'] == repmgr_user %}{{user['pass']}}{% endfor %}
{{ server }}:{{postgresql_port}}:replication:{{repmgr_replication_user}}:{% for user in postgresql_users if user['name'] == repmgr_user %}{{user['pass']}}{% endfor %}
{{ server }}:{{postgresql_port}}:{{repmgr_database}}:{{repmgr_user}}:{% for user in postgresql_users if user['name'] == repmgr_user %}{{user['pass']}}{% endfor %}
{{ server | regex_replace('^([^.]*)\.?.*$', '\\1') }}:{{postgresql_port}}:{{repmgr_database}}:{{repmgr_user}}:{% for user in postgresql_users if user['name'] == repmgr_user %}{{user['pass']}}{% endfor %}
{{ server }}:{{postgresql_port}}:replication:{{repmgr_replication_user}}:{% for user in postgresql_users if user['name'] == repmgr_user %}{{user['pass']}}{% endfor %}
{{ server | regex_replace('^([^.]*)\.?.*$', '\\1') }}:{{postgresql_port}}:replication:{{repmgr_replication_user}}:{% for user in postgresql_users if user['name'] == repmgr_user %}{{user['pass']}}{% endfor %}
{% endfor %}
2 changes: 1 addition & 1 deletion templates/repmgr.conf-4.2.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data_directory='{{repmgr_data_directory}}' # The node's data directory. This is
# Server settings
#------------------------------------------------------------------------------

config_directory='{{repmgr_config_directory}}' # If configuration files are located outside the data
config_directory='{{postgresql_conf_directory}}' # If configuration files are located outside the data
# directory, specify the directory where the main
# postgresql.conf file is located.

Expand Down
Loading