From 94dc4554db8671f10f0278e87c79d24344981cd2 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 13 Jun 2016 07:56:18 -0700 Subject: [PATCH] Workaround bad interaction with .pth files. (#16238) When setuptools installs a python module (as is done via python setup.py install) It puts the module into a subdirectory of site-packages and then creates an entry in easy-install.pth to load that directory. This makes it difficult for Ansiballz to function correctly as the .pth file overrides the sys.path that the wrapper constructs. Using sitecustomize.py fixes this because sitecustomize overrides the directories handled in .pth files. Fixes #16187 --- lib/ansible/executor/module_common.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index 1784c919958..9d9abf2cf00 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -270,18 +270,33 @@ if __name__ == '__main__': # remote_tmpdir and this module executing under async. So we cannot # store this in remote_tmpdir (use system tempdir instead) temp_path = tempfile.mkdtemp(prefix='ansible_') + zipped_mod = os.path.join(temp_path, 'ansible_modlib.zip') modlib = open(zipped_mod, 'wb') modlib.write(base64.b64decode(ZIPDATA)) modlib.close() + if len(sys.argv) == 2: exitcode = debug(sys.argv[1], zipped_mod, ZIPLOADER_PARAMS) else: - z = zipfile.ZipFile(zipped_mod) + z = zipfile.ZipFile(zipped_mod, mode='r') module = os.path.join(temp_path, 'ansible_module_%(ansible_module)s.py') f = open(module, 'wb') f.write(z.read('ansible_module_%(ansible_module)s.py')) f.close() + + # When installed via setuptools (including python setup.py install), + # ansible may be installed with an easy-install.pth file. That file + # may load the system-wide install of ansible rather than the one in + # the module. sitecustomize is the only way to override that setting. + z = zipfile.ZipFile(zipped_mod, mode='a') + # py3: zipped_mod will be text, py2: it's bytes. Need bytes at the end + z = zipfile.ZipFile(zipped_mod, mode='a') + sitecustomize = u'import sys\\nsys.path.insert(0,"%%s")\\n' %% zipped_mod + sitecustomize = sitecustomize.encode('utf-8') + z.writestr('sitecustomize.py', sitecustomize) + z.close() + exitcode = invoke_module(module, zipped_mod, ZIPLOADER_PARAMS) finally: try: