Update All Packages on Ubuntu Server With Ansible

This blog post, Will show you to update all software packages on a Ubuntu server using an Ansible playbook without rebooting the server.

Apt Module

By default, Ansible comes with the Apt module which allows us to update packages on Ubuntu servers. The module is capable of doing the same things as running the apt command directly on a server.

Inventory file

Below is my inventory file and host vars for the host login configuration. The become options are very important and In my case, I am using the following:

You can also see that I am using SSH authntication to connect to my managed hosts.

[servers]
  192.168.100.1
 [servers:vars]
 ansible_private_key_file=/home/user/.ssh/id_rsa
 ansible_user=admin
 ansible_become=yes
 ansible_become_method=sudo
 ansible_python_interpreter='/usr/bin/python3'

Playbook

The following playbook will install all the available packages on the managed hosts without restarting them.

- hosts: servers
  tasks:
    - name: Update cache 
      apt:
       update_cache: yes
         
    - name: Upgrade all packages on servers
      apt: 
        name: "*"
        state: latest
      

To run the playbook I will use the following command.

sudo ansible-playbook playbooks/update-ubuntu.yml -i hosts

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.