avoid key errors on environment access (#72620)

This commit is contained in:
Brian Coca 2020-11-17 12:09:46 -05:00 committed by GitHub
parent ad4ddd8211
commit 07248e5ec1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- avoid possible errors accessing os.environ by not assuming existance of variables.

View file

@ -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):

View file

@ -2649,8 +2649,12 @@ class AnsibleModule(object):
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