Interview Prep/Ansible/Rolling Updates, Delegation and Safe Deployments

How do you run database migrations in a rolling deploy?

AdvancedProductionRolling Updates, Delegation and Safe Deployments
Answer

Run the migration once, before the app hosts update, and make it backward compatible so old and new app versions can run side by side during the rollout.

- hosts: app[0]
  tasks:
    - ansible.builtin.command: /opt/app/bin/migrate --to {{ app_version }}
      run_once: true
      register: mig
      changed_when: "'Applied' in mig.stdout"

- hosts: app
  serial: "25%"
  roles: [app]

Take a backup first, use a migration tool's own locking (Flyway, Alembic) so concurrent runs cannot corrupt state, and split risky changes into expand, deploy, contract steps across releases. Never rely on run_once alone to prevent two parallel pipeline runs; use pipeline concurrency controls too.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Safe Deployments