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
@@ -978,10 +999,7 @@ def _get_code(self):
978999 True , # dont_inherit
9791000 ))
9801001
981- if mitogen .core .PY3 :
982- main_module_name = '__main__'
983- else :
984- main_module_name = b'__main__'
1002+ main_module_name = '__main__'
9851003
9861004 def _handle_magic_exception (self , mod , exc ):
9871005 """
@@ -1032,6 +1050,9 @@ def _run(self):
10321050 'ansible_module_' + os .path .basename (self .path ),
10331051 )
10341052
1053+ # CREDIT https://github.com/phemmer
1054+ # https://github.com/mitogen-hq/mitogen/issues/849#issuecomment-988207114
1055+ sys .modules ['__main__' ]._module_fqn = self .py_module_name
10351056 code = self ._get_code ()
10361057 rc = 2
10371058 try :
0 commit comments