Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible not able to find python module

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)
...
like image 441
Arya N Avatar asked Mar 27 '19 09:03

Arya N


People also ask

Where are Ansible Python modules located?

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.

How do I find Ansible module code?

You can find the module utility source code in the lib/ansible/module_utils directory under your main Ansible path.

How do I force Ansible in Python 3?

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.

Does Ansible require Python on target?

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.


1 Answers

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.

like image 105
valdeci Avatar answered Oct 12 '22 04:10

valdeci