Skip to content

Commit

Permalink
Pass environment variables through task execution (#69)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: None <None>
Reviewed-by: Mike Graves <[email protected]>
  • Loading branch information
gravesm authored Jul 27, 2021
1 parent 62a2cd2 commit b4ea34d
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/69-pass-envvar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- pass current task's environment through to execution (https://github.com/ansible-collections/cloud.common/pull/69).
1 change: 1 addition & 0 deletions plugins/module_utils/turbo/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion plugins/module_utils/turbo/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pickle

sys_path_lock = None
env_lock = None

import ansible.module_utils.basic

Expand Down Expand Up @@ -260,6 +261,7 @@ def _terminate(result):
(
ansiblez_path,
params,
env,
) = json.loads(content)
if self.debug_mode:
print( # pylint: disable=ansible-bad-function
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/turbo_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/targets/turbo_mode/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/sanity/ignore-2.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
plugins/module_utils/turbo/common.py metaclass-boilerplate!skip
2 changes: 1 addition & 1 deletion tests/sanity/ignore-2.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
plugins/module_utils/turbo/common.py metaclass-boilerplate!skip
2 changes: 1 addition & 1 deletion tests/sanity/ignore-2.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
plugins/module_utils/turbo/common.py metaclass-boilerplate!skip

0 comments on commit b4ea34d

Please sign in to comment.