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

Show the standard pattern for a zero-downtime rolling deploy behind a load balancer.

IntermediateProductionRolling Updates, Delegation and Safe Deployments
Answer

Take each host out of rotation, change it, verify it, put it back, in small batches.

- hosts: web
  serial: "25%"
  max_fail_percentage: 0
  pre_tasks:
    - community.general.haproxy:
        state: disabled
        host: "{{ inventory_hostname }}"
        backend: app
        wait: true
      delegate_to: "{{ groups['lb'][0] }}"
  roles:
    - app
  post_tasks:
    - ansible.builtin.uri:
        url: "http://{{ inventory_hostname }}:8080/health"
        status_code: 200
      register: h
      until: h.status == 200
      retries: 20
      delay: 3
    - community.general.haproxy:
        state: enabled
        host: "{{ inventory_hostname }}"
        backend: app
      delegate_to: "{{ groups['lb'][0] }}"

max_fail_percentage: 0 stops the rollout on the first failed host, leaving the rest on the old, working version.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Safe Deployments