4949import shutil
5050import sys
5151import tempfile
52+ import textwrap
5253import traceback
5354import types
5455
56+ from ansible .module_utils .common .text .converters import to_native
5557from ansible .module_utils .six .moves import shlex_quote
5658
5759import mitogen .core
7577# Prevent accidental import of an Ansible module from hanging on stdin read.
7678import ansible .module_utils .basic
7779ansible .module_utils .basic ._ANSIBLE_ARGS = '{}'
80+ import ansible .module_utils .common .respawn
7881
7982# For tasks that modify /etc/resolv.conf, non-Debian derivative glibcs cache
8083# resolv.conf at startup and never implicitly reload it. Cope with that via an
@@ -622,6 +625,20 @@ def revert(self):
622625 sys .argv [:] = self .original
623626
624627
628+ def _create_payload ():
629+ smuggled_args = getattr (ansible .module_utils .basic , '_ANSIBLE_ARGS' )
630+ if not smuggled_args :
631+ raise Exception ('unable to access ansible.module_utils.basic._ANSIBLE_ARGS (not launched by AnsiballZ?)' )
632+
633+ assert 0 , '%r, %r, spec %r' % (
634+ sys .modules ['__main__' ],
635+ ' ' .join ('%s=%r' % (s , getattr (sys .modules ['__main__' ], s )) for s in sorted (dir (sys .modules ['__main__' ]))),
636+ ' ' .join ('%s=%r' % (s , getattr (sys .modules ['__main__' ].__spec__ , s )) for s in sorted (dir (sys .modules ['__main__' ].__spec__ ))),
637+ )
638+ module_fqn = sys .modules ['__main__' ]._module_fqn
639+ modlib_path = sys .modules ['__main__' ]._modlib_path
640+
641+
625642class NewStyleStdio (object ):
626643 """
627644 Patch ansible.module_utils.basic argument globals.
@@ -640,11 +657,15 @@ def __init__(self, args, temp_dir):
640657 self .original_get_path = getattr (ansible .module_utils .basic ,
641658 'get_module_path' , None )
642659 ansible .module_utils .basic .get_module_path = self ._get_path
660+ self .original_create_payload = getattr (ansible .module_utils .common .respawn ,
661+ '_create_payload' , None )
662+ ansible .module_utils .common .respawn ._create_payload = _create_payload
643663
644664 def _get_path (self ):
645665 return self .temp_dir
646666
647667 def revert (self ):
668+ ansible .module_utils .common .respawn ._create_payload = self .original_create_payload
648669 ansible .module_utils .basic .get_module_path = self .original_get_path
649670 sys .stdout = self .original_stdout
650671 sys .stderr = self .original_stderr
@@ -1032,6 +1053,9 @@ def _run(self):
10321053 'ansible_module_' + os .path .basename (self .path ),
10331054 )
10341055
1056+ # CREDIT https://github.com/phemmer
1057+ # https://github.com/mitogen-hq/mitogen/issues/849#issuecomment-988207114
1058+ mod ._module_fqn = self .py_module_name
10351059 code = self ._get_code ()
10361060 rc = 2
10371061 try :
0 commit comments