ansible即使安装也找不到python包

Tec*_*y94 3 ansible docker docker-compose devops

尝试使用docker-compose该模块在 ansible 剧本中使用docker_compose。请注意,我可以在 ansible 中成功使用 docker 模块,但遇到docker_compose模块问题。我使用纯文本变量进行本地测试,使用 ansible-vault 来存储机密。

在 ansible 文档中,它解释说他们的 docker-compose 模块仅支持 docker-compose 的版本 1 和 2。

这是我的docker-compose.yml

---
version: "2.4"
services:
   my_demo:
       build: .
       networks:
          demo_net:
             ipv4_address: 172.0.1.2
       ports:
          - 8080:8080
       image: demo_image

networks:
    demo_net:
       driver: bridge
       ipam:
         driver: default
         config:
           - subnet: 172.0.1.0/30
             gateway: 172.0.1.1
Run Code Online (Sandbox Code Playgroud)

这是我的main.yml

---
- hosts: localhost
  connection: local
  become: true

  vars:
    - ansible_sudo_pass: password
    - ansible_python_interpreter: /usr/bin/python3

  tasks:
    - name: Docker compose
      docker_compose:
         project_name: docker_fhe_ansible
         project_src: .
         build: yes
      register: output

    - debug:
        var: output
Run Code Online (Sandbox Code Playgroud)

输出:

PLAY [localhost] **********************************************************************************************************************************************************************************

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

TASK [Docker compose] *****************************************************************************************************************************************************************************
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 ubuntu's Python /usr/bin/python3. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'docker'"}

PLAY RECAP ****************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
Run Code Online (Sandbox Code Playgroud)

错误是“没有名为 docker 的模块”,我可以验证 python3 有该模块:

john@ubuntu:~/Documents/docker-ansible$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
>>> import ansible
Run Code Online (Sandbox Code Playgroud)

toy*_*ian 5

您声明,您已验证这些软件包是否存在,但您可以通过多种方式安装软件包,但这些方式并不能使它们全局可用。例如,您可以将它们安装在 virtualenv 中或仅为您的用户安装。
如果 ansible 告诉您它缺少这些软件包,则它无法找到它们。您正在root机器 ( become: true) 上运行 ansible,因此您需要确保这些软件包可用于root.
(编辑:这实际上是问题所在。该软件包可供用户使用john,但不适用于root

您缺少该docker模块(可能还缺少其他要求)。
在游戏开始时使用以下命令安装它们

- name: ensure required pip-packages are installed
  pip:
    name: 
      - docker>=1.8.0
      - PyYAML>=3.11
      - docker-compose>=1.7.0
Run Code Online (Sandbox Code Playgroud)

如果您使用单独的剧本安装它们,请确保也在become: true该剧本上使用,否则它将再次仅为您的用户安装。

在编写剧本或角色时,我通常要做的就是确保首先安装所有必需的包。这意味着我会将任务放在使用该docker_compose模块的同一剧本/角色中。像这样,我确信即使同事在机器上搞乱,我的东西也能工作,并且他们也可以“按原样”使用我的代码,而无需先安装某些东西。

进一步说明:

  • 在使用模块之前,请务必检查文档以了解模块要求并安装它们。例如docker_compose 模块
  • 另请注意,您需要将它们安装在远程主机上,而不是 ansible-controller 上!
  • 如果您使用的是 virtualenv,请查看pip 模块的文档。