From 8ec2c261bda9672795950864ddc35088e114dd4c Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Sun, 14 May 2023 05:19:38 +0000 Subject: [PATCH] add helper to respawn modules with system python The seboolean, selinux, firewalld, and firewalld_info modules depend on system bindings that are only available for the default system python interpreter. ansible-core is not packaged for the default system python interpreter on RHEL 8 and 9. When automatic interpreter discovery does not occur (e.g. when using implicit localhost [1]), ansible-core will not use the system interpreter to run ansible modules and the aforementioned modules will not work even if the bindings are installed. The RHEL ansible-core maintainers as well as the EPEL ansible and ansible-collection-* package maintainers (inc. me) have gotten multiple bug reports about this. We have been telling people to fix their setup to use the correct Python interpreter. Fortunately, ansible-core 2.11 and above have a module utility that'll respawn modules to use the correct system interpreter. [1] https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html --- plugins/module_utils/_respawn.py | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 plugins/module_utils/_respawn.py diff --git a/plugins/module_utils/_respawn.py b/plugins/module_utils/_respawn.py new file mode 100644 index 0000000..55abaf5 --- /dev/null +++ b/plugins/module_utils/_respawn.py @@ -0,0 +1,45 @@ +# Copyright (c) 2023 Maxwell G +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +""" +Helpers to respawn a module to run using the system interpreter +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +try: + from ansible.module_utils.common import respawn +except ImportError: + HAS_RESPAWN_UTIL = False +else: + HAS_RESPAWN_UTIL = True + + +SYSTEM_PYTHON_INTERPRETERS = ( + "/usr/bin/libexec/platform-python", + "/usr/bin/python3", + "/usr/bin/python2", + "/usr/bin/python", +) + + +def respawn_module(module): + """ + Respawn an ansible module to using the first interpreter in + SYSTEM_PYTHON_INTERPRETERS that contains `module`. + + Args: + module (str): Name of python module to search for + + Returns: + Returns None if the module cannot be respawned. + """ + if respawn.has_respawned(): + return + interpreter = respawn.probe_interpreters_for_module( + SYSTEM_PYTHON_INTERPRETERS, module + ) + if interpreter: + respawn.respawn_module(interpreter)