Restart host if needed #5

Open
opened 2026-07-23 18:00:22 +02:00 by alessandrovitali · 0 comments
Owner

Option A: Conditional Reboot (Recommended)
On Debian/Ubuntu systems, when a package upgrade requires a system reboot (such as a kernel update), the OS writes a file to /var/run/reboot-required.

Checking for this file prevents unnecessary reboots during routine runs where no kernel upgrades occurred. You can place this at the end of your base/tasks/main.yml:

- name: Check if reboot is required
  ansible.builtin.stat:
    path: /var/run/reboot-required
  register: reboot_required_file

- name: Reboot the server if required
  ansible.builtin.reboot:
    msg: "Rebooting to apply system/kernel upgrades"
  become: true
  when: reboot_required_file.stat.exists

Option B: Role Handler (Clean & Post-Execution)

If you want the base role to handle the reboot automatically when packages are updated, you can register the result of the upgrade task and notify a handler. Because Ansible handlers execute at the very end of the play, this ensures all other configuration tasks complete before the machine reboots.

In roles/base/tasks/main.yml:

- name: Fully upgrade system
  ansible.builtin.apt:
    update_cache: true
    upgrade: dist
    cache_valid_time: 3600
  register: upgrade_result
  become: true
  notify: Reboot host

In roles/base/handlers/main.yml:

---
- name: Reboot host
  ansible.builtin.reboot:
    msg: "Rebooting after system upgrades"
  become: true

Option C: Playbook-Level post_tasks (Orchestration Level)

If you run these roles inside a larger playbook, you might want to defer any reboots to the very end of the playbook execution. This keeps the roles clean and lets the master playbook handle host lifecycle states.

- name: Provision Homelab VM
  hosts: all
  roles:
    - base
    - users
    - security
    - docker

  post_tasks:
    - name: Check if reboot is required
      ansible.builtin.stat:
        path: /var/run/reboot-required
      register: reboot_required_file

    - name: Reboot host if needed
      ansible.builtin.reboot:
        msg: "Rebooting host after provisioning"
      become: true
      when: reboot_required_file.stat.exists
Option A: Conditional Reboot (Recommended) On Debian/Ubuntu systems, when a package upgrade requires a system reboot (such as a kernel update), the OS writes a file to `/var/run/reboot-required`. Checking for this file prevents unnecessary reboots during routine runs where no kernel upgrades occurred. You can place this at the end of your `base/tasks/main.yml`: ```yaml - name: Check if reboot is required ansible.builtin.stat: path: /var/run/reboot-required register: reboot_required_file - name: Reboot the server if required ansible.builtin.reboot: msg: "Rebooting to apply system/kernel upgrades" become: true when: reboot_required_file.stat.exists ``` #### Option B: Role Handler (Clean & Post-Execution) If you want the `base` role to handle the reboot automatically when packages are updated, you can register the result of the upgrade task and notify a handler. Because Ansible handlers execute at the **very end of the play**, this ensures all other configuration tasks complete before the machine reboots. **In `roles/base/tasks/main.yml`:** ```yaml - name: Fully upgrade system ansible.builtin.apt: update_cache: true upgrade: dist cache_valid_time: 3600 register: upgrade_result become: true notify: Reboot host ``` **In `roles/base/handlers/main.yml`:** ```yaml --- - name: Reboot host ansible.builtin.reboot: msg: "Rebooting after system upgrades" become: true ``` #### Option C: Playbook-Level `post_tasks` (Orchestration Level) If you run these roles inside a larger playbook, you might want to defer any reboots to the very end of the playbook execution. This keeps the roles clean and lets the master playbook handle host lifecycle states. ```yaml - name: Provision Homelab VM hosts: all roles: - base - users - security - docker post_tasks: - name: Check if reboot is required ansible.builtin.stat: path: /var/run/reboot-required register: reboot_required_file - name: Reboot host if needed ansible.builtin.reboot: msg: "Rebooting host after provisioning" become: true when: reboot_required_file.stat.exists
alessandrovitali added the bug label 2026-08-16 23:53:32 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: studio/ansible#5