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.
More on Write-an-Ansible-playbook tasks
Write a playbook that installs and starts Nginx on Ubuntu web servers.BeginnerWrite an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.Advanced