如何使用ansible正确升级pip?

How to correctly upgrade pip using ansible?

目标与环境

我正在对 Ubuntu 16.04 使用 ansible。 最终目标是使用 mongodb_user 模块。 这需要 pymongo,所以这需要 python-pip

我在做什么

- name: Package prerequisites for pymongo ansible module
  apt:
    force_apt_get: yes
    name: ['python-pip', 'python-setuptools']
    install_recommends: no
    state: present
  become: true
  tags:
    - mongo
  register: output

- name: Upgrade pip to latest vesion
  pip:
    name: pip
    extra_args: --upgrade
  register: output

- debug:
    var: output    

问题

这是实际输出;请注意:

    "output": {
        "changed": true, 
        "cmd": [
            "/usr/bin/pip2", 
            "install", 
            "--upgrade", 
            "pip"
        ], 
        "failed": false, 
        "name": [
            "pip"
        ], 
        "requirements": null, 
        "state": "present", 
        "stderr": "You are using pip version 8.1.1, however version 18.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.\n", 
        "stderr_lines": [
            "You are using pip version 8.1.1, however version 18.1 is available.", 
            "You should consider upgrading via the 'pip install --upgrade pip' command."
        ], 
        "stdout": "Collecting pip\n  Using cached https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl\nInstalling collected packages: pip\nSuccessfully installed pip-8.1.1\n", 
        "stdout_lines": [
            "Collecting pip", 
            "  Using cached https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl", 
            "Installing collected packages: pip", 
            "Successfully installed pip-8.1.1"
        ], 
        "version": null, 
        "virtualenv": null
    }

奇怪的是,我从命令行得到了

$ /usr/bin/pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ /home/mirko/.local/bin/pip -V
pip 18.1 from /home/mirko/.local/lib/python2.7/site-packages/pip (python 2.7)

一试

我在安装 python-pip 后尝试 手动 升级 pip 并得到另一件奇怪的事情:pip 不想卸载旧的 pip...

sudo pip install pip --upgrade
[sudo] password for mirko: 
The directory '/home/mirko/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/mirko/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 1.2MB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr

问题

在 ansible 和 Ubuntu 16.04 中使用 pip 的正确方法是什么?

Must/Can 我强制 ansible 使用 "my" pip?

Must/Can 我卸载 "wrong" pip?

我是不是做错了什么造成了这个双版本问题?

实际上我解决了使用 ubuntu-特定的 pymongo 包

- name: Package prerequisites for pymongo ansible module
  apt:
    force_apt_get: yes
    name: ['python-pip', 'python-setuptools', 'python-virtualenv', 'python-pymongo']
    install_recommends: yes
    state: present
  become: true
  tags:
    - mongo

env: Ubuntu 18.04, Python 3.6.9, pip 最初在 9.0.1.

我最后做了以下事情:

    - name: pip self-update
      pip:
        name: pip
        state: latest

doc about pip module

我自己做了这个, 运行 pip 要求安装之前。安装失败太多,pip 太旧了,我想在接近其他安装之前再确定一下。

这使我达到了 pip 20.0.2 左右。

更大的上下文:我确实指定了一个 virtualenv,我还通过清单文件设置了 ansible_python_interpreter="/usr/bin/python3"

        virtualenv: "/srv/venv"

您可能需要也可能不需要做这些事情,重要的是要 200% 确定 pip 是最新的。