|
| 1 | +- name: Import pg_autofailover_common playbook |
| 2 | + ansible.builtin.import_playbook: pg_autofailover_common.yaml |
| 3 | + vars: |
| 4 | + host: "{{ host }}" |
| 5 | + |
1 | 6 | - name: PostgreSQL Node Setup |
2 | 7 | hosts: "{{ host }}" |
| 8 | + vars: |
| 9 | + ansible_common_remote_group: postgres |
3 | 10 |
|
4 | 11 | tasks: |
5 | | - - name: Import pg_autofailover_common playbook |
6 | | - ansible.builtin.import_playbook: pg_autofailover_common.yaml |
7 | | - vars: |
8 | | - host: "{{ host }}" |
| 12 | + - name: Set config file path |
| 13 | + set_fact: |
| 14 | + config_file: "ini/config-{{ env }}.ini" |
9 | 15 |
|
10 | | -- hosts: "{{ host }}" |
11 | | - vars: |
12 | | - db_name: "{{ lookup('ini', 'db_name', file='ini/config-{{ env }}.ini') }}" |
13 | | - db_user: "{{ lookup('ini', 'db_user', file='ini/config-{{ env }}.ini') }}" |
14 | | - db_password: "{{ lookup('ini', 'db_password', file='ini/config-{{ env }}.ini') }}" |
15 | | - postgres_monitor_hostname: "{{ lookup('ini', 'postgres_monitor_hostname', file='ini/config-{{ env }}.ini') }}" |
16 | | - node_pgdata: "{{ lookup('ini', 'node_pgdata', file='ini/config-{{ env }}.ini', default='/var/lib/postgresql/node') }}" |
17 | | - node_port: "{{ lookup('ini', 'node_port', file='ini/config-{{ env }}.ini', default='5432') }}" |
18 | | - backup_dir: "{{ lookup('ini', 'backup_dir', file='ini/config-{{ env }}.ini', default='/var/lib/backup') }}" |
| 16 | + - name: Set config vars from INI file |
| 17 | + set_fact: |
| 18 | + db_name: "{{ lookup('ini', 'db_name', file=config_file) }}" |
| 19 | + db_user: "{{ lookup('ini', 'db_user', file=config_file) }}" |
| 20 | + db_password: "{{ lookup('ini', 'db_password', file=config_file) }}" |
| 21 | + postgres_monitor_hostname: "{{ lookup('ini', 'postgres_monitor_hostname', file=config_file) }}" |
| 22 | + node_pgdata: "{{ lookup('ini', 'node_pgdata', file=config_file, default='/var/lib/postgresql/node') }}" |
| 23 | + node_port: "{{ lookup('ini', 'node_port', file=config_file, default='5432') }}" |
| 24 | + backup_dir: "{{ lookup('ini', 'backup_dir', file=config_file, default='/var/lib/backup') }}" |
| 25 | + no_log: true |
19 | 26 |
|
20 | | - tasks: |
21 | 27 | - name: Create backup directory |
22 | 28 | become: true |
23 | 29 | file: |
|
36 | 42 | become: true |
37 | 43 | become_user: postgres |
38 | 44 |
|
| 45 | + - name: Debug node_initialized |
| 46 | + debug: |
| 47 | + msg: "Node initialized: {{ node_initialized.stat.exists }}, path: {{ node_pgdata }}/PG_VERSION" |
| 48 | + |
39 | 49 | - name: Get Tailscale IP |
40 | 50 | shell: tailscale ip --4 |
41 | 51 | register: tailscale_ip |
|
56 | 66 | --hostname {{ tailscale_ip.stdout }} \ |
57 | 67 | --monitor 'postgres://{{ db_user }}:{{ db_password }}@{{ postgres_monitor_hostname }}:5432/pg_auto_failover?sslmode=require' |
58 | 68 | when: not node_initialized.stat.exists |
59 | | - no_log: true |
60 | 69 | args: |
61 | 70 | creates: "{{ node_pgdata }}/PG_VERSION" |
62 | 71 |
|
|
108 | 117 | port: "{{ node_port }}" |
109 | 118 | delay: 5 |
110 | 119 | timeout: 60 |
| 120 | + |
| 121 | + - name: Check if node is writable (primary) |
| 122 | + become: true |
| 123 | + become_user: postgres |
| 124 | + shell: | |
| 125 | + psql -d {{ db_name }} -tAc "SELECT NOT pg_is_in_recovery();" |
| 126 | + register: is_writable |
| 127 | + changed_when: false |
| 128 | + |
| 129 | + - name: Set password for pgautofailover_replicator user |
| 130 | + become: true |
| 131 | + become_user: postgres |
| 132 | + shell: | |
| 133 | + psql -d {{ db_name }} -c "ALTER USER pgautofailover_replicator PASSWORD '{{ db_password }}';" |
| 134 | + when: is_writable.stdout == 't' |
| 135 | + no_log: true |
0 commit comments