diff --git a/changelogs/fragments/64664-fix-sys-modules-file.yml b/changelogs/fragments/64664-fix-sys-modules-file.yml new file mode 100644 index 0000000000..4e5c38efbd --- /dev/null +++ b/changelogs/fragments/64664-fix-sys-modules-file.yml @@ -0,0 +1,4 @@ +bugfixes: +- module executor - Address issue where changes to Ansiballz module code, change the behavior + of module execution as it pertains to ``__file__`` and ``sys.modules`` + (https://github.com/ansible/ansible/issues/64664) diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index 3b76ff7bbd..5451ce9586 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -192,7 +192,7 @@ def _ansiballz_main(): basic._ANSIBLE_ARGS = json_params %(coverage)s # Run the module! By importing it as '__main__', it thinks it is executing as a script - runpy.run_module(mod_name='%(module_fqn)s', init_globals=None, run_name='__main__', alter_sys=False) + runpy.run_module(mod_name='%(module_fqn)s', init_globals=None, run_name='__main__', alter_sys=True) # Ansible modules must exit themselves print('{"msg": "New-style module did not handle its own exit", "failed": true}') @@ -286,7 +286,7 @@ def _ansiballz_main(): basic._ANSIBLE_ARGS = json_params # Run the module! By importing it as '__main__', it thinks it is executing as a script - runpy.run_module(mod_name='%(module_fqn)s', init_globals=None, run_name='__main__', alter_sys=False) + runpy.run_module(mod_name='%(module_fqn)s', init_globals=None, run_name='__main__', alter_sys=True) # Ansible modules must exit themselves print('{"msg": "New-style module did not handle its own exit", "failed": true}') diff --git a/test/integration/targets/ansiballz_python/library/sys_check.py b/test/integration/targets/ansiballz_python/library/sys_check.py new file mode 100644 index 0000000000..aa22fe6820 --- /dev/null +++ b/test/integration/targets/ansiballz_python/library/sys_check.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# https://github.com/ansible/ansible/issues/64664 +# https://github.com/ansible/ansible/issues/64479 + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import sys + +from ansible.module_utils.basic import AnsibleModule + + +def main(): + module = AnsibleModule({}) + + this_module = sys.modules[__name__] + module.exit_json( + failed=not getattr(this_module, 'AnsibleModule', False) + ) + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/ansiballz_python/tasks/main.yml b/test/integration/targets/ansiballz_python/tasks/main.yml index d6efad8017..0aaa64515b 100644 --- a/test/integration/targets/ansiballz_python/tasks/main.yml +++ b/test/integration/targets/ansiballz_python/tasks/main.yml @@ -61,3 +61,8 @@ # custom module returned the correct answer - custom_module_return.answer == 42 + +# https://github.com/ansible/ansible/issues/64664 +# https://github.com/ansible/ansible/issues/64479 +- name: Run module that tries to access itself via sys.modules + sys_check: diff --git a/test/lib/ansible_test/_data/sanity/import/importer.py b/test/lib/ansible_test/_data/sanity/import/importer.py index a4c0e34e2a..1143db5366 100755 --- a/test/lib/ansible_test/_data/sanity/import/importer.py +++ b/test/lib/ansible_test/_data/sanity/import/importer.py @@ -223,7 +223,7 @@ def main(): with monitor_sys_modules(path, messages): with blacklist_imports(path, name, messages): with capture_output(capture_main): - runpy.run_module(name, run_name='__main__') + runpy.run_module(name, run_name='__main__', alter_sys=True) except ImporterAnsibleModuleException: # module instantiated AnsibleModule without raising an exception pass diff --git a/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py b/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py index 8d1b7ecf9a..666aa4347c 100644 --- a/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py +++ b/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py @@ -114,7 +114,7 @@ def get_py_argument_spec(filename, collection): with setup_env(filename) as fake: try: with CaptureStd(): - runpy.run_module(name, run_name='__main__') + runpy.run_module(name, run_name='__main__', alter_sys=True) except AnsibleModuleCallError: pass except BaseException as e: