The machine on which we install Ansible (this is known as the Ansible control machine) should be running on any Linux distribution. In this recipe, we will outline how to install Ansible on both an Ubuntu Linux machine or a CentOS machine.
Installing Ansible
Getting ready
To install Ansible, we need a Linux VM using either an Ubuntu 18.04+ OS or CentoS 7+ OS. Furthermore, this machine needs to have internet access for Ansible to be installed on it.
How to do it...
Ansible is written in Python and all its modules need Python to be installed on the Ansible control machine. Our first task is to ensure that Python is installed on the Ansible control machine, as outlined in the following steps.
- Most Linux distributions have Python installed by default. However, if Python is not installed, here are the steps for installing it on Linux:
-
- On an Ubuntu OS, execute the following command:
# Install python3
$sudo apt-get install python3
# validate python is installed
$python3 --version
Python 3.6.9
-
- On a CentOS OS, execute the following command:
# Install python
$sudo yum install pytho3
# validate python is installed
$python3 --version
Python 3.6.8
- After we have validated that Python is installed, we can start to install Ansible:
-
- On an Ubuntu OS, execute the following command:
# We need to use ansible repository to install the latest version of Ansible
$ sudo apt-add-repository ppa:ansible/ansible
# Update the repo cache to use the new repo added
$ sudo apt-get update
# We install Ansible
$ sudo apt-get install ansible
-
- On a CentOS OS, execute the following command:
# We need to use latest epel repository to get the latest ansible
$ sudo yum install epel-release
# We install Ansible
$ sudo yum install ansible
How it works..
The easiest way to install Ansible is by using the package manager specific to our Linux distribution. We just need to make sure that we have enabled the required repositories to install the latest version of Ansible. In both Ubuntu and CentOS, we need to enable extra repositories that provide the latest version for Ansible. In CentOS, we need to install and enable the Extra Packages for Enterprise Linux Repository (EPEL repo), which provides extra software packages and has the latest Ansible packages for CentOS.
Using this method, we will install Ansible and all the requisite system packages needed to run the Ansible modules. In both Ubuntu and CentOS, this method will also install Python 2 and run Ansible using Python 2. We can validate the fact that Ansible is installed and which version is used by running the following command:
$ ansible --version
ansible 2.9
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/vagrant/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Also, we can check that Ansible is working as expected by trying to connect to the local machine using the ping module as shown:
$ ansible -m ping localhost
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
Using this method, we can see that it has the following issues:
- It uses Python 2 as the execution environment, but we want to use Python 3.
- It updates the Python packages installed on the system, which might not be desirable.
- It doesn't provide us with the granularity needed in order to select which version of Ansible to use. Using this method, we will always install the latest version of Ansible, which might not be what we need.
How it works...
In order to install Ansible in a Python 3 environment and to have more control over the version of Ansible deployed, we are going to use the pip Python program to install Ansible as shown here:
- Install Python 3 if it is not present, as follows:
# Ubuntu
$ sudo apt-get install python3
# CentOS
sudo yum install python3
- Install the python3-pip package:
# Ubuntu
$ sudo apt-get install python3-pip
# CentOS
$ sudo yum install python3-pip
- Install Ansible:
# Ubuntu and CentOS
# This will install ansible for the current user ONLY
$ pip3 install ansible==2.9 --user
# We Can install ansible on the System Level
$ sudo pip3 install ansible==2.9
- We can verify that Ansible has been installed successfully, as shown here:
$$ ansible --version
ansible 2.9
config file = None
configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/vagrant/.local/lib/python3.6/site-packages/ansible
executable location = /home/vagrant/.local/bin/ansible
python version = 3.6.8 (default, Aug 7 2019, 17:28:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Installing Ansible using this method ensures that we are using Python 3 as our execution environment and allows us to control which version of Ansible to install, as outlined in the example shown.
We are going to use this method as our Ansible installation method and all the subsequent chapters will be based on this installation procedure.
See also...
For more information regarding the installation of Ansible, please check the following URL:
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html