Skip to content
Latchkey

Ansible "Unexpected Exception ... cannot import name" in CI

Ansible crashed during startup with a Python ImportError, usually because a controller dependency (Jinja2, cryptography, resolvelib) is at a version incompatible with the installed ansible-core. The traceback names the symbol that could not be imported.

What this error means

The command exits with "ERROR! Unexpected Exception, this is probably a bug: cannot import name 'X' from 'Y'" and a Python traceback, before any host is contacted.

ansible
ERROR! Unexpected Exception, this is probably a bug: cannot import name
'environmentfilter' from 'jinja2.filters'

Common causes

A controller dependency version mismatch

A pip upgrade pulled a Jinja2 or cryptography version that ansible-core does not support, breaking an internal import.

A mixed install of ansible and ansible-core

Conflicting ansible and ansible-core versions, or a stale install, leave incompatible modules on the path.

How to fix it

Pin a compatible controller environment

  1. Install ansible-core in a clean venv with pinned versions.
  2. Let pip resolve a Jinja2/cryptography that matches that ansible-core.
  3. Re-run from the venv so the imports succeed.
Terminal
python -m venv .ansible && . .ansible/bin/activate
pip install "ansible-core==2.17.*"

Avoid mixing ansible and ansible-core

Install one or the other and do not upgrade controller libraries independently of ansible-core.

Terminal
pip uninstall -y ansible
pip install "ansible==10.*"

How to prevent it

  • Install ansible-core in a pinned, isolated venv in CI.
  • Do not upgrade Jinja2/cryptography independently of ansible-core.
  • Cache the controller venv so the resolved set stays stable.

Frequently asked questions

What causes ""Unexpected Exception ... cannot import name""?
A pip upgrade pulled a Jinja2 or cryptography version that ansible-core does not support, breaking an internal import.
How do I fix "Unexpected Exception ... cannot import name"?
Pin a compatible controller environment

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card