Installing Ansible on Ubuntu 20.04 LTS and CentOS 7
Ansible is one of the most popular automation tools used in the DevOps community mainly for the configuration of servers. It can also be used for provisioning servers on cloud service providers like AWS, GCP, Microsoft Azure, etc. This article is a simple guide on how to install Ansible on Ubuntu and CentOS servers.
What You’ll Learn
In this article, you’ll learn how to
- create non-root users with sudo privileges on Ubuntu and CentOS servers
- install ansible on Ubuntu and CentOS from the command line
- install ansible using a Bash script.
Pre-requisites
You would need the following:
- Ubuntu virtual machine (VM)
- CentOS virtual machine
- A non-root user with sudo privileges
You can spin up these virtual machines on any cloud provider of your choice. How to spin up VMs is beyond the scope of this article, however, you can learn about that from Sulaiman Adamu’s hands-on article on How to Create Ubuntu and Centos servers on Vagrant and ssh into them. Setting up local VMs helps you avoid the cost of spinning up VMs on cloud service providers.
Creating a Non-root Sudo User
- on Ubuntu
To create a non-root user with escalated privileges on Ubuntu, run the following commands as root:
adduser <name-of-user>
usermod -aG sudo <name-of-user>
- on CentOS
While the sudo group is present on Ubuntu, on CentOS we have the “wheel” group — an equivalent of Ubuntu’s sudo group. Therefore, run the following command as root:
adduser <name-of-user>
usermod -aG wheel <name-of-user>
Installing Ansible on Ubuntu VM
Switch to your newly created non-root user with
su <name-of-user>
cd /home/<name-of-user>
and run the following commands:
sudo apt update
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y
- sudo apt update: this is the command by which we update the primary package manager of our Ubuntu server, which is apt. It is good practice to run this command immediately after spinning up a new VM. Running this command the second time as shown in the code block above, refreshes our system’s package index so that it is aware of the packages available in the newly included Personal Package Archive (PPA).
- sudo apt-add-repository ppa:ansible/ansible: this command ensures that the official project’s PPA is included in your system’s list of apt sources.
- sudo apt install ansible -y: finally, we install the latest version of Ansible. You can check what version of Ansible is installed by running
ansible --version
Install with a Bash Script (Ubuntu)
#!/bin/sh
echo "INSTALLING ANSIBLE ON UBUNTU 20.04"
echo "UPDATING PACKAGE MANAGER"
sudo apt update
echo "INSTALLING SOURCE REPOSITORY"
sudo apt-add-repository ppa:ansible/ansible -y
echo "UPDATING PACKAGE INDEX WITH SOURCE REPOSITORY"
sudo apt update
echo "INSTALLING ANSIBLE"
sudo apt install ansible -y
echo "ANSIBLE VERSION"
ansible --version
Copy and save the script above in your home directory, and run -
sh /home/<name-of-user>/script_name.sh
Installing Ansible on CentOS 7 VM
The process is quite similar to that of Ubuntu, only that on CentOS, apt is replaced by “yum” which is the primary package manager of CentOS. After switching to your non-root user, run the following commands:
sudo yum update -y
sudo yum install epel-release -y
sudo yum update -y
sudo yum install ansible -y
ansible --version
The command, <yum install epel-release -y> ensures that the CentOS 7 EPEL repository is added to our package index.
Install with a Bash Script (CentOS)
#!/bin/sh
echo "INSTALLING ANSIBLE ON CENTOS 7"
echo "UPDATING PACKAGE MANAGER"
sudo yum update -y
echo "INSTALLING CENTOS 7 EPEL REPO"
sudo yum install epel-release -y
echo "UPDATING PACKAGE INDEX WITH EPEL REPO"
sudo yum update -y
echo "INSTALLING ANSIBLE"
sudo yum install ansible -y
echo "ANSIBLE VERSION"
ansible --version
Copy and save this script in your home directory, and run
sh /home/<name-of-user>/script_name.sh
This will run every command on the script, install Ansible and display the version installed.
Conclusion
Ansible is a very powerful tool, and a must-have for every System Administrator, Cloud, or DevOps engineer. To learn more about this amazing tool, visit Ansible’s official documentation website. Follow my tutorial series on Ansible to learn more about how to use this incredible tool. If you run into an error trying to run this script or if you have observations you would like to share, please chat with me on WhatsApp.