An Ansible role for installing the latest pip (the python package installer) to the system.
By default, this role ensures that pip’s version always matches the latest version
that work’s with version of the given python executable
(something which just pip --upgrade pip
'ing for whatever reason doesn’t do).
This role can also be used to install a given set of pip packages to either the system or in a virtualenv.
Below you can find information on…
-
the role’s required Ansible version
-
the role’s supported platforms
-
the role’s role dependencies
---
galaxy_info:
role_name: "pip"
description: "An ansible role for installing the latest pip (python package installer) to the system."
author: "jonaspammer"
license: "MIT"
min_ansible_version: "2.11"
platforms:
# note: text after "actively tested: " represent the docker image name
- name: EL # (Enterprise Linux)
versions:
- "9" # actively tested: rockylinux9
- name: Fedora
versions:
- "38" # actively tested: fedora38
- "39" # actively tested: fedora39
- name: Debian
versions:
- bullseye # actively tested: debian11
- bookworm # actively tested: debian12
- name: Ubuntu
versions:
- focal # actively tested: ubuntu2004
- jammy # actively tested: ubuntu2204
galaxy_tags: []
dependencies: []
The Ansible User needs to be able to become
.
The community.general
collection
must be installed on the Ansible controller.
pip_version: [latest possible version which supports the python version of behind `pip_python_executable`]
The version of pip to install using pip after pip was installed in form of a pip requirement specifier.
Version behind pip_python_executable |
Default pip_version |
---|---|
no match |
>=22 |
3.6 |
<22 |
3.5 or 2 (.7 assumed) |
<21 |
3.4 |
<19.2 |
3.3 |
<18 |
Tip
|
Checkout pip’s pypi release history
for available Also, if interested, checkout the pip documentation
for a human-generated change log of the changes to the |
pip_state: "forcereinstall"
State to pass to pip install pip
.
One of: forcereinstall
, latest
, present
.
If forcereinstall
is passed this role tries to keep idemponency
by disabling changed_when
on the install and instead executing
another task that compares the before/after result of pip --version
.
If latest
is passed pip_version
has no effect.
pip_package: "python3-pip"
The name of the package(s) to install to get pip
on the system.
For older systems that don’t have Python 3 available, you can set this to python-pip
.
pip_virtualenv_packages: [OS-specific by default, see /defaults directory]
The name of the package(s) to install to get virtualenv
on the system.
pip_executable: "{{ 'pip3' if pip_package.startswith('python3') else 'pip' }}"
The pip_executable
passed to the ansible.builtin.pip
modules issued by this role.
The role will try to autodetect the pip executable based on the pip_package
.
You may also override this explicitly, e.g. pip_executable: pip3.6
,
in case .
pip_install_packages: []
A list of packages to install with pip.
Each entry may either be a simple string (shorthand for - name: …
)
or an own object with below properties:
- chdir
-
cd
into this directory before running the command. - name
-
The name of a Python library to install or the url(bzr+,hg+,git+,svn+) of the remote package.
- version
-
The version number to install of the Python library specified in the name parameter.
- requirements
-
Instead of using
name
andversion
to define a single package inline you may also use this option to reference to a path of a pip requirements file, which should be local to the remote system. File can be specified as a relative path if using thechdir
option. - state
-
The state of the pip module (i.e. absent / forcereinstall / latest / present)
- umask
-
Defaults to
pip_install_packages_umask
if exists.The system umask to apply before installing the pip package. This is useful, for example, when installing on systems that have a very restrictive umask by default (e.g., "0077") and you want to pip install packages which are to be used by all users. Note that this requires you to specify desired umask mode as an octal string, (e.g., "0022").
- virtualenv
-
Defaults to
pip_install_packages_virtualenv
if exists.Path to a virtualenv directory to install into. If the virtualenv does not exist, it will be created before installing packages. The optional
virtualenv_command
, andvirtualenv_python
options affect the creation of the virtualenv. - virtualenv_command
-
Defaults to
pip_install_packages_virtualenv_command
if exists.The command or a pathname to the command to create the virtual environment with. For example
pyvenv
,virtualenv
,virtualenv2
,~/bin/virtualenv
,/usr/local/bin/virtualenv
. - virtualenv_python
-
Defaults to
pip_install_packages_virtualenv_python
if exists.The Python executable used for creating the virtual environment. For example python3.5, python2.7. When not specified, the Python version used to run the ansible module is used. This parameter should not be used when
virtualenv_command
is usingpyvenv
or the-m venv
module. - virtualenv_site_packages
-
Defaults to
pip_install_packages_virtualenv_python
if exists.Whether the virtual environment will inherit packages from the global site-packages directory. Note that if this setting is changed on an already existing virtual environment it will not have any effect - the environment must be deleted and newly created.
- extra_args
-
Defaults to
pip_install_packages_extra_args
if exists.Extra arguments passed to pip.
- environment
-
Defaults to
pip_install_packages_environment
if exists.Environment Variables passed to the pip module.
pip_python_executable: "{{ 'python3' if pip_package.startswith('python3') else 'python' }}"
This variable is being used to determine the default value of pip_version
.
The role will try to autodetect the python executable based on the pip_package
.
Each variable listed in this section
is dynamically defined when executing this role (and can only be overwritten using ansible.builtin.set_facts
) and
is meant to be used not just internally.
Tasks are tagged with the following tags:
Tag | Purpose |
---|---|
This role does not have officially documented tags yet. |
You can use Ansible to skip tasks, or only run certain tasks by using these tags. By default, all tasks are run when no tags are specified.
Note
|
This role is part of many compatible purpose-specific roles of mine. The machine needs to be prepared.
In CI, this is done in ---
- name: prepare
hosts: all
become: true
gather_facts: false
roles:
- role: jonaspammer.bootstrap
# - role: jonaspammer.core_dependencies The following diagram is a compilation of the "soft dependencies" of this role as well as the recursive tree of their soft dependencies. |
---
roles:
- role: jonaspammer.pip
---
roles:
- role: jonaspammer.pip
vars:
- pip_package: "python-pip"
---
roles:
- role: jonaspammer.pip
vars:
pip_install_packages:
# Specify names and versions.
- name: docker
version: "1.2.3"
- name: awscli
version: "1.11.91"
# Or specify bare packages to get the latest release (on first install).
- docker
- awscli
# Or uninstall a package.
- name: docker
state: absent
# Or update a package to the latest version.
- name: docker
state: latest
# Or force a reinstall.
- name: docker
state: forcereinstall
A role may work on different distributions, like Red Hat Enterprise Linux (RHEL), even though there is no test for this exact distribution.
OS Family | Distribution | Distribution Release Date | Distribution End of Life | Accompanying Docker Image |
---|---|---|---|---|
Rocky |
Rocky Linux 8 (RHEL/CentOS 8 in disguise) |
2021-06 |
2029-05 |
|
Rocky |
Rocky Linux 9 |
2022-07 |
2032-05 |
|
RedHat |
Fedora 39 |
2023-11 |
2024-12 |
|
Debian |
Ubuntu 20.04 LTS |
2021-04 |
2025-04 |
|
Debian |
Ubuntu 22.04 LTS |
2022-04 |
2027-04 |
|
Debian |
Debian 11 |
2021-08 |
2024-06 (2026-06 LTS) |
|
Debian |
Debian 12 |
2023-06 |
2026-06 (2028-06 LTS) |
The tested ansible versions try to stay equivalent with the
support pattern of Ansible’s community.general
collection.
As of writing this is:
-
2.13 (Ansible 6)
-
2.14 (Ansible 7)
-
2.15 (Ansible 8)
-
2.16 (Ansible 9)
Development Dependencies are defined in a
pip requirements file
named requirements-dev.txt
.
Example Installation Instructions for Linux are shown below:
# "optional": create a python virtualenv and activate it for the current shell session $ python3 -m venv venv $ source venv/bin/activate $ python3 -m pip install -r requirements-dev.txt
Please take a look at my Ansible Role Development Guidelines.
If interested, I’ve also written down some General Ansible Role Development (Best) Practices.
Versions are defined using Tags, which in turn are recognized and used by Ansible Galaxy.
Versions must not start with v
.
When a new tag is pushed,
a GitHub CI workflow
()
takes care of importing the role to my Ansible Galaxy Account.
Automatic Tests are run on each Contribution using GitHub Workflows.
The Tests primarily resolve around running Molecule on a varying set of linux distributions and using various ansible versions.
The molecule test also includes a step which lints all ansible playbooks using
ansible-lint
to check for best practices and behaviour that could potentially be improved.
To run the tests, simply run tox
on the command line.
You can pass an optional environment variable to define the distribution of the
Docker container that will be spun up by molecule:
$ MOLECULE_DISTRO=ubuntu2204 tox
For a list of possible values fed to MOLECULE_DISTRO
,
take a look at the matrix defined in .github/workflows/ci.yml.
-
Run your molecule tests with the option
MOLECULE_DESTROY=never
, e.g.:$ MOLECULE_DESTROY=never MOLECULE_DISTRO=ubuntu1604 tox -e py3-ansible-5 ... TASK [ansible-role-pip : (redacted).] ************************ failed: [instance-py3-ansible-9] => changed=false ... ___________________________________ summary ____________________________________ pre-commit: commands succeeded ERROR: py3-ansible-9: commands failed
-
Find out the name of the molecule-provisioned docker container:
$ docker ps 30e9b8d59cdf geerlingguy/docker-debian12-ansible:latest "/lib/systemd/systemd" 8 minutes ago Up 8 minutes instance-py3-ansible-9
-
Get into a bash Shell of the container, and do your debugging:
$ docker exec -it 30e9b8d59cdf /bin/bash root@instance-py3-ansible-2:/#
TipIf the failure you try to debug is part of your
verify.yml
step and not the actualconverge.yml
, you may want to know that the output of ansible’s modules (vars
), hosts (hostvars
) and environment variables have been stored into files on both the provisioner and inside the docker machine under: */var/tmp/vars.yml
(contains host variables under thehostvars
key) */var/tmp/environment.yml
grep
,cat
or transfer these as you wish! -
After you finished your debugging, exit it and destroy the container:
root@instance-py3-ansible-2:/# exit $ docker stop 30e9b8d59cdf $ docker container rm 30e9b8d59cdf or $ docker container prune
Although a standard feature in tox 3, this now only happens when tox recognizes the presence of a CI variable. For example:
$ CI=true tox
This Project offers a definition for a "1-Click Containerized Development Environment".
This Container even enables one to run docker containers inside of it (Docker-In-Docker, dind), allowing for molecule execution.
To use it:
-
Ensure you fullfill the the System requirements of Visual Studio Code Development Containers, optionally following the Installation-Section of the linked page section.
This includes: Installing Docker, Installing Visual Studio Code itself, and Installing the necessary Extension. -
Clone the project to your machine
-
Open the folder of the repo in Visual Studio Code (File - Open Folder…).
-
If you get a prompt at the lower right corner informing you about the presence of the devcontainer definition, you can press the accompanying button to enter it. Otherwise, you can also execute the Visual Studio Command
Remote-Containers: Open Folder in Container
yourself (View - Command Palette → type in the mentioned command).
Tip
|
I recommend using |
Note
|
You may need to configure your host system to enable the container to use your SSH/GPG Keys. The procedure is described in the official devcontainer docs under "Sharing Git credentials with your container". |
This Project shall be kept in sync with the CookieCutter it was originally templated from using cruft (if possible) or manual alteration (if needed) to the best extend possible.
General Linting and Styling Conventions are
automatically held up to Standards
by various pre-commit
hooks, at least to some extend.
Automatic Execution of pre-commit is done on each Contribution using
pre-commit.ci
*.
Pull Requests even automatically get fixed by the same tool,
at least by hooks that automatically alter files.
Note
|
Not to confuse: Although some pre-commit hooks may be able to warn you about script-analyzed flaws in syntax or even code to some extend (for which reason pre-commit’s hooks are part of the test suite), pre-commit itself does not run any real Test Suites. For Information on Testing, see 🧪 Testing. |
Tip
|
Nevertheless, I recommend you to integrate pre-commit into your local development workflow yourself. This can be done by cd’ing into the directory of your cloned project and running You can also, for example, execute pre-commit’s hooks at any time by running |
The following sections are generic in nature and are used to help new contributors. The actual "Development Documentation" of this project is found under 📝 Development.
First off, thank you for considering contributing to this Project.
Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return, they should reciprocate that respect in addressing your issue, assessing changes, and helping you finalize your pull requests.
This Project owns many of its files to the CookieCutter it was originally templated from.
Please check if the edit you have in mind is actually applicable to the template and if so make an appropriate change there instead. Your change may also be applicable partly to the template as well as partly to something specific to this project, in which case you would be creating multiple PRs.
A casual contributor does not have to worry about following the spec by definition, as pull requests are being squash merged into one commit in the project. Only core contributors, i.e. those with rights to push to this project’s branches, must follow it (e.g. to allow for automatic version determination and changelog generation to work).
Contributions are made to this repo via Issues and Pull Requests (PRs). A few general guidelines that cover both:
-
Search for existing Issues and PRs before creating your own.
-
If you’ve never contributed before, see the first timer’s guide on Auth0’s blog for resources and tips on how to get started.
Issues should be used to report problems, request a new feature, or to discuss potential changes before a PR is created. When you create a new Issue, a template will be loaded that will guide you through collecting and providing the information we need to investigate.
If you find an Issue that addresses the problem you’re having, please add your own reproduction information to the existing issue rather than creating a new one. Adding a reaction can also help be indicating to our maintainers that a particular problem is affecting more than just the reporter.
PRs to this Project are always welcome and can be a quick way to get your fix or improvement slated for the next release. In general, PRs should:
-
Only fix/add the functionality in question OR address wide-spread whitespace/style issues, not both.
-
Add unit or integration tests for fixed or changed functionality (if a test suite already exists).
-
Address a single concern
-
Include documentation in the repo
-
Be accompanied by a complete Pull Request template (loaded automatically when a PR is created).
For changes that address core functionality or would require breaking changes (e.g. a major release), it’s best to open an Issue to discuss your proposal first.
In general, we follow the "fork-and-pull" Git workflow
-
Fork the repository to your own Github account
-
Clone the project to your machine
-
Create a branch locally with a succinct but descriptive name
-
Commit changes to the branch
-
Following any formatting and testing guidelines specific to this repo
-
Push changes to your fork
-
Open a PR in our repository and follow the PR template so that we can efficiently review the changes.
Please refer to the Release Page of this Repository for a human changelog of the corresponding Tags (Versions) of this Project.
Note that this Project adheres to Semantic Versioning. Please report any accidental breaking changes of a minor version update.
MIT License Copyright (c) 2022, Jonas Pammer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.