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.
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
- Install ansible-core in a clean venv with pinned versions.
- Let pip resolve a Jinja2/cryptography that matches that ansible-core.
- Re-run from the venv so the imports succeed.
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.
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.