Skip to content

Commit

Permalink
Fix collection installation bug when having custom config (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Dec 27, 2023
1 parent 45bef12 commit 443ae74
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/ansible_compat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ class AnsibleConfig(UserDict[str, object]): # pylint: disable=too-many-ancestor
default_private_role_vars: bool = False
default_remote_port: str | None = None
default_remote_user: str | None = None
# https://docs.ansible.com/ansible/latest/reference_appendices/config.html#collections-paths
default_collections_path: list[str] = [
"~/.ansible/collections",
"/usr/share/ansible/collections",
]
default_roles_path: list[str] = [
"~/.ansible/roles",
"/usr/share/ansible/roles",
Expand Down
34 changes: 5 additions & 29 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import shutil
import subprocess
import sys
import tempfile
import warnings
from collections import OrderedDict
from dataclasses import dataclass, field
Expand Down Expand Up @@ -456,13 +455,13 @@ def install_collection(
cmd.append(f"{collection}")

_logger.info("Running from %s : %s", Path.cwd(), " ".join(cmd))
run = self.run(
process = self.run(
cmd,
retry=True,
env={**self.environ, ansible_collections_path(): ":".join(cpaths)},
)
if run.returncode != 0:
msg = f"Command returned {run.returncode} code:\n{run.stdout}\n{run.stderr}"
if process.returncode != 0:
msg = f"Command returned {process.returncode} code:\n{process.stdout}\n{process.stderr}"
_logger.error(msg)
raise InvalidPrerequisiteError(msg)

Expand All @@ -472,30 +471,7 @@ def install_collection_from_disk(
destination: Path | None = None,
) -> None:
"""Build and install collection from a given disk path."""
if not self.version_in_range(upper="2.11"):
self.install_collection(path, destination=destination, force=True)
return
# older versions of ansible able unable to install without building
with tempfile.TemporaryDirectory() as tmp_dir:
cmd = [
"ansible-galaxy",
"collection",
"build",
"--output-path",
str(tmp_dir),
str(path),
]
_logger.info("Running %s", " ".join(cmd))
run = self.run(cmd, retry=False)
if run.returncode != 0:
_logger.error(run.stdout)
raise AnsibleCommandError(run)
for archive_file in os.listdir(tmp_dir):
self.install_collection(
str(Path(tmp_dir) / archive_file),
destination=destination,
force=True,
)
self.install_collection(path, destination=destination, force=True)

# pylint: disable=too-many-branches
def install_requirements( # noqa: C901
Expand Down Expand Up @@ -791,7 +767,7 @@ def _prepare_ansible_paths(self) -> None:

if library_paths != self.config.DEFAULT_MODULE_PATH:
self._update_env("ANSIBLE_LIBRARY", library_paths)
if collections_path != self.config.collections_paths:
if collections_path != self.config.default_collections_path:
self._update_env(ansible_collections_path(), collections_path)
if roles_path != self.config.default_roles_path:
self._update_env("ANSIBLE_ROLES_PATH", roles_path)
Expand Down

0 comments on commit 443ae74

Please sign in to comment.