Ansible not able to find python module

15,796

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.

Share:
15,796
Arya N
Author by

Arya N

Updated on June 13, 2022

Comments

  • Arya N
    Arya N almost 2 years

    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)
    ...
    
  • Zeitounator
    Zeitounator about 5 years
    This example does not use any docker* ansible modules and does not need any docker(-py) pip module installed
  • cdauth
    cdauth about 2 years
    @Zeitounator: The docker module from ansible requires the python docker module to be installed on the managed machine. One way to achieve this is to install python-docker using an Ansible module before using the Ansible docker plugin. Although I don't see this answer actually installing python-docker.