首页 > 解决方案 > Ansible pip virtualenv 安装失败,缺少 setuptools

问题描述

我发现了很多与 ansible pip + virtualenv 相关的文章/问题,但没有什么能让我感到困惑:

试图用 python 设置一个“简单”的 virtualenv,这个任务完全失败了:

---
- hosts: local
  connection: local
  tasks:
    - name:
      apt:
        pkg:
          - python3
          - python3-pip
          - python3-setuptools
          - virtualenv

    - name: Install some pip packages in virtualenv
      pip:
        name: requests
        virtualenv: /opt/somevenv
        virtualenv_python: python3
        virtualenv_site_packages: yes

这导致

root@my-pc:/some/path# ansible-playbook test.yaml 

PLAY [local] *****************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [apt] *******************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Install some pip packages in virtualenv] *******************************************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "No setuptools found in remote host, please install it first."}
    to retry, use: --limit @/some/path/test.retry

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1   

在当前安装了 python 3.7.3 并且使用 python3 的 Debian 10 上

root@my-pc:/some/path# ansible-playbook --version
ansible-playbook 2.7.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]

pip3 也正确安装了 setuptools:

root@my-pc:/some/path# pip3 show setuptools
Name: setuptools
Version: 40.8.0
Summary: Easily download, build, install, upgrade, and uninstall Python packages
Home-page: https://github.com/pypa/setuptools
Author: Python Packaging Authority
Author-email: distutils-sig@python.org
License: UNKNOWN
Location: /usr/lib/python3/dist-packages
Requires: 
Required-by:

此外,python3 的使用是强制的:

root@my-pc:/some/path# tail -n3 /etc/ansible/ansible.cfg 
# context = 3
[py3-hosts:vars]
ansible_python_interpreter=/usr/bin/python3

有人知道它为什么会失败吗?

奇怪的是,当做一个

# apt install python-setuptools

一切正常......我真的不明白为什么在后台以某种方式使用python2,尽管python3甚至设置为ansible。

标签: pythonansiblevirtualenvsetuptools

解决方案


推荐阅读