avoid key errors on environment access (#72620)
This commit is contained in:
parent
ad4ddd8211
commit
07248e5ec1
3 changed files with 9 additions and 3 deletions
2
changelogs/fragments/better_os_environ_access.yml
Normal file
2
changelogs/fragments/better_os_environ_access.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- avoid possible errors accessing os.environ by not assuming existance of variables.
|
|
@ -1020,7 +1020,7 @@ def start_connection(play_context, variables, task_uuid):
|
||||||
Starts the persistent connection
|
Starts the persistent connection
|
||||||
'''
|
'''
|
||||||
candidate_paths = [C.ANSIBLE_CONNECTION_PATH or os.path.dirname(sys.argv[0])]
|
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:
|
for dirname in candidate_paths:
|
||||||
ansible_connection = os.path.join(dirname, 'ansible-connection')
|
ansible_connection = os.path.join(dirname, 'ansible-connection')
|
||||||
if os.path.isfile(ansible_connection):
|
if os.path.isfile(ansible_connection):
|
||||||
|
|
|
@ -2649,8 +2649,12 @@ class AnsibleModule(object):
|
||||||
old_env_vals[key] = os.environ.get(key, None)
|
old_env_vals[key] = os.environ.get(key, None)
|
||||||
os.environ[key] = val
|
os.environ[key] = val
|
||||||
if path_prefix:
|
if path_prefix:
|
||||||
old_env_vals['PATH'] = os.environ['PATH']
|
path = os.environ.get('PATH', '')
|
||||||
os.environ['PATH'] = "%s:%s" % (path_prefix, os.environ['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:
|
# If using test-module.py and explode, the remote lib path will resemble:
|
||||||
# /tmp/test_module_scratch/debug_dir/ansible/module_utils/basic.py
|
# /tmp/test_module_scratch/debug_dir/ansible/module_utils/basic.py
|
||||||
|
|
Loading…
Reference in a new issue