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 requiredansible.builtin.stat:path:/var/run/reboot-requiredregister:reboot_required_file- name:Reboot the server if requiredansible.builtin.reboot:msg:"Rebooting to apply system/kernel upgrades"become:truewhen: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.
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 VMhosts:allroles:- base- users- security- dockerpost_tasks:- name:Check if reboot is requiredansible.builtin.stat:path:/var/run/reboot-requiredregister:reboot_required_file- name:Reboot host if neededansible.builtin.reboot:msg:"Rebooting host after provisioning"become:truewhen: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
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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:Option B: Role Handler (Clean & Post-Execution)
If you want the
baserole 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:In
roles/base/handlers/main.yml: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.