issue #309: fix environment cleanup regression.

Closes #309.
pull/312/head
David Wilson 6 years ago
parent def848f118
commit 15d68b3c32

@ -288,7 +288,11 @@ class TemporaryEnvironment(object):
os.environ[key] = str(value)
def revert(self):
if self.env:
"""
Revert changes made by the module to the process environment. This must
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)

@ -24,6 +24,11 @@ Mitogen for Ansible
* `#299 <https://github.com/dw/mitogen/pull/299>`_: fix the ``network_cli``
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
~~~~~~~~~~~~

@ -11,4 +11,5 @@
- import_playbook: custom_python_new_style_module.yml
- import_playbook: custom_python_want_json_module.yml
- import_playbook: custom_script_interpreter.yml
- import_playbook: environment_isolation.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