Upgrade CentOS|RHEL|Ubuntu|Debian Systems Using Ansible

Ansible is a powerful automation tool created to be purely open-source. With this automation tool you can perform configuration management and applications deployment with ease, to deliver fully working infrastructure as code. Ansible can be installed on many Unix-like systems, with the ability to configure both Unix-like systems as well as Microsoft Windows.

In this short guide we show you how to perform a system upgrade and reboot on CentOS / RHEL / Ubuntu and Debian Linux systems using Ansible. We assume you have ansible installed and working on your Local System where the actions will be performed.

Step 1: Installing Ansible Automation tool

Ansible can be installed on many Linux systems with pip, the Python package manager.

If pip is not already available on your system, run the following commands to install it:

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

# Python 2
$ python get-pip.py --user

# Python 3
$ python3 get-pip.py --user

Once pip is installed, use it to install Ansible on your local machine:

# Python 2
python -m pip install --user ansible
python -m pip install --user paramiko

# Python 3
python3 -m pip install --user ansible
python3 -m pip install --user paramiko

If you wish to install Ansible globally, run the following commands:

# Python 2
sudo python get-pip.py
sudo python -m pip install ansible

# Python 3
sudo python3 get-pip.py
sudo python3 -m pip install ansible

Step 2: Write Ansible Playbook for upgrading system

Once the Ansible tool is installed, you can then begin the creation of a Playbook that will perform upgrade and system reboot upon successful upgrades.

Let’s create a new playbook file:

vim upgrade_reboot.yml

We will paste the contents below into the file.

---
- name: Upgrade and Reboot RHEL & Debian family Linux distros
  hosts: all
  vars:
    reboot_connect_timeout: 5
    reboot_post_reboot_delay: 15
    reboot_timeout: 600
  tasks:
    # Upgrade RHEL family OS packages
    - name: Upgrade RHEL Family OS packages
      yum:
        name: '*'
        state: latest
      when: ansible_os_family == "RedHat"

    # Debian Family upgrade
    - name: Update repositories cache
      apt:
        update_cache: yes
      when: ansible_os_family == "Debian"
    - name: Update all packages to their latest version
      apt:
        name: "*"
        state: latest
      when: ansible_os_family == "Debian"
    - name: Upgrade the OS (apt-get dist-upgrade)
      apt:
        upgrade: dist
      when: ansible_os_family == "Debian"

    # Reboot after upgrade
    - name: Reboot host
      reboot:
        connect_timeout: "{{ reboot_connect_timeout }}"
        post_reboot_delay: "{{ reboot_post_reboot_delay }}"
        reboot_timeout: "{{ reboot_timeout }}"

Then create a hosts which contains inventory of systems to be updated and upgraded:

vim hosts

Set user authentication credentials and add the systems IP addresses or hostnames. I have two servers to be upgraded:

[all:vars]
ansible_user='root' #set ssh user accordingly
ansible_become=yes
ansible_become_method=sudo

[servers]
192.168.1.179
192.168.1.182

Step 3: Run the Playbook

If you don’t want to use password authentication make sure ssh public key is copied to the servers:

ssh-copy-id [email protected] #replace root with your ssh user & correct IP
ssh-copy-id [email protected] #replace root with your ssh user & correct IP

Check playbook syntax before execution:

$ ansible-playbook --syntax-check upgrade_reboot.yml -i hosts
playbook: upgrade_reboot.yml

Once the key is copied, run the playbook:

ansible-playbook -i hosts upgrade_reboot.yml

Command execution:

PLAY [Upgrade and Reboot RHEL & Debian family Linux distros] *****************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [192.168.1.179]
ok: [192.168.1.182]

TASK [Upgrade RHEL Family OS packages] ***************************************************************************************************************************
skipping: [192.168.1.179]
changed: [192.168.1.182]

TASK [Update repositories cache] *********************************************************************************************************************************
skipping: [192.168.1.182]
changed: [192.168.1.179]

TASK [Update all packages to their latest version] ***************************************************************************************************************
skipping: [192.168.1.182]
changed: [192.168.1.179]

TASK [Upgrade the OS (apt-get dist-upgrade)] *********************************************************************************************************************
skipping: [192.168.1.182]
ok: [192.168.1.179]

TASK [Reboot host] ***********************************************************************************************************************************************
changed: [192.168.1.182]
changed: [192.168.1.179]

PLAY RECAP *******************************************************************************************************************************************************
192.168.1.179              : ok=5    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0
192.168.1.182              : ok=3    changed=2    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0

You can further tune the playbook to accommodate custom requirements around system updates and upgrades. Visit the official Ansible documentation to learn more.

We have other articles on Ansible as shared below:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

In this tutorial we will cover step-by-step procedure of compiling Open vSwitch (OVS) from source code on Rocky Linux, AlmaLinux, […]

Open vSwitch (OVS) is an open source virtual switch widely adopted in network virtualization. OVS is popular in platforms like […]

Photoshop can be such a headache for both experts and beginners alike. Every time you open photoshop there’s always a […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.