Interview Prep/Real-World Interview/Write-an-Ansible-playbook tasks

Write a playbook that waits for hosts to come back after a reboot, only when a reboot is needed.

IntermediateHands-onWrite-an-Ansible-playbook tasks
Answer
- hosts: all
  become: true
  serial: "25%"
  tasks:
    - name: Upgrade packages
      ansible.builtin.apt: { upgrade: dist, update_cache: true }

    - name: Check if reboot required
      ansible.builtin.stat: { path: /var/run/reboot-required }
      register: rr

    - name: Reboot if needed
      ansible.builtin.reboot:
        reboot_timeout: 600
        msg: "Reboot by Ansible for kernel update"
      when: rr.stat.exists

Key trick → The reboot module reboots, waits for SSH and checks the host is back, with no fragile wait_for_connection dance. serial: "25%" keeps 75% of the fleet serving.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-an-Ansible-playbook tasks