|
| 1 | +#!/usr/bin/ansible-playbook |
| 2 | +--- |
| 3 | +#--------------------------------------------------------------# |
| 4 | +# Modify pg_wal destination if pg_fs_wal defined [pg_wal] |
| 5 | +# new pg_wal is <pg_fs_wal>/<pg_cluster>-<pg_version>/pg_wal |
| 6 | +#--------------------------------------------------------------# |
| 7 | + |
| 8 | + |
| 9 | +- name: validate variable pg_fs_wal |
| 10 | + tags: [ validate_pg_fs_wal ] |
| 11 | + when: pg_fs_wal | length > 0 |
| 12 | + block: |
| 13 | + - name: validate pg_fs_wal "{{ pg_fs_wal }}" |
| 14 | + fail: |
| 15 | + msg: "The variable pg_fs_wal must start with '/' and not contain spaces" |
| 16 | + when: > |
| 17 | + (pg_fs_wal[0] != '/') or (' ' in pg_fs_wal) |
| 18 | +
|
| 19 | +- name: retrieve path of pg_wal directory |
| 20 | + become: yes |
| 21 | + block: |
| 22 | + - name: validate variable pg_data |
| 23 | + assert: |
| 24 | + that: pg_data is defined and pg_data | length > 0 |
| 25 | + msg: "pg_data must be defined" |
| 26 | + changed_when: false |
| 27 | + |
| 28 | + - name: get current pg_wal real path |
| 29 | + command: realpath -m "{{ pg_data }}/pg_wal" |
| 30 | + register: real_curr_pg_wal_cmd |
| 31 | + changed_when: false |
| 32 | + |
| 33 | + - name: calculate new pg_data path (parent of target pg_wal) |
| 34 | + set_fact: |
| 35 | + target_new_pg_data: >- |
| 36 | + {% if pg_fs_wal is defined and pg_fs_wal %} |
| 37 | + {{ pg_fs_wal | trim }}/{{ pg_cluster }}-{{ pg_version }} |
| 38 | + {% else %} |
| 39 | + {{ pg_data }} |
| 40 | + {% endif %} |
| 41 | + changed_when: false |
| 42 | + |
| 43 | + - name: get target pg_wal real path |
| 44 | + command: "realpath -m {{ target_new_pg_data }}" # do not quote the var like "{{ target_new_pg_data }}" |
| 45 | + register: new_pg_data_cmd |
| 46 | + changed_when: false |
| 47 | + |
| 48 | + - name: Set final path facts |
| 49 | + set_fact: |
| 50 | + new_pg_data: "{{ new_pg_data_cmd.stdout }}" |
| 51 | + real_curr_pg_wal: "{{ real_curr_pg_wal_cmd.stdout }}" |
| 52 | + pg_wal_dst: "{{ new_pg_data_cmd.stdout }}/pg_wal" |
| 53 | + changed_when: false |
| 54 | + |
| 55 | + - name: get current pg_wal stat |
| 56 | + stat: |
| 57 | + path: "{{ pg_data }}/pg_wal" # do not use real_curr_pg_wal, use pg_data instead |
| 58 | + register: real_curr_pg_wal_stat |
| 59 | + changed_when: false |
| 60 | + |
| 61 | + - name: Set real_curr_pg_wal_is_link variable |
| 62 | + set_fact: |
| 63 | + real_curr_pg_wal_is_link: "{{ real_curr_pg_wal_stat.stat.exists and real_curr_pg_wal_stat.stat.islnk }}" |
| 64 | + changed_when: false |
| 65 | + |
| 66 | + - name: variable output for pg_wal |
| 67 | + debug: |
| 68 | + msg: | |
| 69 | + real_curr_pg_wal: {{ real_curr_pg_wal }}, pg_wal_dst: {{ pg_wal_dst }}, |
| 70 | + real_curr_pg_wal_is_link: {{ real_curr_pg_wal_is_link }}, |
| 71 | + pg_data: {{ pg_data }}, new_pg_data: {{ new_pg_data }}, |
| 72 | + pg_fs_wal: {{ pg_fs_wal }}, |
| 73 | + changed_when: false |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +#--------------------------------------------------------------# |
| 78 | +# Modify WAL [modify_pg_wal] |
| 79 | +#--------------------------------------------------------------# |
| 80 | +- include_tasks: pg_wal/pg_wal_modification.yml |
| 81 | + tags: modify_pg_wal |
| 82 | + when: (real_curr_pg_wal != pg_wal_dst) and (pg_fs_wal | length > 0) |
| 83 | + |
| 84 | + |
| 85 | +#--------------------------------------------------------------# |
| 86 | +# Restore WAL [restore_pg_wal] |
| 87 | +#--------------------------------------------------------------# |
| 88 | +- include_tasks: pg_wal/pg_wal_restoration.yml |
| 89 | + tags: restore_pg_wal |
| 90 | + when: (real_curr_pg_wal != pg_wal_dst) and (pg_fs_wal is not defined or (pg_fs_wal | trim | length == 0)) |
| 91 | + |
| 92 | + |
| 93 | +... |
0 commit comments