Ansible reboot a Debian/Ubuntu Linux for kernel update and wait for it

How can I reboot a Debian or Ubuntu Linux server/host remotely using an Ansible playbook for kernel update and wait for it to come back again?

Introduction: It is pretty standard to update a large number of cloud servers or bare metal server using Ansible IT automation or DevOps tool. When new kernel installed, you must reboot the Debian or Ubuntu Linux server. This page shows how to reboot the machine using shell or command module and wait for it to come back.

ADVERTISEMENTS

Ansible modules you need to use

  1. apt – Manages apt packages for Debian/Ubuntu Linux such as install a new package or update package.
  2. command or shell – Execute commands in nodes using shell module. Executes a command on a remote node using command module. Use any one of the module to reboot the box when kernel updated.
  3. wait_for_connection – Waits until remote system is reachable/usable.

Ansible reboot a Debian/Ubuntu Linux for kernel update and wait for it

Let us see how to use these tree Ansible modules to reboot a Debian/Ubuntu Linux kernel update and wait for it to come back online again.

Update your Debian or Ubuntu box in Ansible

The playbook should be as follows:

      - name: Update all packages
        apt:
            update_cache: yes
            upgrade: dist

Make Ansible to wait for a server to reboot and continue playbook to work with shell module

Update playbook as follows:

      - name: Reboot box if kernel/libs updated and requested by the system
        shell: sleep 10 && /sbin/shutdown -r now 'Rebooting box to update system libs/kernel as needed' 
        args:
            removes: /var/run/reboot-required
        async: 300
        poll: 0
        ignore_errors: true

If the file /var/run/reboot-required exists on a Debian or Ubuntu Linux (see How to find out if my Ubuntu/Debian Linux server needs a reboot), you need to reboot the Linux server using the shutdown/reboot command. Please notice that I added sleep command before shutdown -r now, then use async with shell module. It forces the playbook the shell module asynchronously. The removes is some sort of condition. If A filename called /var/run/reboot-required does not exist, the shutdown command will not be run. We only need to run the reboot command or shutdown command if kernel updated.

Reboot and wait for reboot to complete in Ansible

The final puzzle of waiting is as follows:

      - name: Wait for system to become reachable again
        wait_for_connection:
            delay: 60
            timeout: 300

Ansible now waits for a total of 300 seconds. I also added 60 number of seconds to wait before starting to poll.

Reboot a Host Remotely using an Ansible Playbook

Ansible reboot a Debian/Ubuntu Linux for kernel update and wait for it complete example:

# update.yml 
---
- hosts: cluster
  tasks:
      - name: Update all packages on a Debian/Ubuntu
        apt:
            update_cache: yes
            upgrade: dist
      
      - name: Reboot box if kernel/libs updated and requested by the system
        shell: sleep 10 && /sbin/shutdown -r now 'Rebooting box to update system libs/kernel as needed' 
        args:
            removes: /var/run/reboot-required
        async: 300
        poll: 0
        ignore_errors: true
      
      - name: Wait for system to become reachable again
        wait_for_connection:
            delay: 60
            timeout: 300

      - name: Verify new update (optional)
        command: uname -mrs
        register: uname_result
      - name: Display new kernel version
        debug:
            var: uname_result.stdout_lines

Here is my hosts file displayed using the cat command:

[all:vars]
ansible_user=vivek
ansible_port=22
ansible_python_interpreter='/usr/bin/env python3'
ansible_become=yes
ansible_become_method=sudo
 
[cluster]
mysql2 ansible_host=192.168.1.10
mysql1 ansible_host=192.168.1.11
mysql3 ansible_host=192.168.1.12

Now just it as follows:
$ ansible-playbook -i hosts update.yml
Ansible reboot a Debian/Ubuntu Linux for kernel update and wait for it

Conclusion

You just learned how to reboot a Debian or Ubuntu Linux server remotely using an Ansible playbook and wait for it to continue operation. For more info see Ansible docs here.

This entry is 1 of 2 in the Ansible Reboot a Machine/Server Tutorial series. Keep reading the rest of the series:
  1. Ansible reboot a Debian/Ubuntu Linux for kernel update and wait for it
  2. Ansible reboot Linux machine or server with playbooks
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.