Skip to content

Commit

Permalink
Re-init plugin loader after collections installation (ansible#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ирина Розет committed Oct 7, 2024
1 parent 16538ab commit cac8c1a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def load_collections(self) -> None:
path=path,
)

def _ensure_module_available(self) -> None:
def _ensure_module_available(self, reload = False) -> None:
"""Assure that Ansible Python module is installed and matching CLI version."""
ansible_release_module = None
with contextlib.suppress(ModuleNotFoundError, ImportError):
Expand All @@ -337,12 +337,14 @@ def _ensure_module_available(self) -> None:

# For ansible 2.15+ we need to initialize the plugin loader
# https://github.com/ansible/ansible-lint/issues/2945
if not Runtime.initialized:
if not Runtime.initialized or reload:
col_path = [f"{self.cache_dir}/collections"]
if self.version >= Version("2.15.0.dev0"):
# pylint: disable=import-outside-toplevel,no-name-in-module
from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
from ansible.plugins.loader import init_plugin_loader

_AnsibleCollectionFinder._remove()
init_plugin_loader(col_path)
else:
# noinspection PyProtectedMember
Expand Down Expand Up @@ -622,6 +624,7 @@ def install_requirements( # noqa: C901
if result.returncode != 0:
_logger.error(result.stderr)
raise AnsibleCommandError(result)
self._ensure_module_available(reload=True)

# pylint: disable=too-many-locals
def prepare_environment( # noqa: C901
Expand Down

0 comments on commit cac8c1a

Please sign in to comment.