Ansible - Python - Docker: Module Not Found Docker / Docker-Py — Why No Module Found Error Is Coming in Python Prompt and in Ansible
Ansible 2.8.3 When I Run My Playbook, I See the Following Error. Task [Deployer_Role: Creating Network] ********************************************************...
Ansible 2.8.3
When I run my playbook, I see the following error.
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..)
Task action is:
- name: Creating volumes
docker_volume:
name: "{{ item }}"
with_items:
- bbdb-data
- bb-data
On the machine, it shows the required pip modules are installed for Python 2.7.5.
NOTE: I'm not using or have Python3 not sure why I'm getting No module found error when everything is installed.
[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 ~]$
I have tried passing:
--extra-vars "ansible_python_interpreter=/usr/bin/python2.7 to no luck.
On target node, Folder/File permission on /usr/lib/python2.7/site-packages or /usr/lib64/python2.7/site-packages is 655 or 775 (Tried both) to no luck.
Adding - pip: { name: docker, state: present } before - name: "Creating volumes" ... task, shows Requirements already satisfied for the required pip modules lilke docker etc and they are installed, but the next one for creating docker_volume or any use of ansible docker_* module fails with No module found for either docker or urllib3 or requests
They are all present (if I do pip list or - pip: { name: <pip_module>, state: present } in Ansible.
root user's umask is 0027 on both the ansible control and the target node machines.
1 Answer
After adding the following main task/action, before calling any other main tasks/actions or importing other yml file based tasks, worked!!
Basically the idea was to ensure state: forcereinstall is used while installing docker.
-- When this option was not used, Ansible's docker installation was showing ok: ... i.e. docker is already installed but then later, other actions where I used docker_* ansible modules, were still giving me the above no named module errors.
# 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