From 82e4b469f6096a35c5a983377190a30bae6f44d4 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 10 Apr 2025 12:14:24 -0700 Subject: [PATCH] respawn: Update ENV dict copy with PYTHONPATH value (#84962) * Use shallow copy of os.environ to update PYTHONPATH value instead of using '|' operator Fixes: #84954 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/respawn_os_env.yml | 3 +++ lib/ansible/module_utils/common/respawn.py | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/respawn_os_env.yml diff --git a/changelogs/fragments/respawn_os_env.yml b/changelogs/fragments/respawn_os_env.yml new file mode 100644 index 00000000000..fb54fad8dbe --- /dev/null +++ b/changelogs/fragments/respawn_os_env.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - respawn - use copy of env variables to update existing PYTHONPATH value (https://github.com/ansible/ansible/issues/84954). diff --git a/lib/ansible/module_utils/common/respawn.py b/lib/ansible/module_utils/common/respawn.py index 4b47777337d..d16815b9a17 100644 --- a/lib/ansible/module_utils/common/respawn.py +++ b/lib/ansible/module_utils/common/respawn.py @@ -56,10 +56,13 @@ def probe_interpreters_for_module(interpreter_paths, module_name): :arg interpreter_paths: iterable of paths to Python interpreters. The paths will be probed in order, and the first path that exists and can successfully import the named module will be returned (or ``None`` if probing fails for all supplied paths). - :arg module_name: fully-qualified Python module name to probe for (eg, ``selinux``) + :arg module_name: fully-qualified Python module name to probe for (for example, ``selinux``) """ PYTHONPATH = os.getenv('PYTHONPATH', '') - env = os.environ | {'PYTHONPATH': f'{_ANSIBLE_PARENT_PATH}:{PYTHONPATH}'.rstrip(': ')} + env = os.environ.copy() + env.update({ + 'PYTHONPATH': f'{_ANSIBLE_PARENT_PATH}:{PYTHONPATH}'.rstrip(': ') + }) for interpreter_path in interpreter_paths: if not os.path.exists(interpreter_path): continue