---
#--------------------------------------------------------------#
# Remove pgbackrest                                 [pgbackrest]
#--------------------------------------------------------------#
# when cleanup primary, remove pgbackrest repo too
- name: stop pgbackrest and delete stanza with timeout
  when: pg_role == 'primary'
  become: true
  become_user: "{{ pg_dbsu }}"
  ignore_errors: true
  shell: |
    if [[ -f /etc/pgbackrest/pgbackrest.conf ]]; then
      # stop with timeout, force kill if needed
      /usr/bin/pgbackrest --stanza={{ pg_cluster }} --force stop
      stop_result=$?
      if [[ $stop_result -eq 124 ]]; then
        echo "pgbackrest stop timeout, force killing processes"
        pkill -f "pgbackrest.*{{ pg_cluster }}" || /bin/true
      fi
      
      # delete stanza with timeout
      /usr/bin/pgbackrest --stanza={{ pg_cluster }} --force stanza-delete
      delete_result=$?
      if [[ $delete_result -eq 124 ]]; then
        echo "pgbackrest stanza-delete timeout, but continuing"
      fi
    fi
    exit 0
  args: { executable: /bin/bash }

# stop file: /tmp/pgbackrest/{{ pg_cluster }}.stop

- name: remove local postgres backup dir
  when: pgbackrest_method == 'local'
  file: path={{ pg_fs_backup }} state=absent

- name: remove postgres backup config
  file: path={{ item }} state=absent
  with_items:
    - /etc/pgbackrest/pgbackrest.conf
    - /etc/pgbackrest/initial.done
...