Ansible - Resolve "Failed to import the required Python library"

by
Jeremy Canfield |
Updated: February 26 2023
| Ansible articles
Let's say the following is being returned.
fatal: [server1.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on server1.example.com's Python /usr/bin/python. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'requests'"}
On the target syste, determine if Python 2.x.x or 3.x.x is being used.
~]$ /usr/bin/python --version
Python 3.9.12
If running Python version 2.7 or higher, use pip install to install the Python docker module.
pip3 install docker
If not root, you may need to run this command using sudo as well.
sudo pip3 install docker
If running Python version 2.6 or below, use pip to install docker-py.
pip3 install docker-py
If not root, you may need to run this command using sudo as well.
sudo pip3 install docker-py
Did you find this article helpful?
If so, consider buying me a coffee over at