Interview Prep/Ansible/Rolling Updates, Delegation and Orchestration

You must restart database replicas one at a time but never the primary first.

AdvancedScenarioRolling Updates, Delegation and Orchestration
Answer

Situation read → Ordering matters and roles differ.

What I'd do → Two plays: replicas with serial: 1, then the primary after a controlled failover.

- hosts: db_replicas
  serial: 1
  tasks:
    - ansible.builtin.include_role: { name: pg_upgrade }
    - ansible.builtin.command: pg_isready
      register: r
      until: r.rc == 0
      retries: 20
      delay: 5

- hosts: db_primary
  tasks:
    - ansible.builtin.command: patronictl switchover --force --candidate "{{ new_primary }}"
      run_once: true
    - ansible.builtin.include_role: { name: pg_upgrade }

Why → Ansible plays run in order, so order is explicit and each stage is gated.

Interview tip → Check replication lag before moving on.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Orchestration