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 -o
# Python 2
$ python --user

# Python 3
$ python3 --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
$ sudo python -m pip install ansible

# Python 3
$ sudo python3
$ sudo python3 -m pip install ansible

Step 2: Write Ansible Playbook to Upgrade Linux Systems

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
    reboot_connect_timeout: 5
    reboot_post_reboot_delay: 15
    reboot_timeout: 600
    # Upgrade RHEL family OS packages
    - name: Upgrade RHEL Family OS packages
        name: '*'
        state: latest
      when: ansible_os_family == "RedHat"

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

    # Reboot after upgrade
    - name: Reboot host
        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:

ansible_user='root' #set ssh user accordingly


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: []
ok: []

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

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

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

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

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

PLAY RECAP *******************************************************************************************************************************************************              : ok=5    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0              : 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:

How To Manage Docker Containers With Ansible

How To Install and Use Ansible on Linux Mint

Provision VMs on Openstack with Terraform