SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
Ansible New Added 19 September 2026

Ansible: Failed to import the required Python library (docker) on host

Modules run on the target with the interpreter Ansible discovered there, so a library installed on the control node, or into a different virtualenv, is invisible to them. The message names the interpreter it used, and that path is the part worth reading before installing anything again.

Quick fix

Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.

Quick fix
# Which interpreter the module actually used on that host
ansible -m setup -a 'filter=ansible_python*' web01
ansible web01 -m command -a 'python3 -c "import docker; print(docker.__file__)"'

# Install into that interpreter, not into the system default
- name: Install the Docker SDK for the interpreter Ansible uses
  ansible.builtin.pip:
    name: docker>=7.1.0
    executable: "{{ ansible_python_interpreter | dirname }}/pip3"

# Or pin the interpreter so discovery cannot drift between hosts
# inventory
[web]
web01 ansible_python_interpreter=/usr/bin/python3

# delegate_to: localhost runs on the control node, so the library is needed there
- community.docker.docker_container: { name: app, image: nginx }
  delegate_to: localhost

# become changes the user, and a pip --user install belongs to the other one

How to diagnose Ansible errors

Ansible errors split cleanly between transport problems (it cannot reach or authenticate to the host) and execution problems (it reached the host but the play failed). The message header tells you which: UNREACHABLE! is transport, FAILED! is execution. Transport issues are SSH issues wearing an Ansible hat and should be debugged with plain ssh first.

If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.

  1. Run ansible -m ping all before running any playbook. It isolates connectivity and privilege escalation from your task logic.
  2. Raise verbosity progressively: -v shows results, -vvv shows the SSH command line, -vvvv shows connection debugging. Most unreachable errors are solved at -vvv.
  3. Use --check --diff to see what a play would change without changing it. This turns a destructive debugging loop into a safe one.
  4. For undefined-variable errors, dump the actual scope with a debug task (var: hostvars[inventory_hostname]) rather than reasoning about precedence from the docs.
  5. Remember variable precedence order: extra vars (-e) beat everything, role defaults lose to everything. Surprising values are nearly always a precedence surprise.

Tools worth reaching for

  • ansible -m ping
  • ansible-playbook --check --diff
  • ansible-lint
  • ssh -vvv

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

docs.ansible.com docs.ansible.com

Related Ansible errors

See all 12 Ansible errors →

Browse other categories

Something missing or wrong?

This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.