Using ansible to run docker swarm on multiple virtual machines.
The ansible is not able to find the python module docker
on the remote machine, even though it has been installed.
Runs the playbook
sudo ansible-playbook -i inv2.py /etc/ansible/playbook.yml
Error message:
fatal: [10.212.137.216]: FAILED! => {"changed": false, "msg": "Failed to import docker or docker-py - No module named requests.exceptions. Try `pip install docker` or `pip install docker-py` (Python 2.6)"}
Module list:
ubuntu@donald0:~$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
...
cryptography (2.1.4)
docker (3.7.1)
docker-pycreds (0.4.0)
...
While you can write Ansible modules in any language, most Ansible modules are written in Python, including the ones central to letting Ansible work. By default, Ansible assumes it can find a /usr/bin/python on your remote system that is either Python2, version 2.6 or higher or Python3, 3.5 or higher.
You can find the module utility source code in the lib/ansible/module_utils directory under your main Ansible path.
Ansible will automatically detect and use Python 3 on many platforms that ship with it. To explicitly configure a Python 3 interpreter, set the ansible_python_interpreter inventory variable at a group or host level to the location of a Python 3 interpreter, such as /usr/bin/python3.
Most Ansible modules that execute under a POSIX environment require a Python interpreter on the target host. Unless configured otherwise, Ansible will attempt to discover a suitable Python interpreter on each target host the first time a Python module is executed for that host.
This error occurs because Ansible is searching for a different path of the python modules that you are using.
When you install Ansible using the official package, it uses Python 2.7, so when you run Ansible it will search for the python 2 modules.
There are some ways to solve this:
- Adding the ansible_python_interpreter
option setting your correct Python path:
Like the following example:
ansible-playbook -i inventory playbook.yml -e 'ansible_python_interpreter=/usr/bin/python3'
- Reinstall the ansible using the pip3:
Using the following commands:
sudo apt remove ansible
pip3 install ansible
I think that the second option is the best approach to avoid future errors.
Read more about Python 3 Support with Ansible: Ansible - Python 3 Support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With