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

How do you patch a fleet and reboot only when needed?

IntermediateProductionRolling Updates, Delegation and Safe Deployments
Answer
- hosts: all
  serial: "10%"
  become: true
  tasks:
    - ansible.builtin.apt:
        upgrade: dist
        update_cache: true
    - ansible.builtin.stat: { path: /var/run/reboot-required }
      register: reboot_flag
    - ansible.builtin.reboot:
        reboot_timeout: 900
      when: reboot_flag.stat.exists

On RHEL family check needs-restarting -r (return code 1 means reboot required). Patch in rings (dev, stage, canary, then the rest), keep redundancy with serial, take nodes out of the load balancer first, and schedule within a change window.

All Ansible questions Roadmap: Ansible

More on Rolling Updates, Delegation and Safe Deployments