Deploying Ansible: Step by step Installation on Ubuntu and CentOS using different methods

Deploying Ansible: Step by step Installation on Ubuntu and CentOS using different methods

Play this article

In this tutorial I will demonstrate different ways to install Ansible on one of the most popular Linux Operating systems (CentOS and Ubuntu).

Prerequisites

  1. One Ansible Control Node The Ansible control or mater node will be the machine we will use to connect to and control the Ansible hosts over SSH.

My Environment

HostnameIP addressOperating SystemDefault Python Version
lco-linux-master.example.com172.62.62.100Ubuntu 18.04.4 LTSPython 2.7.17
lco-ansible-master.example.com172.52.52.200CentOS Linux 7 (Core)Python 2.7.5

Here I have taken two Ansible master nodes just to demonstrate installation steps for two most popular operating systems RHEL based CentOS and Debian based Ubuntu.

Ansible python compatibility matrix

As per official documentation Ansible can be run from any machine with Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed. This includes Red Hat, Debian, CentOS, macOS, any of the BSDs, and so on. Windows is not supported for the control node.

On the controller Ansible supports Python 3.5 or greater and Python 2.7 or greater. Module-side, they support Python 3.5 or greater and Python 2.6 or greater.

Now lets start the practical demonstration of installing Ansible.

Installing Ansible on Ubuntu

There are two methods of installing Ansible on Ubuntu OS. One is with the apt package manager by configuring official ansible PPA repository and second is with python-pip method.

Installing Ansible with apt package manager

On your Master node, run the following command to include the official project’s PPA (personal package archive) in your system’s list of sources:

root@lco-linux-master:~# apt-add-repository ppa:ansible/ansible

You should press ENTER when it prompts for accepting PPA addition.

Update the system’s package index so that it can include the packages available in the newly included PPA:

root@lco-linux-master:~# apt update

Now you can install the Ansible by running following command:

root@lco-linux-master:~# apt install ansible

On successful installation of Ansible verify the binary location and the installed version:

root@lco-linux-master:~# which ansible
/usr/bin/ansible
root@lco-linux-master:~# ansible --version
ansible 2.9.16
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Apr 15 2020, 17:20:14) [GCC 7.5.0]

Congratulations! You have successfully installed the latest available version of Ansible from PPA repository which is 2.9.x.

Uninstalling Ansible using apt package manager

root@lco-linux-master:~# apt-get remove ansible

Installing Ansible on CentOS

Like Ubuntu here also we have two methods of installing Ansible on CentOS. One is with the yum package manager by configuring official ansible PPA repository and second is with python-pip method which is common and we will discuss that at the end.

Installing Ansible with yum package manager

On your master node run the following command to make sure CentOS 7 EPEL repository is installed:

[root@lco-ansible-master ~]# yum install epel-release -y

List down the available repositories on your system:

[root@lco-ansible-master ~]# yum repolist | grep epel
 * epel: epel.mirror.angkasa.id
epel/x86_64           Extra Packages for Enterprise Linux 7 - x86_64      13,494

We can see EPEL repo as part of our repository list.

Now install Ansible with yum:

[root@lco-ansible-master ~]# yum install ansible -y

On successful installation of Ansible verify the binary location and the installed version:

[root@lco-ansible-master ~]# which ansible
/usr/bin/ansible
[root@lco-ansible-master ~]# ansible --version
ansible 2.9.15
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Uninstalling Ansible using apt package manager

[root@lco-ansible-master ~]# yum remove ansible -y

Installing Ansible with Python pip

First make sure you have pip installed on your system.

root@lco-linux-master:~# whereis pip
pip:

root@lco-linux-master:~# pip --version
-bash: pip: command not found

I need to install the pip first.

Installing pip for Python 2
root@lco-linux-master:~# apt install python-pip

Now verify the version again:

root@lco-linux-master:~# pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Installing pip for Python 3
root@lco-linux-master:~# apt install python3-pip

verify the pip version:

root@lco-linux-master:~# pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Recommended is to update pip package after installation.

root@lco-linux-master:~# pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/eb/4a3642e971f404d69d4f6fa3885559d67562801b99d7592487f1ecc4e017/pip-20.3.3-py2.py3-none-any.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 953kB/s
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
Successfully installed pip-20.3.3

Installing Ansible using pip

Now since we have pip installed and running lets install Ansible with that.

root@lco-linux-master:~# pip install ansible

You will get a deprecation warning while installing Ansible with Python version 2.7:

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021.

On successful installation of Ansible verify the binary location and the installed version:

root@lco-linux-master:~# which ansible
/usr/local/bin/ansible
root@lco-linux-master:~# /usr/local/bin/ansible --version
ansible 2.10.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0]

Uninstalling Ansible using Python pip

root@lco-linux-master:~# python2.7 -m pip uninstall ansible

It will produce a very scary output of removing so many things but you no need to worry, all will be related to Ansible installation only. You might see

Installing Ansible within Python virtual environment

Install python virtual environment on Ubuntu:
root@lco-linux-master:~# apt install python-virtualenv
Setting up virtual environment with Python2:
root@lco-linux-master:~# virtualenv myansible
Running virtualenv with interpreter /usr/bin/python2
New python executable in /root/myansible/bin/python2
Also creating executable in /root/myansible/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
Activate virtual environments and verify the Python version:
root@lco-linux-master:~# source myansible/bin/activate
(myansible) root@lco-linux-master:~# python --version
Python 2.7.17
Install Ansible using pip now:
(myansible) root@lco-linux-master:~# pip install ansible
Verify the version on successful installation:
(myansible) root@lco-linux-master:~# ansible --version
/root/myansible/local/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
  from cryptography.exceptions import InvalidSignature
ansible 2.10.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/myansible/local/lib/python2.7/site-packages/ansible
  executable location = /root/myansible/bin/ansible
  python version = 2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0]
Deactivate the virtual environment
(myansible) root@lco-linux-master:~# deactivate
root@lco-linux-master:~#
Setting up virtual environment with Python3:
root@lco-linux-master:~# virtualenv myansible-py3 --python=python3
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /root/myansible-py3/bin/python3
Also creating executable in /root/myansible-py3/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
Activate virtual environments and verify the Python version:
root@lco-linux-master:~# source myansible-py3/bin/activate
(myansible-py3) root@lco-linux-master:~#

(myansible-py3) root@lco-linux-master:~# python --version
Python 3.6.9
Install Ansible using pip now:
(myansible-py3) root@lco-linux-master:~# pip install ansible
Verify the version on successful installation:
(myansible-py3) root@lco-linux-master:~# ansible --version
ansible 2.10.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/myansible-py3/lib/python3.6/site-packages/ansible
  executable location = /root/myansible-py3/bin/ansible
  python version = 3.6.9 (default, Oct  8 2020, 12:12:24) [GCC 8.4.0]
Deactivate the virtual environment
(myansible-py3) root@lco-linux-master:~# deactivate
root@lco-linux-master:~#
Install python virtual environment on CentOS:
[root@lco-ansible-master ~]# yum install python-virtualenv

Apart from the above step all other steps will remain same as above part of Ubuntu virtual env installations.

That's all for this tutorial of Deploying Ansible on different Linux Operating Systems using various ways.

Hope you like the article. Stay Tuned and don't forget to subscribe for more.

Thank you. Happy learning!

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!