#!/usr/bin/env ansible-playbook
---
- name: POSTGRESQL SWITCH CURRENT STANDBY TO PRIMARY
  hosts: postgres_standby
  tasks:
    - name: fail if node status if not standby
      fail:
        msg: "Current status {{ rephacheck['stdout'] }} must be standby."
      when: rephacheck['stdout'] != "standby"
    - name: check if node is currently in standby
      command: "repmgr standby switchover -f /etc/postgresql/11/main/repmgr.conf --siblings-follow --dry-run"
      become: true
      become_user: postgres
      when: rephacheck['stdout'] == "standby"
      register: standby_dry_run
    - name: switch standby node to primary
      command: "repmgr standby switchover -f /etc/postgresql/11/main/repmgr.conf --siblings-follow"
      become: true
      become_user: postgres
      when:
        - standby_dry_run is succeeded
        - rephacheck['stdout'] == "standby"

...
