Ansible apt update all packages on Ubuntu / Debian Linux

I have a bunch of servers powered by Ubuntu and Debian Linux. How do I update all of them using the apt module of Ansible? How can I use Ansible for system updates and reboot the box when kernel upgrades took place?

Keeping your system up to date and applying all security patches is an essential task for sysadmins and developers. One can use the apt module of Ansible to manages apt packages for Debian/Ubuntu-based Linux distros. This module can either use aptitude or apt-get command on the remote server for package management. Similarly, it would be best if you used the reboot module of Ansible to reboot a machine, wait for it to go down, come back up, and respond to commands. This page explains how to run apt/apt-get update and upgrade all packages via Ansible and reboot the machine if the need occurs.

Ansible apt update all packages using apt module

Typically you run the following to refresh package cache using the apt-get command or apt command:
sudo apt-get update
To update repositories cache using Ansible:

    - name: Update apt-get repo and cache
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600


  1. update_cache=yes – Run the equivalent of apt-get update command on all servers
  2. force_apt_get=yes – Do not use the aptitude command, instead use the apt-get command on Debian/Ubuntu boxes
  3. cache_valid_time=3600 – Update the apt cache if its older than the cache_valid_time. This option is set in seconds. In this examples, it is set to 3600 seconds.

Upgrading all apt packages using Ansible

The syntax is:

   - name: Upgrade all apt packages
      apt: upgrade=dist force_apt_get=yes


  1. upgrade=dist – Run the equivalent of ‘apt-get upgrade’ command on all Ubuntu or Debian Linux servers. In other words, upgrade all packages to latest version.
  2. force_apt_get=yes – Use apt-get instead of aptitude.

Find out if we need to reboot the servers

If the file /var/run/reboot-required exists, you need to reboot your Debian or Ubuntu Linux box. We need to register a new variable if file /var/run/reboot-required exists on the system as follows:

    - name: Check if a reboot is needed for Debian and Ubuntu boxes
      register: reboot_required_file
      stat: path=/var/run/reboot-required get_md5=no


  1. register: reboot_required_file – The ‘register’ keyword decides what variable to save a result in and we are going to use it as follows to reboot the box.
  2. stat: path=/var/run/reboot-required – Determine if a path (/var/run/reboot-required) exists
  3. get_md5=no – Algorithm to determine checksum of file. In this example, I am using md5, but you can use sha1, sha224, sha256, sha384, and sha512.

Rebooting server when a new kernel installed

You can use either command or shell module to reboot the Linux server when kernel updated as follows:

    - name: Reboot the Debian or Ubuntu server
        msg: "Reboot initiated by Ansible due to kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists


  1. test_command: uptime – Execute uptime command on the rebooted server and expect success from to determine the machine is ready for further tasks.
  2. when: reboot_required_file.stat.exists – First, check that the file named /var/run/reboot-required exists using a variable named reboot_required_file. The reboot module will only work if that file exists and it is enforced using ‘when: reboot_required_file.stat.exists’ Ansible condition.

Using Ansible for system updates and reboot Linux servers if necessary

Now that you are aware of basic logic let us create a new host file:
vi hosts
Append the following:

## set up ssh user name and path to python3 ##
ansible_python_interpreter='/usr/bin/env python3'
## our aws server names
## aws-ls-www-1 may be mapped using /etc/hosts or ~/.ssh/config
## you can use ip address here too

Sample playbook

Create a new file named update.yml as follows:
vi update.yml
Append the following Ansbile code:

- hosts: servers
  become: true
  become_user: root
    - name: Update apt repo and cache on all Debian/Ubuntu boxes
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

    - name: Upgrade all packages on servers
      apt: upgrade=dist force_apt_get=yes

    - name: Check if a reboot is needed on all servers
      register: reboot_required_file
      stat: path=/var/run/reboot-required get_md5=no

    - name: Reboot the box if kernel updated
        msg: "Reboot initiated by Ansible for kernel updates"
        connect_timeout: 5
        reboot_timeout: 300
        pre_reboot_delay: 0
        post_reboot_delay: 30
        test_command: uptime
      when: reboot_required_file.stat.exists

Make sure you set up ssh keys and run it as follows:
ansible-playbook -i hosts update.yml


You learned how to update all packages on your Debian and Ubuntu Linux boxes and reboot the server if required using Ansible playbooks. See docs here for more info.

This entry is 1 of 2 in the Tutorial series. Keep reading the rest of the series:
  1. Ubuntu / Debian Linux update and reboot server
  2. OpenSUSE/SUSE Linux update packages and reboot box

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum