Bootstrap FreeKB - Ansible - Install Ansible on Linux using pip
Ansible - Install Ansible on Linux using pip

Updated:   |  Ansible articles

This article describes how to install Ansible but not Ansible Tower. Refer to Install Ansible Tower on Linux if you also want Tower.

The system that Ansible is installed on is known as the control node. Windows is not supported for the control node. Instead, Ansible must be installed on a Linux distribution.


Python

The control node must have Python version 2.7 or higher or Python version 3.5 or higher installed. The python --version command can be used to determine if Python is installed and the version that is installed. If Python is not installed, the installation of Ansible will also install Python.

~]$ python --version
Python 3.6.8

 


EPEL (Extra Packages for Enterprise Linux)

On a Red Hat distribution, PIP is in the EPEL(Extra Packages for Enterprise Linux) repository, thus epel-release must be installed.

yum install epel-release

 


Install

On a Red Hat distribution, the yum install ansible command can be used to install Ansible. However, be aware that this will setup Ansible to use Python version 2.7.5. Python 2.7 reached it's end of life support in January of 2020. For this reason, it is preferrable to use PIP to install Ansible. The pip --version can be used to determine if PIP is installed. If PIP is installed, something like this should be displayed. If PIP is not installed, refer to Installing PIP on Linux CentOS.

AVOID TROUBLE

There are 3 versions of pip

  • pip is used for Python version 2.6 and below
  • pip2 is used for Python version 2.7 and above
  • pip3 is used for Python version 3

For example, the /usr/bin/pip command (or just pip) would be used for Python version 2.6 and below.

~]$ pip --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

 

The hash command caches the paths to commands, and if a command is relocated then bash will not pick it up unless that cache is cleared. Issue the following command to clear Ansible from the cache.

hash -d ansible

 

You may also want to purge the pip cache to avoid used cached files.

pip cache purge

 

Something like this should be returned.

Files removed: 2

 

If the wheel package is not installed, something like this will be returned. Ansible will still be installed.

Using legacy 'setup.py install' for ansible, since package 'wheel' is not installed.

 

However, so as to avoid using legacy setup.py, let's first use pip install to install the wheel package.

pip install wheel

 

Then install the latest stable version of Ansible.

pip install ansible-core
pip install ansible

 

Or, a specific version of ansible can be installed.

pip install ansible-core==2.14.0
pip install ansible==3.2.0

 

Or, better yet, use a requirements.txt file.

ansible-core==2.14.0
ansible==3.2.0
wheel==0.37.1

 

And then install the packages using the requirements.txt file.

pip install --requirement requirements.txt

 

When Ansible version 2.10 was release, ansible-base was renamed to ansible-core. If you need to install Ansible version 2.9 or lower, you'll use ansible-base instead of ansible-core. Also install the packaging package, which is used by ansible base.

pip install packaging
pip install ansible-base==2.9.0
pip install ansible==2.9.0

 

If the version of ansible-core is not available, you may need to download the .whl file for the version of Ansible you want to install.

pip install ansible_core-2.14.0-py3-none-any.whl

 

This is required, because if the packaging package is not installed, something like this will probably get returned when using the ansible-playbook command.

ERROR! Unexpected Exception, this is probably a bug: The 'packaging' distribution was not found and is required by ansible-base

 

The pip show command can be used to ensure that ansible-core was installed.

~]# pip show ansible-core
Name: ansible-core
Version: 2.14.0
Summary: Radically simple IT automation
Home-page: https://ansible.com/
Author: Ansible, Inc.
Author-email: info@ansible.com
License: GPLv3+
Location: /usr/local/lib/python3.6/site-packages
Requires: packaging, jinja2, PyYAML, cryptography
Required-by: ansible

 

And Ansible.

~]# pip show ansible
Name: ansible
Version: 3.0.0
Summary: Radically simple IT automation
Home-page: https://ansible.com/
Author: Ansible, Inc.
Author-email: info@ansible.com
License: GPLv3+
Location: /usr/local/lib/python3.6/site-packages
Requires: jinja2, PyYAML, cryptography

 

Note that if you include the --user flag when installing Ansible (pip install --user ansible), then the location of Ansible will be something like this.

Location: /root/.local/lib/python3.6/site-packages


Note that Ansible does not create a daemon, thus the systemctl command will not be used. Instead, Ansible uses SSH to manage remote systems (or a different networking protocol could be used). When installing Ansible using pip (or from source), the config file should return None. In this scenario, refer to Ansible - Configuration file.

ansible 2.9.12
  config file = None
  configured module search path = ['/home/jeremy.canfield/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Apr  2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

 

There are a few different ways to tell Ansible to use Python version 2 or Python version 3.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 23fcc6 in the box below so that we can be sure you are a human.