Home » Ansible Pip

Ansible Pip

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 the pip module.

  • The pip package should already be installed on the remote server.
  • Virtualenv package should be installed on the remote server already if you need to manage the packages in the python virtual environment.

NOTE: If you get the error “unable to find any of pip2, pip to use. Pip needs to be installed”. The pip module is not available on the remote server during the execution.

Installing a Pip Module

To install a new python library, you need to set the name of the package against the “name” parameter. By default, the “state” parameter is “present”, the module will try to install the library.

If the library is already installed, then nothing will be done. And if a new version of the library exists, it will not be upgraded.

Installing Using a Requirement 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, give it as input to the “requirements” parameters.

Also, you can use the copy module beforehand to copy the requirements file to every remote server. In the following code, install the requirements file in the location/tmp/req.txt.

Installing Multiple Python Libraries

To install the multiple packages, set all the libraries against the “name” parameter, separated by a comma.

Installing a Particular Version of Pip Library

There is a “version” parameter, which can be used to install only the mentioned version of a library. In the following code, install the version of nltk library.

Reinstall a Python Library

You can reinstall the python library by using the “forcereinstall” value for the “state” parameter.

This will reinstall the latest version of the library. You can use the “version” parameter along with it. The following code will install the version 3.0.0 of the nltk library.

Removing a Python Library

You can delete a python library by changing the state to “absent”. In the following code, we will remove the “NumPy” and “SciPy” python libraries from the remote servers.

Next TopicAnsible vs Chef

You may also like