Ansible执行docker-compose任务时Docker Python SDK导入失败问题求助
Let's break down the possible causes and actionable fixes for your issue, since you've already confirmed the Python interpreter path and installed the docker module:
1. User-Specific vs. System-Wide Module Installation Mismatch
It’s common to install Python modules with pip install --user docker, which places them in your personal ~/.local/lib directory. But if Ansible runs as the root user (the default unless you specify remote_user), it won’t access modules installed for a regular user.
Fix:
Install the docker module system-wide using the absolute path of your Python 3 interpreter to ensure it’s available to all users:
sudo /usr/bin/pip3 install docker
Since your Python version is 3.6.9 (which is no longer supported by the latest docker SDK), install a compatible version:
sudo /usr/bin/pip3 install docker==4.4.4
(Versions of the Docker SDK for Python >=5.0 require Python 3.7+, so this older version works with 3.6.)
2. Confirm Ansible is Using the Correct Python Interpreter
Even if you set ansible_python_interpreter in ansible.cfg, individual inventory entries or playbook-level variables might override this setting. Let’s verify the interpreter Ansible actually uses on the managed node:
Add this debug task before your docker_compose task:
- name: Check Python interpreter details debug: msg: "Using Python path: {{ ansible_python.executable }}, Version: {{ ansible_python.version.full }}"
If the output doesn’t show /usr/bin/python3 and version 3.6.9, explicitly set the interpreter for the docker_compose task:
- name: Run docker-compose docker_compose: project_src: "{{ my_project_path }}" files: - docker-compose.yaml - other_file.yaml vars: ansible_python_interpreter: /usr/bin/python3
3. Validate Module Accessibility Directly on the Managed Node
Log into your managed host and run this command to check if the docker module is accessible to /usr/bin/python3:
/usr/bin/python3 -c "import docker; print('Module found at:', docker.__file__)"
- If this fails: The module isn’t installed for this Python version. Reinstall using
/usr/bin/pip3as mentioned earlier. - If this succeeds: The issue lies with Ansible’s execution environment. Check if Ansible runs with a different user or missing environment variables. You can explicitly set the
PYTHONPATHin your task to include the module’s directory:
Replace- name: Run docker-compose with correct PYTHONPATH docker_compose: project_src: "{{ my_project_path }}" files: - docker-compose.yaml - other_file.yaml environment: PYTHONPATH: "/usr/local/lib/python3.6/dist-packages:/home/your_user/.local/lib/python3.6/site-packages"/home/your_userwith the actual username if you installed modules locally.
4. Rule Out Virtual Environment Conflicts
If your managed node uses virtual environments (like virtualenv or pyenv), ensure Ansible isn’t accidentally picking up a different Python environment. Using the absolute path /usr/bin/python3 should avoid this, but double-check there’s no alias or environment variable overriding it.
内容的提问来源于stack exchange,提问作者Max

