From a5ce34307fe28fd4dd227f4537b6052efc4f1d3e Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 10 Mar 2025 11:35:10 -0500 Subject: [PATCH] [stable-2.18] Limit respawn to supported python versions (#83662) (#84769) * Limit respawn to supported python versions (cherry picked from commit 00067f1) --- changelogs/fragments/respawn-min-python.yml | 2 ++ lib/ansible/module_utils/common/respawn.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/respawn-min-python.yml diff --git a/changelogs/fragments/respawn-min-python.yml b/changelogs/fragments/respawn-min-python.yml new file mode 100644 index 00000000000..400f9587f97 --- /dev/null +++ b/changelogs/fragments/respawn-min-python.yml @@ -0,0 +1,2 @@ +bugfixes: +- module respawn - limit to supported Python versions diff --git a/lib/ansible/module_utils/common/respawn.py b/lib/ansible/module_utils/common/respawn.py index 0f57c154576..25500f7be06 100644 --- a/lib/ansible/module_utils/common/respawn.py +++ b/lib/ansible/module_utils/common/respawn.py @@ -4,11 +4,14 @@ from __future__ import annotations import os +import pathlib import subprocess import sys from ansible.module_utils.common.text.converters import to_bytes +_ANSIBLE_PARENT_PATH = pathlib.Path(__file__).parents[3] + def has_respawned(): return hasattr(sys.modules['__main__'], '_respawned') @@ -54,11 +57,20 @@ def probe_interpreters_for_module(interpreter_paths, module_name): be returned (or ``None`` if probing fails for all supplied paths). :arg module_name: fully-qualified Python module name to probe for (eg, ``selinux``) """ + PYTHONPATH = os.getenv('PYTHONPATH', '') + env = os.environ | {'PYTHONPATH': f'{_ANSIBLE_PARENT_PATH}:{PYTHONPATH}'.rstrip(': ')} for interpreter_path in interpreter_paths: if not os.path.exists(interpreter_path): continue try: - rc = subprocess.call([interpreter_path, '-c', 'import {0}'.format(module_name)]) + rc = subprocess.call( + [ + interpreter_path, + '-c', + f'import {module_name}, ansible.module_utils.basic', + ], + env=env, + ) if rc == 0: return interpreter_path except Exception: