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: