---
title: "How to Manage Python Libraries with Ansible Pip"
description: "Learn how to effectively manage Python libraries with the Ansible Pip module and take control of your Python dependencies!"
canonical: "https://adamtheautomator.com/ansible-pip/"
---

# How to Manage Python Libraries with Ansible Pip

> Learn how to effectively manage Python libraries with the Ansible Pip module and take control of your Python dependencies!

Source: https://adamtheautomator.com/ansible-pip/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Manage Python Libraries with Ansible Pip](https://adamtheautomator.com/wp-content/uploads/2022/10/How-to-Manage-Python-Libraries-with-Ansible-Pip.jpg)

# How to Manage Python Libraries with Ansible Pip

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)18 October 20223 min. read

Categories: [DevOps](/category/devops/)

Tags:[Ansible](/tag/ansible/)[Python](/tag/python/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Importing Python Modules Using Ansible PIP](#importing-python-modules-using-ansible-pip)
*   [Installing the Ansible Python pip Module within a Playbook](#installing-the-ansible-python-pip-module-within-a-playbook)
*   [Verifying the Modules of Python Libraries](#verifying-the-modules-of-python-libraries)
*   [Conclusion](#conclusion)

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 [Ansible controller host](https://docs.ansible.com/ansible/latest/installation_guide/index.html) – This tutorial uses Ansible v2.11.7 on an [Ubuntu](https://adamtheautomator.com/install-ansible/) 20.04.3 LTS machine.

Related:[How to Setup Ansible (Ubuntu, RHEL, CentOS, macOS)](https://adamtheautomator.com/install-ansible/)

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   An [inventory file](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html) and one or more hosts are configured to run Ansible commands and playbooks. The remote Linux computer is the [localhost](http://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.
    

Related:[How Do You Install Python 3.6?](https://adamtheautomator.com/install-python-36/)

*   [Python modules boto3](https://zoomadmin.com/HowToInstall/UbuntuPackage/python-boto3) greater than 1.15.0 and [botocore](https://docs.aws.amazon.com/efs/latest/ug/install-botocore.html) 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](https://docs.python.org/3/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`).

```powershell
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](https://adamtheautomator.com/wp-content/uploads/2022/10/image-288.png)

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.

Related:[How to Build (And Actually Understand) a Solid Ansible Playbook](https://adamtheautomator.com/ansible-playbook-example/)

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](https://www.redhat.com/en/topics/automation/what-is-an-ansible-playbook#:~:text=An%20Ansible%C2%AE%20playbook%20is,make%20up%20an%20Ansible%20inventory.) and all required configuration files you’ll use to invoke the Ansible pip module.

```bash
# 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._

Related:[How to Create a Django Docker Application Container](https://adamtheautomator.com/django-docker/)

```yaml
---
- 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](https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html) command below to execute the _pip.yml_ playbook.

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/10/image-289.png)

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](https://pip.pypa.io/en/stable/cli/pip_show/) command to verify the installed bottle modules.

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/10/image-290.png)

Verifying the Python-pip bottle module

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

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/10/image-291.png)

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?

Related:[How to Work with Ansible When and Other Conditionals](https://adamtheautomator.com/ansible-when/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fansible-pip%2F&text=How%20to%20Manage%20Python%20Libraries%20with%20Ansible%20Pip)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fansible-pip%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fansible-pip%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2021/04/A-Step-by-Step-Guide-to-Getting-Started-with-Ansible-on-Windows.jpg)

### [Mastering Ansible on Windows: Your Go-To Expert Guide](/ansible-on-windows/)

Ansible on Windows made simple. A complete guide to hassle-free installation and configuration, perfect for users seeking quick and effective mastery.

![](https://adamtheautomator.com/wp-content/uploads/2023/01/google-cloud-storage-python.jpg)

### [How to Install & Use the Google Cloud Storage Python Client](/google-cloud-storage-python-client/)

Discover how to use the Google Cloud Storage Python client in your projects and make your DevOps pipelines work even better!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
