AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible reboot Module: Restart Hosts and Wait for Recovery (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to use Ansible reboot module to safely restart servers. Wait for SSH, set timeout, handle kernel updates. Complete guide with playbook examples.

Ansible reboot Module: Restart Hosts and Wait for Recovery (Complete Guide)

The ansible.builtin.reboot module reboot a machine and wait for it to come back. This guide covers all common use cases with practical playbook examples.

See also: Ansible sysctl Module: Manage Kernel Parameters (Complete Guide)

Basic Reboot

- name: Reboot the server
  ansible.builtin.reboot:

# Ansible automatically waits for the host to come back

Reboot with Custom Timeout

- name: Reboot with extended timeout
  ansible.builtin.reboot:
    reboot_timeout: 600    # Wait up to 10 minutes
    connect_timeout: 30    # Connection attempt timeout
    pre_reboot_delay: 5    # Wait 5 seconds before reboot
    post_reboot_delay: 30  # Wait 30 seconds after reboot before checking
    msg: "Reboot initiated by Ansible for kernel update"

See also: Ansible hostname Module: Set System Hostname on Linux (Guide)

Conditional Reboot (Kernel Update)

- name: Update kernel
  ansible.builtin.apt:
    name: linux-generic
    state: latest
    update_cache: true
  register: kernel_update

- name: Reboot if kernel was updated ansible.builtin.reboot: msg: "Kernel updated, rebooting" when: kernel_update.changed

Reboot and Verify Service

- name: Reboot server
  ansible.builtin.reboot:

- name: Wait for application to be ready ansible.builtin.uri: url: "http://localhost:8080/health" status_code: 200 retries: 30 delay: 5 register: health until: health.status == 200

See also: Ansible on Bottlerocket: Reboot-aware Patching Workflow Complete Guide

Check if Reboot is Required

# Debian/Ubuntu
- name: Check if reboot required
  ansible.builtin.stat:
    path: /var/run/reboot-required
  register: reboot_required

- name: Reboot if required ansible.builtin.reboot: when: reboot_required.stat.exists

# RHEL/CentOS - name: Check if reboot needed (RHEL) ansible.builtin.command: needs-restarting -r register: needs_restart changed_when: false failed_when: false

- name: Reboot if needed ansible.builtin.reboot: when: needs_restart.rc == 1

FAQ

How do I reboot a server with Ansible?

Use ansible.builtin.reboot. It reboots the host and automatically waits for SSH to become available again before continuing with the next task.

How long does Ansible wait after a reboot?

By default, Ansible waits up to 600 seconds (10 minutes) for the host to come back. Adjust with reboot_timeout. Use post_reboot_delay to add extra wait time after SSH reconnects.

How do I check if a reboot is required in Ansible?

On Debian/Ubuntu, check if /var/run/reboot-required exists with ansible.builtin.stat. On RHEL, run needs-restarting -r and check the return code.

Conclusion

The ansible.builtin.reboot module is a versatile tool for reboot a machine and wait for it to come back. Use the examples above as starting points and adapt them to your infrastructure needs.

Related Articles

Ansible service Module: Manage ServicesAnsible wait_for Module

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home