Skip to content

Commit

Permalink
Re-init plugin loader after collections installation (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ирина Розет committed Oct 14, 2024
1 parent 16538ab commit f0d03a1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class Runtime:
initialized: bool = False
plugins: Plugins
_has_playbook_cache: dict[tuple[str, Path | None], bool] = {}
require_module: bool = False

def __init__(
self,
Expand Down Expand Up @@ -218,6 +219,7 @@ def __init__(
msg = f"Found incompatible version of ansible runtime {self.version}, instead of {min_required_version} or newer."
raise RuntimeError(msg)
if require_module:
self.require_module = True
self._ensure_module_available()

# pylint: disable=import-outside-toplevel
Expand Down Expand Up @@ -339,23 +341,28 @@ def _ensure_module_available(self) -> None:
# https://github.com/ansible/ansible-lint/issues/2945
if not Runtime.initialized:
col_path = [f"{self.cache_dir}/collections"]
# noinspection PyProtectedMember
from ansible.utils.collection_loader._collection_finder import ( # pylint: disable=import-outside-toplevel
_AnsibleCollectionFinder,
)
if self.version >= Version("2.15.0.dev0"):
# pylint: disable=import-outside-toplevel,no-name-in-module
from ansible.plugins.loader import init_plugin_loader

_AnsibleCollectionFinder( # noqa: SLF001
paths=col_path,
)._remove() # pylint: disable=protected-access
init_plugin_loader(col_path)
else:
# noinspection PyProtectedMember
from ansible.utils.collection_loader._collection_finder import ( # pylint: disable=import-outside-toplevel
_AnsibleCollectionFinder,
)

# noinspection PyProtectedMember
# pylint: disable=protected-access
col_path += self.config.collections_paths
col_path += os.path.dirname( # noqa: PTH120
os.environ.get(ansible_collections_path(), "."),
).split(":")
_AnsibleCollectionFinder(
paths=col_path,
)._remove()
_AnsibleCollectionFinder( # noqa: SLF001
paths=col_path,
)._install() # pylint: disable=protected-access
Expand Down Expand Up @@ -622,6 +629,9 @@ def install_requirements( # noqa: C901
if result.returncode != 0:
_logger.error(result.stderr)
raise AnsibleCommandError(result)
if self.require_module:
Runtime.initialized = False
self._ensure_module_available()

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

0 comments on commit f0d03a1

Please sign in to comment.