How to Manage Python Libraries with Ansible Pip

Published:18 October 2022 - 4 min. read

If you’re tired of manually managing Python libraries and dependencies, let the Ansible pip module do the dirty work. The Ansible pip module allows you to automate the Python library and dependencies management.

In this tutorial, you’ll learn how Ansible can manage Python library dependencies within an Ansible playbook.

Read on and save yourself the stress of manual Python library management!

Prerequisites

This tutorial comprises step-by-step instructions. If you’d like to follow along, be sure you have the following in place:

  • An inventory file and one or more hosts are configured to run Ansible commands and playbooks. The remote Linux computer is the localhost itself, and this tutorial uses an inventory group called localhost.
  • Python v3.6 or later installed on your Ansible controller host and the remote node machine. This tutorial uses Python v3.8.10 on an Ubuntu machine.
  • Python modules boto3 greater than 1.15.0 and botocore greater than 1.18.0 should be installed on the Ansible controller host and the remote node machine.

Importing Python Modules Using Ansible PIP

Python standard library contains several well-designed Python modules for convenient reuse, like representing data, processing data, interacting with operating systems and filesystems, and web programming. Python modules are Python Programs like a file (abc.py) that are imported.

If you plan to install a single package or import a single Python module, running ad hoc commands will suffice. Ad hoc commands are a quick and efficient way to run a single command to install a package, update the repository, or any Python configuration on remote machines.

Log onto your Ansible controller and run the below ansible command to connect (-m ansible.builtin.pip) to the localhost (i.e., your machine).

The command below passes an argument (-a) that tells Ansible to install a bottle module in your Ubuntu machine regardless if the module is already installed (state=forcereinstall).

ansible localhost -m ansible.builtin.pip -a "name=bottle state=forcereinstall"

Once the command completes, you’ll see a CHANGED message, as shown below, that confirms Ansible successfully installed the software.

Installing a bottle module using an ad hoc command
Installing a bottle module using an ad hoc command

Installing the Ansible Python pip Module within a Playbook

Executing an Ansible ad hoc command is great for a one-off action. But perhaps you need to perform multiple tasks. If so, create an Ansible playbook with numerous tasks that install multiple dependencies and Python software.

1. Open the terminal in your Ansible controller host, and run the following commands to create a directory called ~/ansible_python_pip_module and switch to that directory.

This directory will contain the playbook and all required configuration files you’ll use to invoke the Ansible pip module.

# Create directory
mkdir ~/ansible_python_pip_module
# Switch the working directory
cd ~/ansible_python_pip_module

2. Next, create a file called pip.yml to open your favorite text editor in the ~/ansible_python_pip_module directory.

Populate the pip.yml file with the following YAML playbook contents. The playbook below contains three multiple tasks to install the Ansible PIP modules properly.

The virtual environments (i.e., virtualenv) are light weighted as they contain directories isolated from system directories. They also have their own Python binary, an independent set of installed Python packages.

---
- name: Ansible Python pip module demo
  hosts: localhost
  tasks:

   - name: Install bottle using the 'pip3.3' executable
     ansible.builtin.pip:
       name: bottle
       executable: pip

   - name: Install specified Python requirements
     ansible.builtin.pip:
        requirements: /my_app/requirements.txt

   - name: Install bottle Python package on version 0.12
     ansible.builtin.pip:
       name: bottle==0.12.23

   - name: Install multi python packages with version specifiers
     ansible.builtin.pip:
       name:
         - django>1.11.0,<1.12.0

   - name: Install bottle, forcing reinstallation if it's already installed
     ansible.builtin.pip:
       name: bottle
       state: forcereinstall

   - name: Install bottle module within a user home directory
     ansible.builtin.pip:
       name: bottle
       extra_args: --user

   - name: Install specified python requirements in indicated (virtualenv)
     ansible.builtin.pip:
       requirements: /my_app/requirements.txt
       virtualenv: /my_app

   - name: Install bottle while ensuring the umask is 0022 (so other users can use it)
     ansible.builtin.pip:
       name: bottle
       umask: "0022"
     become: True

3. Run the ansible-playbook command below to execute the pip.yml playbook.

ansible-playbook pip.yml

Below, you can see that some tasks show a changed status, which indicates Ansible installed the pip module successfully and modified the task’s state to run the command. In contrast, you see an ok status since some tasks don’t require changes.

Executing the Ansible Playbook in Ubuntu Machine
Executing the Ansible Playbook in Ubuntu Machine

Verifying the Modules of Python Libraries

You’ve successfully installed modules using Ansible pip within a playbook. But verifying the modules is a must to be on the safe side.

Run the following pip show command to verify the installed bottle modules.

pip show bottle

As you can see below, the below output shows the installed bottle modules along with the version and all other vital details.

Verifying the Python-pip bottle module
Verifying the Python-pip bottle module

Now, run the below command to verify the Django package is installed.

pip show django

Like in the previous step, the output below shows the details of the Django package. This output confirms that you’ve successfully installed the Django package.

Verifying the Python-pip Django package
Verifying the Python-pip Django package

Conclusion

Whether you’re just checking out Python modules or installing multiple modules, the Ansible pip module is one to consider. And throughout this tutorial, you’ve learned how to use the Ansible pip module in a playbook to install and manage modules with conditions.

The Ansible pip module is a quick way to effectively work with Python libraries and modules on remote hosts. And with this newfound knowledge, why not leverage Ansible conditions with Ansible services modules in your subsequent deployments?

Or perhaps add more control over your playbooks with Ansible when and other conditionals?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!