#!/usr/bin/env ansible-playbook
---
- name: POSTGRESQL SWITCH CURRENT STANDBY TO PRIMARY
  hosts: postgres_standby
  tasks:
    - name: fail if node status if not standby
      ansible.builtin.fail:
        msg: Current status {{ rephacheck['stdout'] }} must be standby.
      when: rephacheck['stdout'] != "standby"

    - name: check if node is currently in standby
      ansible.builtin.command:
        cmd: repmgr standby switchover -f /etc/postgresql/13/main/repmgr.conf --siblings-follow --dry-run
      become: true
      become_user: postgres
      changed_when: false
      register: standby_dry_run
      when: rephacheck['stdout'] == "standby"

    - name: switch standby node to primary
      ansible.builtin.command:
        cmd: repmgr standby switchover -f /etc/postgresql/13/main/repmgr.conf --siblings-follow
      become: true
      become_user: postgres
      changed_when: false
      when:
        - standby_dry_run is succeeded
        - rephacheck['stdout'] == "standby"
