Skip to content

Commit

Permalink
avoid key errors on environment access (ansible#72620)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoca authored Nov 17, 2020
1 parent ad4ddd8 commit 07248e5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/better_os_environ_access.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- avoid possible errors accessing os.environ by not assuming existance of variables.
2 changes: 1 addition & 1 deletion lib/ansible/executor/task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def start_connection(play_context, variables, task_uuid):
Starts the persistent connection
'''
candidate_paths = [C.ANSIBLE_CONNECTION_PATH or os.path.dirname(sys.argv[0])]
candidate_paths.extend(os.environ['PATH'].split(os.pathsep))
candidate_paths.extend(os.environ.get('PATH', '').split(os.pathsep))
for dirname in candidate_paths:
ansible_connection = os.path.join(dirname, 'ansible-connection')
if os.path.isfile(ansible_connection):
Expand Down
8 changes: 6 additions & 2 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2649,8 +2649,12 @@ def run_command(self, args, check_rc=False, close_fds=True, executable=None, dat
old_env_vals[key] = os.environ.get(key, None)
os.environ[key] = val
if path_prefix:
old_env_vals['PATH'] = os.environ['PATH']
os.environ['PATH'] = "%s:%s" % (path_prefix, os.environ['PATH'])
path = os.environ.get('PATH', '')
old_env_vals['PATH'] = path
if path:
os.environ['PATH'] = "%s:%s" % (path_prefix, path)
else:
os.environ['PATH'] = path_prefix

# If using test-module.py and explode, the remote lib path will resemble:
# /tmp/test_module_scratch/debug_dir/ansible/module_utils/basic.py
Expand Down

0 comments on commit 07248e5

Please sign in to comment.