Ansible Sanity Tests Failing Due to Unrecognized 'cryptography' Module"

Hi,

We have implemented a custom lookup plugin in Ansible that includes a certificate validation feature.

To perform the certificate validation, we are using the Python cryptography module. However, during the Ansible sanity tests, we are encountering the following error, causing the tests to fail:

Error:
ERROR: Found 1 import issue(s) on Python 3.12 that need to be resolved:

ERROR: plugins/lookup/test.py:170:0: traceback: ModuleNotFoundError: No module named ‘cryptography’

Modules used in the code:

from cryptography.x509 import load_pem_x509_certificate

from cryptography.hazmat.backends import default_backend

The cryptography module is already available in the Python environment, but it seems that the Ansible sanity tests are unable to recognize it.

Could you please assist me in resolving this issue?

Your code needs to be importable even when custom libraries are not available, so you should use try/except and then handle the missing library gracefully at runtime.

For example:

1 Like

There’s also import — Ansible Community Documentation which documents what to do if you encounter failures due to the import sanity test.

1 Like

Ah, thanks, I knew that had to be documented somewhere but wasn’t able to track it down.

It is in my permanent browsing history, I just have to type “im” to get it as the first suggestion :wink: