ansible 返回“无法导入所需的 Python 库(适用于 Python 的 Docker SDK:docker (Python >= 2.7) 或 docker-py (Python 2.6))

问题描述 投票:0回答:6

我正在 ubuntu 中运行我的服务器:

+ sudo cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

我使用ansible,当我运行它时,出现以下错误:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on dd63315fad06's Python /usr/bin/python. Please read module documentation and install in the appropriate location, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named docker"}

当我跑步时

python -c "import sys; print(sys.path)"

我明白了:

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/pip-19.2.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/fasteners-0.15-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/monotonic-1.5-py2.7.egg', '/usr/lib/python2.7/dist-packages']

和python版本如下:

+ python --version
Python 2.7.12
+ python3 --version
Python 3.5.2

然后我发现一切都很好,但我不知道为什么我会这样

"Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on dd63315fad06's Python /usr/bin/python. Please read module documentation and install in the appropriate location, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named docker"

在ansible中?

ansible ansible-2.x ansible-inventory ansible-facts
6个回答
30
投票

您似乎没有安装

docker
模块。

您需要通过系统包管理器(例如,

apt install python-docker
)或使用
pip
pip install docker
)来安装它。

如果您有多个 Python 版本,请确保已将

docker
模块安装到 Ansible 正在使用的版本中。


14
投票

在这里,在 2021 年 5 月,对于 Ubuntu 20.04,您需要运行

apt install python3-docker
,因为默认情况下不再提供 python 2.x


10
投票

我在 Ansible docker-compose 模块中遇到了同样的问题。我能够通过为这些任务选择 python3 来修复它。

之前(不工作)

- name: Create docker service services
  docker_compose:
    project_src: /root/
  become: true

(工作后)

我们可以通过 $which python3

获取python位置
- name: Create fleuntd services
  docker_compose:
    project_src: /root/
  become: true
  vars:
    ansible_python_interpreter: /bin/python3

4
投票

随着 2021 年 4 月 Docker SDK For Python 5.0 版本的发布,我开始遇到同样的错误。错误消息几乎与原始问题完全相同,唯一的区别是错误消息末尾是以下语句之一:

The error was: No module named parse

The error was: No module named selectors

这最终是由于 Ansible 使用旧版本的

pip
,在 Python2.7 设置上错误地安装了 Python3 库。修复方法是将
docker
Python 库的版本固定到早于版本 5.0 的版本,并将“websocket-client”库固定到早于版本 1.0 的版本:

- name: Install Docker SDK for Python
  pip:
    name: "docker<5" 
  become: yes

- name: Setup more docker dependencies
  pip:
    name: "websocket-client<1" 
  become: yes

或者,考虑到 Python2 仍在使用,这组命令也可以工作:

pip install docker<5
pip install websocket-client<1

安装这些旧版本的适用于 Python 的 Docker SDK 和 Websocket 客户端后,Ansible 能够再次代表我成功管理 Docker。


3
投票

就我而言(安装了 docker 的 Ubuntu 20)需要这些命令

apt update
apt install python3 python3-pip
pip3 install docker docker-compose

详细要求:

https://docs.ansible.com/ansible/latest/collections/community/general/docker_compose_module.html


0
投票

带有 debian 10 的 Debian bootstrap 具有本机 python 2.6,使用 chroot 进入 eco 并安装 ansible,这很容易

© www.soinside.com 2019 - 2024. All rights reserved.