From b4ea34d0af4f2eff617a021661e4e0f933584beb Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Tue, 27 Jul 2021 17:19:51 -0400 Subject: [PATCH] Pass environment variables through task execution (#69) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pass environment variables through task execution Depends-On: ansible-collections/vmware.vmware_rest#235 SUMMARY Previously, the environment for a task would have been the same environment as the first task run. This first task was responsible for spawning the server which inherited that task's environment. This is a problem in cases where the environment might change in a latter task, for example, by setting the environment keyword on a task. In these cases, the environment for the new task is lost. This change wraps execution of a module in a temporary environment, passing through whatever envvars might have been set by the controller. ISSUE TYPE Bugfix Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Gonéri Le Bouder Reviewed-by: None Reviewed-by: Mike Graves --- changelogs/fragments/69-pass-envvar.yaml | 3 +++ plugins/module_utils/turbo/module.py | 1 + plugins/module_utils/turbo/server.py | 8 +++++++- plugins/modules/turbo_demo.py | 1 + .../targets/turbo_mode/tasks/main.yaml | 18 ++++++++++++++++++ tests/sanity/ignore-2.10.txt | 2 +- tests/sanity/ignore-2.11.txt | 2 +- tests/sanity/ignore-2.9.txt | 2 +- 8 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/69-pass-envvar.yaml diff --git a/changelogs/fragments/69-pass-envvar.yaml b/changelogs/fragments/69-pass-envvar.yaml new file mode 100644 index 0000000..804edc0 --- /dev/null +++ b/changelogs/fragments/69-pass-envvar.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: + - pass current task's environment through to execution (https://github.com/ansible-collections/cloud.common/pull/69). diff --git a/plugins/module_utils/turbo/module.py b/plugins/module_utils/turbo/module.py index d4bca04..372eb12 100644 --- a/plugins/module_utils/turbo/module.py +++ b/plugins/module_utils/turbo/module.py @@ -104,6 +104,7 @@ def run_on_daemon(self): data = [ ansiblez_path, json.dumps(args), + dict(os.environ), ] content = json.dumps(data).encode() result = turbo_socket.communicate(content) diff --git a/plugins/module_utils/turbo/server.py b/plugins/module_utils/turbo/server.py index 12727f8..05ec731 100644 --- a/plugins/module_utils/turbo/server.py +++ b/plugins/module_utils/turbo/server.py @@ -18,6 +18,7 @@ import pickle sys_path_lock = None +env_lock = None import ansible.module_utils.basic @@ -260,6 +261,7 @@ def _terminate(result): ( ansiblez_path, params, + env, ) = json.loads(content) if self.debug_mode: print( # pylint: disable=ansible-bad-function @@ -272,7 +274,10 @@ def _terminate(result): await embedded_module.load() try: - result = await embedded_module.run() + async with env_lock: + os.environ.clear() + os.environ.update(env) + result = await embedded_module.run() except SystemExit: backtrace = traceback.format_exc() result = {"msg": str(backtrace), "failed": True} @@ -338,6 +343,7 @@ def stop(self): if args.fork: fork_process() sys_path_lock = asyncio.Lock() + env_lock = asyncio.Lock() server = AnsibleVMwareTurboMode() server.socket_path = args.socket_path diff --git a/plugins/modules/turbo_demo.py b/plugins/modules/turbo_demo.py index 144e5b4..7ae9be5 100644 --- a/plugins/modules/turbo_demo.py +++ b/plugins/modules/turbo_demo.py @@ -56,6 +56,7 @@ def run_module(): result["changed"] = True result["message"] = get_message() result["counter"] = counter.i + result["envvar"] = os.environ.get("TURBO_TEST_VAR") if module._diff: result["diff"] = {"before": previous_value, "after": counter.i} diff --git a/tests/integration/targets/turbo_mode/tasks/main.yaml b/tests/integration/targets/turbo_mode/tasks/main.yaml index a7bf05e..1f01d3d 100644 --- a/tests/integration/targets/turbo_mode/tasks/main.yaml +++ b/tests/integration/targets/turbo_mode/tasks/main.yaml @@ -31,3 +31,21 @@ - assert: that: - _result_no_diff.diff is undefined + +- name: Test task environment var + cloud.common.turbo_demo: + environment: + TURBO_TEST_VAR: foobar + register: _result + +- assert: + that: + - _result.envvar == "foobar" + +- name: Test task environment var not set + cloud.common.turbo_demo: + register: _result + +- assert: + that: + - not _result.envvar diff --git a/tests/sanity/ignore-2.10.txt b/tests/sanity/ignore-2.10.txt index 70a6191..a41b318 100644 --- a/tests/sanity/ignore-2.10.txt +++ b/tests/sanity/ignore-2.10.txt @@ -61,4 +61,4 @@ plugins/module_utils/turbo/common.py future-import-boilerplate!skip plugins/module_utils/turbo/common.py import-2.6!skip plugins/module_utils/turbo/common.py import-2.7!skip plugins/module_utils/turbo/common.py import-3.5!skip -plugins/module_utils/turbo/common.py metaclass-boilerplate!skip \ No newline at end of file +plugins/module_utils/turbo/common.py metaclass-boilerplate!skip diff --git a/tests/sanity/ignore-2.11.txt b/tests/sanity/ignore-2.11.txt index e22ec79..72879cb 100644 --- a/tests/sanity/ignore-2.11.txt +++ b/tests/sanity/ignore-2.11.txt @@ -68,4 +68,4 @@ plugins/module_utils/turbo/common.py future-import-boilerplate!skip plugins/module_utils/turbo/common.py import-2.6!skip plugins/module_utils/turbo/common.py import-2.7!skip plugins/module_utils/turbo/common.py import-3.5!skip -plugins/module_utils/turbo/common.py metaclass-boilerplate!skip \ No newline at end of file +plugins/module_utils/turbo/common.py metaclass-boilerplate!skip diff --git a/tests/sanity/ignore-2.9.txt b/tests/sanity/ignore-2.9.txt index 70a6191..a41b318 100644 --- a/tests/sanity/ignore-2.9.txt +++ b/tests/sanity/ignore-2.9.txt @@ -61,4 +61,4 @@ plugins/module_utils/turbo/common.py future-import-boilerplate!skip plugins/module_utils/turbo/common.py import-2.6!skip plugins/module_utils/turbo/common.py import-2.7!skip plugins/module_utils/turbo/common.py import-3.5!skip -plugins/module_utils/turbo/common.py metaclass-boilerplate!skip \ No newline at end of file +plugins/module_utils/turbo/common.py metaclass-boilerplate!skip