即使安装了 Ansible,也无法导入 docker-py

新手上路,请多包涵

我检查了 这篇文章 并遵循了两个答案中的修复,但都没有用。我要开一篇新帖子,部分是因为这个,部分是因为我得到了一个稍微不同的错误,即使问题可能是一样的。

可靠的主机:

 $ ansible --version
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

目标客户端 myserver:

 $ pip list | egrep 'six|docker|websocket_client'
docker-py (1.2.3)
six (1.10.0)

测试.yml:

 ---
- hosts: myserver
  remote_user: root
  tasks:
  - name: stop any running docker registries
    docker_container:
      name: registry
      state: stopped
...

Ansible 服务器(ansible-playbook 别名为 ap):

$ ap -vvvv test.yml

输出:

(可能是无关的输出,被剪掉了):

 fatal: [myserver]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "api_version": null,
            "blkio_weight": null,
            "cacert_path": null,
            "capabilities": null,
            "cert_path": null,
            "command": null,
            "cpu_period": null,
            "cpu_quota": null,
            "cpu_shares": null,
            "cpuset_cpus": null,
            "cpuset_mems": null,
            "debug": false,
            "detach": true,
            "devices": null,
            "dns_opts": null,
            "dns_search_domains": null,
            "dns_servers": null,
            "docker_host": null,
            "entrypoint": null,
            "env": null,
            "etc_hosts": null,
            "exposed_ports": null,
            "filter_logger": false,
            "force_kill": false,
            "groups": null,
            "hostname": null,
            "image": null,
            "interactive": false,
            "ipc_mode": null,
            "keep_volumes": true,
            "kernel_memory": null,
            "key_path": null,
            "kill_signal": null,
            "labels": null,
            "links": null,
            "log_driver":
            "json-file",
            "log_options": null,
            "mac_address": null,
            "memory": "0",
            "memory_reservation": null,
            "memory_swap": null,
            "memory_swappiness": null,
            "name": "registry",
            "network_mode": null,
            "networks": null,
            "oom_killer": null,
            "paused": false,
            "pid_mode": null,
            "privileged": false,
            "published_ports": null,
            "pull": false,
            "read_only": false,
            "recreate": false,
            "restart": false,
            "restart_policy": null,
            "restart_retries": 0,
            "security_opts": null,
            "shm_size": null,
            "ssl_version": null,
            "state": "stopped",
            "stop_signal": null,
            "stop_timeout": null,
            "timeout": null,
            "tls": null,
            "tls_hostname": null,
            "tls_verify": null,
            "trust_image_content": false,
            "tty": false,
            "ulimits": null,
            "user": null,
            "uts": null,
            "volume_driver": null,
            "volumes": null,
            "volumes_from": null
        },
        "module_name": "docker_container"
    },
    "msg":

(相关错误):

"Failed to import docker-py - cannot import name NotFound. Try pip install docker-py"}

当我按照参考帖子中的第一个答案将 docker-py 模块降级到 1.1.0 时,我得到了同样的错误。我还尝试对目录进行 chmod,但没有任何区别:

 (/usr/lib/python2.7/site-packages) myserver$ ls -lad docker*
drwxr-xr-x. 6 root root 4096 Jul  4 10:57 docker/
drwxr-xr-x. 2 root root 4096 Jul  4 10:57 docker_py-1.2.3-py2.7.egg-info/

来自 chmod -R go+rx docker*

有没有人见过这个?我尝试使用 pip ansible 模块来安装模块,然后在手动删除它们之后,像参考帖子中那样手动重新安装它们。我也在使用 2.1.0.0。如您所见,这应该可以解决此问题。

原文由 volvox 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 764
2 个回答

这是因为 python 模块的新版本 dockerdocker-py 使用不兼容。我不得不返回并明确指定以下版本的 PIP 包:

  • 泊坞窗:2.0.0
  • 泊坞窗:1.10.6

这些的示例剧本任务:

 - name: install certain python modules for docker
  pip:
    name: "{{ item.name }}"
    version: "{{ item.version }}"
    state: present
  with_items:
  - { name: docker, version: 2.0.0 }
  - { name: docker-py, version: 1.10.6 }

从那以后,我所有的剧本都很好用。

原文由 anoxis 发布,翻译遵循 CC BY-SA 3.0 许可协议

长话短说

Don’t use the --user flag for pip to install docker module and then use the -b or --become flag for ansible-playbook 因为提升的剧本实例不会看到 docker 为不同用户安装的模块。


事后看来,其他人可能很明显为什么我遇到了问题,但无论出于何种原因,我选择安装 docker 使用 pip 的 --user 标志然后有了不幸的“想法”使用 -b--become 选项。

这导致“显然”安装了 docker 模块对于运行我的剧本的提升的 Ansible 实例不可用。分享以防有人有“那些日子之一”并且后来偶然发现了这一点。希望它能对您有所帮助,因为我为此提醒支付了一大笔“愚蠢的税”,希望对我们俩都足够。 :)

原文由 deoren 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏