Nonetheless, there is still a gap to be filled between Python dependencies and Ansible. For this purpose, we have the pip module in Ansible, short for Preferred Installer Program. Here we will go into everything you need to know about the Ansible pip module.
What Is the Pip Module?
If you have a remote server set up with Ansible, you will probably need to manage its Python dependencies. The Ansible pip module bridges the gap between you and the Python packages installed/to be installed on your servers.
The pip module is a part of the “ansible-core.” This means that all Ansible installations have the pip module ready to go, so you won’t have to go through the headache of installing it.
This module handles everything, from installing dependencies to changing their versions or removing them altogether. However, before we get into how you can use this amazing utility, some prerequisites must be met.
Pip Module Requirements
There are two main requirements that must be met for the pip module to work properly.
Your target package/dependency must already be present on the remote server you manage.
If you want to use the Python virtual environment, the “virtualenv” package must be preinstalled on the remote server.
If you don’t have these two prerequisites already cleared, go ahead and make sure your system ticks these checkboxes. Once done, we can move on to using the Ansible pip module!
Using the Ansible Pip Module
There are a number of things you can achieve with this module. However, first, you’d need to learn how to use it, which parameters are required, and which arguments need to be passed. Let’s look at some of the most crucial parameters.
chdir – Changes the current directory before execution.
editable – Takes yes or no (default), passes the editable flag.
extra_args – Allows additional pip arguments.
name – The name/URL of the library or dependency that you want to install (the argument can be a string or a list)
requirements – Takes the path to a “requirements” file that is present on the remote server you’re accessing (works with chdir)
state – Defines the module’s state and takes absent or forceinstall or latest or present (default).
version – Specifies the version of the package in the name parameter.
umask – Takes an octal string and specifies the umask applied before installation.
Once you get the hang of using these parameters and what must be passed to them, using the Ansible pip module becomes a piece of cake. Finally, given below are some equally important attributes.
check_mode – Check status prediction without modifying the target.
platform – Supported target operating systems.
With that done, you’re well on your way to using the pip module to install and work with Python libraries on your remote systems!
How to Install a Python Package?
Let’s start with installing a Python package on a remote server. We will use the parameters we just discussed and see how different argument values affect the installation.
We will install the PyTorch package on all hosts by passing all arguments to the hosts parameter. Moreover, the name of the pip module in pytorch.
tasks:
- name: Installing the PyTorch package using the Ansible pip module
pip:
name: pytorch
Once you’re done with that, you can run the playbook by typing this into the Linux shell:
ansible-playbook testbook.yml
You can also specify the exact version of the package you want by simply changing the name parameter as follows.
pip:
name: pytorch==1.9.1
Remote protocols such as git+ or hg+ can also be used to source Python packages. Here’s how you could install a package from a Github repository.
pip:
name: git+http://myrepository/folder/myPackage
Moving on, there’s an option to install dependencies in certain virtual environments as well. Here’s an example of how that would be done.
pip:
name: pytorch
virtualenv: /mypytorch/venv
virtualenv_site_packages: yes
In many cases, you may come across text files that contain all the requirements of some application you want to run.
pip:
requirements: /folder/requirements.txt
Similarly, the same could be done for a virtual environment as well. Given below is an example of how to install Python packages from a requirements file in a particular virtual environment.
pip:
requirements: /folder/requirements.txt
virtualenv: /folder/venv
With that said, you should be well equipped to install Python packages using the Ansible pip module. Let’s move on to some other important commands.
How to Remove/Reinstall a Python Package?
In an earlier section of this guide, we learned that the state parameter is crucial to any operator we want to apply to a package underuse. For instance, if you want to remove a Python package, you can explicitly set the state parameter from its default value (present) to absent. Here’s how that can be done.
tasks:
- name: Removing Python packages
pip:
name: pytorch
state: absent
Continuing down the same path, we can reinstall a Python library by changing the state parameter’s value to forceinstall.
tasks:
- name: Reinstalling a library
pip:
name: pytorch
version: 1.9.1
state: forcereinstall
You now know how to not only install Python packages using the Ansible pip module but also how to remove or reinstall them!
Conclusion
In this guide, we looked at the Ansible pip module – what it is, what it does, and how to use it. It is a handy utility that allows you to manage Python dependencies on remote servers. Hopefully, you are now ready to manage your libraries efficiently!