Ansible - Python - Docker:未找到模块 docker / docker-py — 为什么在 Python 提示符和 Ansible 中出现未找到模块错误

Ansible - Python - Docker:未找到模块 docker / docker-py — 为什么在 Python 提示符和 Ansible 中出现未找到模块错误

Ansible2.8.3

当我运行我的剧本时,看到以下错误。

TASK [deployer_role : Creating network] ***************************************************************************************************************************************************************************
fatal: [ansible_node01.company.server.com]: 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 ans'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 requests (or urllib3 or etc was coming..)

任务动作是:

- name: Creating volumes
  docker_volume:
    name: "{{ item }}"
  with_items:
    - bbdb-data
    - bb-data

在机器上,它显示已安装 Python 2.7.5 所需的 pip 模块。

笔记我没有使用或没有 Python3不确定为什么No module found error所有东西都安装好了之后还会有这种情况。

[akumar@ansible_node1 ~]$ which python
/usr/bin/python
[akumar@ansible_node1 ~]$ python -V
Python 2.7.5
[akumar@ansible_node1 ~]$ pip list
Babel (0.9.6)
backports.ssl-match-hostname (3.5.0.1)
certifi (2020.4.5.1)
chardet (3.0.4)
configobj (4.7.2)
decorator (3.4.0)
docker (4.1.0)
ethtool (0.8)
idna (2.9)
iniparse (0.4)
ipaddr (2.1.11)
ipaddress (1.0.16)
IPy (0.75)
Jinja2 (2.7.2)
jsonpatch (1.2)
jsonpointer (1.9)
kitchen (1.1.1)
lxml (3.2.1)
M2Crypto (0.21.1)
Magic-file-extensions (0.2)
MarkupSafe (0.11)
pciutils (1.7.3)
perf (0.1)
pip (8.1.2)
policycoreutils-default-encoding (0.1)
prettytable (0.7.2)
pycurl (7.19.0)
pygobject (3.22.0)
pygpgme (0.3)
pyinotify (0.9.4)
pyliblzma (0.5.3)
pyOpenSSL (0.13.1)
pyserial (2.6)
python-dateutil (1.5)
python-dmidecode (3.10.13)
python-linux-procfs (0.4.9)
pyudev (0.15)
pyxattr (0.5.1)
PyYAML (3.10)
requests (2.23.0)
rhnlib (2.5.65)
schedutils (0.4)
seobject (0.1)
sepolicy (1.1)
setuptools (0.9.8)
six (1.9.0)
subscription-manager (1.24.26)
syspurpose (1.24.45)
urlgrabber (3.10)
urllib3 (1.25.9)
websocket-client (0.57.0)
yum-metadata-parser (1.1.4)
[akumar@ansible_node1 ~]$ python
Python 2.7.5 (default, Aug 13 2020, 02:51:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import sys
>>> print sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
>>>
>>> import docker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/docker/__init__.py", line 2, in <module>
    from .api import APIClient
  File "/usr/lib/python2.7/site-packages/docker/api/__init__.py", line 2, in <module>
    from .client import APIClient
  File "/usr/lib/python2.7/site-packages/docker/api/client.py", line 5, in <module>
    import requests
  File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
ImportError: No module named urllib3
>>>
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
ImportError: No module named urllib3
>>>
>>> import urllib3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named urllib3
>>>
>>> quit()
[akumar@ansible_node1 ~]$

我曾尝试通过: --extra-vars "ansible_python_interpreter=/usr/bin/python2.7没有运气。

在目标节点上,文件夹/文件的权限为/usr/lib/python2.7/site-packages655/usr/lib64/python2.7/site-packages或 775(均试过)但没有成功。

在任务- pip: { name: docker, state: present }前添加,显示所需的 pip 模块- name: "Creating volumes" ...Requirements already satisfied码头工人等,它们都已安装,但下一个用于创建docker_volume或任何使用 ansibledocker_*模块的失败,No module found fordockerurllib3requests

它们都存在(如果我这样做pip list或者- pip: { name: <pip_module>, state: present }在 Ansible 中)。

root用户的 umask0027在 ansible 控制和目标节点机器上都有。

答案1

添加以下主要任务/操作后,在调用任何其他主要任务/操作或导入其他基于 yml 文件的任务之前,即可成功!!

基本上,这个想法是在安装 docker 时确保state: forcereinstall使用它。——
当不使用这个选项时,Ansible 的 docker 安装显示ok: ...docker 已安装,但后来,我使用docker_*ansible 模块的其他操作仍然出现上述no named module错误。

# If forcereinstall is removed, you may get an error i.e. no module found 
#
- name: "Force Install 'docker' pip module"
  pip:
    name: docker
    extra_args: "--no-index --find-links={{ docker_SDK_Python_dest_path_where_I_see_bunch_of_whl_or_tar_gz_files }}"
    state: forcereinstall

相关内容