Ansible Pip Module – Managing Python Packages

Ansible pip module is used when you need to manage Python libraries on the remote servers.

There are two prerequisites if you need to use all the features in this module.
1 – The pip package should be installed on the remote server already. You can use the Ansible apt module or similar to install this as a part of a playbook if needed.
2 – Virtualenv package should already be installed on the remote servers if you need to manage the packages in the Python virtual environment.

If you get the error ‘Unable to find any of pip2, pip to use. pip needs to be installed.’ during execution, the pip module is not available on the remote server.

Installing a pip module in Ansible

For installing a new python library in Ansible, you just need to set the name of the package against the ‘name’ parameter. The ‘state’ parameter is by default ‘present’. So when you don’t set the ‘state’ parameter, the module will try to install the library.

If the library is already installed, then nothing will be done. But in this case, even if a new version of the library exists, it won’t be upgraded.

In the below example, I am installing the NumPy library.
- hosts: all
  tasks:
  - name: Installing NumPy python library using Ansible pip module
    pip:
      name: NumPy

output
------
mdtutorials2@system01:~$ sudo pip show numpy
Metadata-Version: 2.0
Name: numpy
Version: 1.13.3

Note: Installing the latest library – If you want to install the latest version of a particular python library, then you should set the state parameter to present.

Installing using a requirements file

Another way to install the libraries is via the requirements file. If you have any requirements file with all the libraries in the remote servers, then you can give it as input to the ‘requirements’ parameter.

You can also use the copy module beforehand to copy the requirements file to every remote server.

In the below task, I am trying to execute the requirements file in the location /tmp/req.txt.

- hosts: all
  tasks:
  - name: Installing python libraries using requirements file in Ansible
    pip:
      requirements: req.txt
      chdir: /tmp

req.txt
-------
nltk==3.0.0
numpy<2.0.0 scipy>=1.0.0

output
------
nltk - 3.0.0
numpy - 1.13.3
scipy - 1.0.0

Installing Multiple Python Libraries

You don’t need to have separate tasks for installing multiple packages. You can set all the libraries against the name parameter, separated by a comma.

- hosts: all
  tasks:
  - name: Installing multiple python packages in Ansible
    pip:
      name: NumPy,SciPy

output
------
mdtutorials2@system01:~$ sudo pip show scipy
Metadata-Version: 2.0
Name: scipy
Version: 1.0.0

Installing a particular version of a pip library

Sometimes, due to some dependencies, you might want to have only the mentioned version of a library to be installed. There is a version parameter, which can be used in such scenarios.

In the below task, I am trying to install the version ‘3.0.0’ of ‘nltk’ library(not the latest version).

- hosts: all
  tasks:
  - name: Installing required version of python library in Ansible
    pip:
      name: nltk
      version: '3.0.0'

Reinstalling a python library

In some scenarios, you might want to reinstall the python library every time. You can use the ‘forcereinstall’ value for the ‘state’ parameter.

Note that this will reinstall the latest version of the library and not the library that was already on the remote server. You can use the ‘version’ parameter along with it.

The following task will always install the version 3.0.0 of the nltk library.

- hosts: all
  tasks:
  - name: Reinstalling a python library in Ansible
    pip:
      name: nltk
      version: 3.0.0
      state: forcereinstall

Removing a Python library

You can remove a python library in a similar manner by just changing the state to ‘absent’. The following task will remove the ‘NumPy’ and ‘SciPy’ Python libraries from the remote servers.

- hosts: all
  tasks:
  - name: Removing Python libraries
    pip:
      name: NumPy,SciPy
      state: absent