Patch Me If You Can: Keeping Debian VMs Updated with Ansible (Without Losing My Weekend)

Twelve virtual machines, each updated by hand over SSH – slow, boring and error-prone. There is a way that leaves the weekend free.

Comic: the man reads in a hammock in a data center while a robot arm patches the servers with band-aids

🌐 Also available in: Deutsch · Français · Español · Italiano · Português · Nederlands · Polski

In short: Updating around twelve Debian (opens in a new tab) VMs by hand, one after the other over SSH, was slow, boring and error-prone. Now a simple Ansible (opens in a new tab) playbook does it: it updates all machines, reboots them if needed and reports what happened. The post shows the playbook.

I have around 12 Debian-based VMs running in my homelab. A while ago, I used to update them all manually — one by one. SSH into each machine, run apt update && apt upgrade, wait, reboot if needed... then repeat. It worked, but it was slow, boring, and very easy to forget one or two machines.

Now I use Ansible to handle everything for me. I wrote a simple playbook, and with just one command, all my VMs update in parallel. If any of them need a reboot, Ansible (opens in a new tab) checks for that and takes care of it too. I just run the playbook and let the automation do the heavy lifting.

It saves me a ton of time, and honestly, it feels kind of awesome. I watch logs scroll by across multiple machines at once — and sip coffee like a sysadmin with superpowers 🙂

---
- name: Upgrade Debian-based systems
  hosts: homelab
  become: true
  vars:
    reboot_flag_path: /var/run/reboot-required

  tasks:
    - name: Update apt package index
      apt:
        update_cache: yes
      register: apt_update_result
      ignore_errors: no

    - name: Upgrade installed packages
      apt:
        upgrade: dist
      register: apt_upgrade_result
      ignore_errors: yes
      retries: 3
      delay: 10
      until: apt_upgrade_result is succeeded

    - name: Show upgrade results
      debug:
        var: apt_upgrade_result

    - name: Check if a reboot is required
      stat:
        path: "{{ reboot_flag_path }}"
      register: reboot_check

    - name: Reboot system if needed
      reboot:
        msg: "System will automatically reboot after upgrade"
        pre_reboot_delay: 10
        post_reboot_delay: 30
        reboot_timeout: 600
      when: reboot_check.stat.exists
      register: reboot_result

    - name: Report whether a reboot was performed
      debug:
        msg: >-
          {% if reboot_check.stat.exists %}
            System was rebooted.
          {% else %}
            No reboot required.
          {% endif %}

🎯 Conclusion

One playbook instead of twelve SSH sessions: updates now run the same way for every VM, no machine gets forgotten any more, and only those that need it get rebooted. My weekend belongs to me again. 😎


📚 Related posts:

Some text, images, videos and audio on this site are AI-assisted or AI-generated. Details