Set alter_sys=True instead of False to address backwards incompat (#64670)

* Set alter_sys=True instead of False to address backwards incompat

* ci_complete

* Add integration test

* ci_complete

* sanity

* ci_complete

* Changelog fragment

* Update import test and validate-modules to match
This commit is contained in:
Matt Martz 2019-11-11 16:47:35 -06:00 committed by GitHub
parent c1493e5267
commit b93d92ef9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 4 deletions

View file

@ -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)

View file

@ -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}')

View file

@ -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()

View file

@ -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:

View file

@ -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

View file

@ -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: