From 186404829d4311c816af0d1ce05a6dd831a28240 Mon Sep 17 00:00:00 2001 From: Sergey Putko Date: Thu, 30 Jan 2025 10:40:47 +0200 Subject: [PATCH] ansible_mitogen: Fix dnf module by patching include of dnf.cli (#1230) * fix dnf module import * add changelog --- ansible_mitogen/planner.py | 18 ++++++++++++++++++ docs/changelog.rst | 1 + 2 files changed, 19 insertions(+) diff --git a/ansible_mitogen/planner.py b/ansible_mitogen/planner.py index 6490afce..2915f4b7 100644 --- a/ansible_mitogen/planner.py +++ b/ansible_mitogen/planner.py @@ -615,6 +615,23 @@ def _fix_py35(invocation, module_source): invocation._overridden_sources[invocation.module_path] = module_source +def _fix_dnf(invocation, module_source): + """ + Handles edge case where dnf ansible module showed failure due to a missing import in the dnf module. + Specifically addresses errors like "Failed loading plugin 'debuginfo-install': module 'dnf' has no attribute 'cli'". + https://github.com/mitogen-hq/mitogen/issues/1143 + This issue is resolved by adding 'dnf.cli' to the import statement in the module source. + This works in vanilla Ansible but not in Mitogen otherwise. + """ + if invocation.module_name in {'ansible.builtin.dnf', 'ansible.legacy.dnf', 'dnf'} and \ + invocation.module_path not in invocation._overridden_sources: + module_source = module_source.replace( + b"import dnf\n", + b"import dnf, dnf.cli\n" + ) + invocation._overridden_sources[invocation.module_path] = module_source + + def _load_collections(invocation): """ Special loader that ensures that `ansible_collections` exist as a module path for import @@ -652,6 +669,7 @@ def invoke(invocation): module_source = invocation.get_module_source() _fix_py35(invocation, module_source) + _fix_dnf(invocation, module_source) _planner_by_path[invocation.module_path] = _get_planner( invocation, module_source diff --git a/docs/changelog.rst b/docs/changelog.rst index 16f082f7..016b960f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -35,6 +35,7 @@ In progress (unreleased) * :gh:issue:`1213` :mod:`ansible_mitogen`: Return ``stderr_lines`` from ``_low_level_execute_command()`` * :gh:issue:`1227` tests: Name transport_config tests that use ``mitogen_via`` +* :gh:issue:`1143` :mod:`ansible_mitogen`: Fix dnf module include for dnf.cli v0.3.21 (2025-01-20)