Arya N
Arya N

Reputation: 95

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)
...

Upvotes: 5

Views: 26529

Answers (4)

valdeci
valdeci

Reputation: 15237

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.

Upvotes: 12

Peter Evans
Peter Evans

Reputation: 21

FWIW, I had this problem because the directories under /usr/lib64/python2.7 were readable and executable for root only. After I ran a chmod -R go+rX /usr/lib{,64} the problem was gone. root's umask was 077, hence the problem.

Upvotes: 0

user14344225
user14344225

Reputation:

You need python interpreter in you hosts file- /etc/ansible/hosts

ansible_python_interpreter=/usr/bin/python3

For example:

ubuntu@${ip} ansible_private_key_file=~/.ssh/${var.key_name}.pem ansible_python_interpreter=/usr/bin/python3

For playbook, you need the Python Module and python docker-compose. Working example would be:

tasks:
    - name: Install Docker
      apt:
        update_cache: yes
        name: docker.io
  
    - name: Install Docker-Compose
      apt:
        name: docker-compose
  
    - name: Install Python Module
      apt:
        name: python3-pip
  
    - name: Install Python Docker-Compose
      pip:
        name: docker-compose

Upvotes: -2

Naveed Kamran
Naveed Kamran

Reputation: 453

The following configurations work for me. It installs docker, python and docker-compose latest version

---
- name: Checking docker on latest version
  apt: name=docker.io state=latest

- name: Checking python
  apt: name=python state=latest

- name: Checking docker-compose on latest version
  apt: name=docker-compose state=latest

Upvotes: 0

Related Questions