auto-upgrade pip in Ansible role/task


Lets say you have an Ansible role in which you create a virtual-environment using Python 3.x:

- name: Install python venv and requirements
  environment:
    http_proxy: http://proxy.domain.com:8080
    https_proxy: http://proxy.domain.com:8080
    HTTP_PROXY: http://proxy.domain.com:8080
    HTTPS_PROXY: http://proxy.domain.com:8080
  pip:
    extra_args: '--trusted-host pypi.python.org'
    requirements: /path/to/requirements.txt
    virtualenv: /path/to/venv
    virtualenv_command: /opt/python-3.7.0/bin/python3.7 -m venv

If your requirements.txt only contains the modules you really need for your app, you end up with an old pip version (10.x). And then, when you do a pip freeze, you get this ugly message:

You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Not really nice, uuhh ? The fix is simple: add the required pip release to your requirements.txt, e.g.

# requirements.txt
...
pip>=18.1
...

Fixed !

And if you wonder why I made my pip module config so complex… it’s because Ansible insist to use pip2, which is not really appropriate for Python 3.x venv’s. This is the case with Ansible 2.6.2.