Ansible and Docker Py Path Issues and Resolving Them

I ran into a snag while trying to build out our CICD pipeline here at dronze.com, and I wanted to write a quick article that might help others. TL;DR is that ansible expects its docker-py installations to be in dist-packages for 2.7.

TASK [dronze4slack : Log into private registry and force re-authorization] *****
fatal: [172.31.17.70]: FAILED! => {"changed": false, "failed": true, "msg": "Failed to import docker-py - No module named requests.exceptions. Try `pip install docker-py`"}

If you google it you find long threads about ansible and docker versions, rolling back to 1.9 blah blah blah.

Here is most likely what you are dealing with. Ansible doesn’t know where to find docker-py, that is because in some ubuntu configurations python installs docker-py in site-packages (where ansible does not expect it) and in others in dist-packages (where it does). To resolve this make sure you tell ansible to look in both places on the target machine. This can be done at either the task or play level.

---
- hosts: apphosts
vars:
docker_ecs_login_password:
"{{ lookup('env','ECS_LOGIN_PASSWORD') }}"
gather_facts: no
environment:
PYTHONPATH:
"{{ lookup('env','PYTHONPATH') }}:/usr/local/lib/python2.7/dist-packages:/usr/local/lib/python2.7/site-packages"
roles:
- { role: dronze4slack }