Hi,
I’m maintaining an Ansible collection that currently supports multiple ansible-core versions.
Our module utility currently imports Paramiko as:
from ansible.module_utils.compat.paramiko import paramiko
This works with ansible-core 2.20 and earlier, but with ansible-core 2.21 we get:
ModuleNotFoundError: No module named 'ansible.module_utils.compat.paramiko'
I understand that ansible.module_utils.compat.paramiko was removed in ansible-core 2.21, and the recommended approach for 2.21+ is:
import paramiko
Our collection already has Paramiko listed in requirements.txt:
cryptography
paramiko
pexpect
My question is:
What is the recommended way for an Ansible collection to support both ansible-core 2.20 and earlier, as well as 2.21 and later?
Would the recommended implementation simply be:
import paramiko
and make Paramiko an explicit dependency of the collection?
Or should the collection use a compatibility pattern such as:
try:
import paramiko
except ImportError:
from ansible.module_utils.compat.paramiko import paramiko
The goal is to maintain a single codebase that works across the supported ansible-core versions without depending on an Ansible internal/removed module.
I also tested this with:
ansible-test sanity --test import --python 3.12
After changing to:
import paramiko
the error changed to:
ModuleNotFoundError: No module named 'paramiko'
even though Paramiko is installed in my development virtual environment:
paramiko==5.0.0
So I would also appreciate clarification on the recommended way to make a collection’s external Python dependency available to the ansible-test sanity import environment.
Thanks!