mitogen-hq / mitogen

Distributed self-replicating programs in Python
https://mitogen.networkgenomics.com/
BSD 3-Clause "New" or "Revised" License
2.32k stars 197 forks source link

Ansible custom module: importing Python module only present on target works if target interpreter is Python 2.7 but fails if target is Python 3.x; works fine without mitogen #904

Open mbunkus opened 2 years ago

mbunkus commented 2 years ago

Summary: using Ansible with mitogen & the mitogen_linear strategy my custom Ansible module fails to load Python modules that only exist on the target host if the Python interpreter to use is Python 3, but succeeds if the interpreter is Python 2. The module is installed for both versions on the target.

Longer explanation:

I have a set of Python modules that are always installed on certain types of hosts (Univention Corporate Server) and that aren't easily available for installation elsewhere. On such servers those modules are available both for Python 2 and Python 3, and they're always installed somewhere in Python's system library path (for both versions simultaneously).

On my Ansible control node these modules are therefore not installed.

Using mitogen with mitogen_linear strategy I'd like to make use of said Python modules. I have a custom Ansible module that tries to use them via from … import ….

This works just fine (!) if I use ansible_python_interpreter: /usr/bin/python2 in my inventory. No problems whatsoever, the Python module gets loaded, I can use its functions etc.

This does not work at all if I use ansible_python_interpreter: /usr/bin/python3 in my inventory though. In that case importing throws an exception:

'univention' is present in the Mitogen importer blacklist, therefore this context will not attempt to request it from the master, as the request will always be refused.

If I run /usr/bin/python3 on the target manually and try the same from … import … line, it works just fine, too.

So why does this work when forcing Python 2 on the target, and doesn't when using Python 3 there? And is there a way to get this to work with Python 3, too?

Of course the same Ansible module works fine with both Python versions on the target if I use the original linear strategy instead of mitogen.

The Python used on the Ansible control node is 3.10.x. The Ansible control node does have Python 2 installed, but those aforementioned modules aren't installed on the Ansible control node at all. Ansible itself is run with Python 3.10.x.

I can provide tons of log files etc., but if the answer is "this is architecturally impossible to achieve", then I won't bother collecting them.

I'd be grateful for any type of advice/hint on how to get this running.

davidBMSTU commented 1 year ago

We faced the same. Did you find the solution?

ifalatiksetlog commented 3 months ago

this is still an issue more than two years later..