|
| 1 | +--- |
| 2 | +# yamllint disable rule:line-length |
| 3 | +# yamllint disable rule:comments-indentation |
| 4 | + |
| 5 | +- name: "Make sure {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} is symlink" # noqa 204 |
| 6 | + stat: |
| 7 | + path: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}" |
| 8 | + register: sym |
| 9 | + |
| 10 | +- block: # synchronize WAL`s if wal dir exist (and is not symlink) |
| 11 | + - name: Make sure rsync is installed (for synchronize wal dir) |
| 12 | + package: |
| 13 | + name: |
| 14 | + - rsync |
| 15 | + - sshpass |
| 16 | + state: present |
| 17 | + environment: "{{ proxy_env | default({}) }}" |
| 18 | + |
| 19 | + - name: Stop patroni service (for create symlink) |
| 20 | + systemd: |
| 21 | + name: patroni |
| 22 | + state: stopped |
| 23 | + register: patroni_stop_result |
| 24 | + |
| 25 | + - name: Make sure PostgreSQL is stopped |
| 26 | + become: true |
| 27 | + become_user: postgres |
| 28 | + command: "{{ postgresql_bin_dir }}/pg_ctl status -D {{ postgresql_data_dir }}" |
| 29 | + register: stop_result |
| 30 | + changed_when: false |
| 31 | + failed_when: false |
| 32 | + until: stop_result.rc == 3 |
| 33 | + retries: 100 |
| 34 | + delay: 6 |
| 35 | + |
| 36 | + - name: "Synchronize {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} to {{ postgresql_wal_dir }}" # noqa 204 |
| 37 | + become: true |
| 38 | + become_user: postgres |
| 39 | + synchronize: |
| 40 | + src: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}/" |
| 41 | + dest: "{{ postgresql_wal_dir }}/" |
| 42 | + delegate_to: "{{ inventory_hostname }}" |
| 43 | + |
| 44 | + - name: "Rename {{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} to {{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old" # noqa 204 |
| 45 | + command: mv {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old # noqa 204 |
| 46 | + register: mv_result |
| 47 | + when: sym.stat.exists and not sym.stat.islnk|bool |
| 48 | + |
| 49 | +- name: "Create symlink {{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }} -> {{ postgresql_wal_dir }}" # noqa 204 |
| 50 | + become: true |
| 51 | + become_user: postgres |
| 52 | + file: |
| 53 | + src: "{{ postgresql_wal_dir }}" |
| 54 | + dest: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}" |
| 55 | + state: link |
| 56 | + when: sym.stat.islnk is not defined or not sym.stat.islnk|bool |
| 57 | + |
| 58 | +- block: # start patroni |
| 59 | + - name: Start patroni service |
| 60 | + systemd: |
| 61 | + name: patroni |
| 62 | + state: started |
| 63 | + |
| 64 | + - name: Wait for port 8008 to become open on the host |
| 65 | + wait_for: |
| 66 | + port: 8008 |
| 67 | + host: "{{ hostvars[inventory_hostname]['inventory_hostname'] }}" |
| 68 | + state: started |
| 69 | + timeout: 120 |
| 70 | + delay: 10 |
| 71 | + ignore_errors: false |
| 72 | + |
| 73 | + - name: Check that the patroni is healthy |
| 74 | + uri: |
| 75 | + url: "http://{{ hostvars[inventory_hostname]['inventory_hostname'] }}:8008/health" |
| 76 | + status_code: 200 |
| 77 | + register: replica_result |
| 78 | + until: replica_result.status == 200 |
| 79 | + retries: 120 |
| 80 | + delay: 10 |
| 81 | + when: patroni_stop_result is changed |
| 82 | + |
| 83 | +- name: "Remove {{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old directory" |
| 84 | + file: |
| 85 | + path: "{{ postgresql_data_dir }}/{{ 'pg_wal' if postgresql_version is version('10', '>=') else 'pg_xlog' }}_old" |
| 86 | + state: absent |
| 87 | + when: |
| 88 | + - replica_result.status is defined |
| 89 | + - replica_result.status == 200 |
| 90 | + |
| 91 | +... |
0 commit comments