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!
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 (
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 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
# Switch the working directory
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
- name: Install bottle using the 'pip3.3' executable
- name: Install specified Python requirements
- name: Install bottle Python package on version 0.12
- name: Install multi python packages with version specifiers
- name: Install bottle, forcing reinstallation if it's already installed
- name: Install bottle module within a user home directory
- name: Install specified python requirements in indicated (virtualenv)
- name: Install bottle while ensuring the umask is 0022 (so other users can use it)
3. Run the ansible-playbook command below to execute the pip.yml playbook.
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.
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.
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.
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?