Merge pull request #312 from dw/issue309

issue #309: fix environment cleanup regression.
pull/315/head
dw 6 years ago committed by GitHub
commit 422c6e0b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -288,9 +288,13 @@ class TemporaryEnvironment(object):
os.environ[key] = str(value) os.environ[key] = str(value)
def revert(self): def revert(self):
if self.env: """
os.environ.clear() Revert changes made by the module to the process environment. This must
os.environ.update(self.original) always run, as some modules (e.g. git.py) set variables like GIT_SSH
that must be cleared out between runs.
"""
os.environ.clear()
os.environ.update(self.original)
class TemporaryArgv(object): class TemporaryArgv(object):

@ -24,6 +24,11 @@ Mitogen for Ansible
* `#299 <https://github.com/dw/mitogen/pull/299>`_: fix the ``network_cli`` * `#299 <https://github.com/dw/mitogen/pull/299>`_: fix the ``network_cli``
connection type when the Mitogen strategy is active. connection type when the Mitogen strategy is active.
* `#309 <https://github.com/dw/mitogen/pull/309>`_: fix a regression to process
environment cleanup, caused by the change in v0.2.1 to run local tasks with
the correct environment.
Core Library Core Library
~~~~~~~~~~~~ ~~~~~~~~~~~~

@ -11,4 +11,5 @@
- import_playbook: custom_python_new_style_module.yml - import_playbook: custom_python_new_style_module.yml
- import_playbook: custom_python_want_json_module.yml - import_playbook: custom_python_want_json_module.yml
- import_playbook: custom_script_interpreter.yml - import_playbook: custom_script_interpreter.yml
- import_playbook: environment_isolation.yml
- import_playbook: forking_behaviour.yml - import_playbook: forking_behaviour.yml

@ -0,0 +1,50 @@
# issue #309: ensure process environment is restored after a module runs.
- name: integration/runner/environment_isolation.yml
hosts: test-targets
any_errors_fatal: true
gather_facts: true
tasks:
# ---
# Verify custom env setting is cleared out.
# ---
# Verify sane state first.
- custom_python_detect_environment:
register: out
- assert:
that: not out.env.evil_key is defined
- shell: echo 'hi'
environment:
evil_key: evil
# Verify environment was cleaned up.
- custom_python_detect_environment:
register: out
- assert:
that: not out.env.evil_key is defined
# ---
# Verify non-explicit module env mutations are cleared out.
# ---
# Verify sane state first.
- custom_python_detect_environment:
register: out
- assert:
that: not out.env.evil_key is defined
- custom_python_modify_environ:
key: evil_key
val: evil
# Verify environment was cleaned up.
- custom_python_detect_environment:
register: out
- assert:
that: not out.env.evil_key is defined

@ -0,0 +1,22 @@
#!/usr/bin/python
# I am an Ansible new-style Python module. I modify the process environment and
# don't clean up after myself.
from ansible.module_utils.basic import AnsibleModule
import os
import pwd
import socket
import sys
def main():
module = AnsibleModule(argument_spec={
'key': {'type': str},
'val': {'type': str}
})
os.environ[module.params['key']] = module.params['val']
module.exit_json(msg='Muahahaha!')
if __name__ == '__main__':
main()
Loading…
Cancel
Save