Skip to content

Commit 8c57b06

Browse files
committed
Fix collection installation bug when having custom config
1 parent 45bef12 commit 8c57b06

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

src/ansible_compat/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ class AnsibleConfig(UserDict[str, object]): # pylint: disable=too-many-ancestor
216216
default_private_role_vars: bool = False
217217
default_remote_port: str | None = None
218218
default_remote_user: str | None = None
219+
# https://docs.ansible.com/ansible/latest/reference_appendices/config.html#collections-paths
220+
default_collections_path: list[str] = [
221+
"~/.ansible/collections",
222+
"/usr/share/ansible/collections",
223+
]
219224
default_roles_path: list[str] = [
220225
"~/.ansible/roles",
221226
"/usr/share/ansible/roles",

src/ansible_compat/runtime.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import shutil
1111
import subprocess
1212
import sys
13-
import tempfile
1413
import warnings
1514
from collections import OrderedDict
1615
from dataclasses import dataclass, field
@@ -456,13 +455,13 @@ def install_collection(
456455
cmd.append(f"{collection}")
457456

458457
_logger.info("Running from %s : %s", Path.cwd(), " ".join(cmd))
459-
run = self.run(
458+
process = self.run(
460459
cmd,
461460
retry=True,
462461
env={**self.environ, ansible_collections_path(): ":".join(cpaths)},
463462
)
464-
if run.returncode != 0:
465-
msg = f"Command returned {run.returncode} code:\n{run.stdout}\n{run.stderr}"
463+
if process.returncode != 0:
464+
msg = f"Command returned {process.returncode} code:\n{process.stdout}\n{process.stderr}"
466465
_logger.error(msg)
467466
raise InvalidPrerequisiteError(msg)
468467

@@ -472,30 +471,8 @@ def install_collection_from_disk(
472471
destination: Path | None = None,
473472
) -> None:
474473
"""Build and install collection from a given disk path."""
475-
if not self.version_in_range(upper="2.11"):
476-
self.install_collection(path, destination=destination, force=True)
477-
return
478-
# older versions of ansible able unable to install without building
479-
with tempfile.TemporaryDirectory() as tmp_dir:
480-
cmd = [
481-
"ansible-galaxy",
482-
"collection",
483-
"build",
484-
"--output-path",
485-
str(tmp_dir),
486-
str(path),
487-
]
488-
_logger.info("Running %s", " ".join(cmd))
489-
run = self.run(cmd, retry=False)
490-
if run.returncode != 0:
491-
_logger.error(run.stdout)
492-
raise AnsibleCommandError(run)
493-
for archive_file in os.listdir(tmp_dir):
494-
self.install_collection(
495-
str(Path(tmp_dir) / archive_file),
496-
destination=destination,
497-
force=True,
498-
)
474+
# breakpoint()
475+
self.install_collection(path, destination=destination, force=True)
499476

500477
# pylint: disable=too-many-branches
501478
def install_requirements( # noqa: C901
@@ -791,7 +768,7 @@ def _prepare_ansible_paths(self) -> None:
791768

792769
if library_paths != self.config.DEFAULT_MODULE_PATH:
793770
self._update_env("ANSIBLE_LIBRARY", library_paths)
794-
if collections_path != self.config.collections_paths:
771+
if collections_path != self.config.default_collections_path:
795772
self._update_env(ansible_collections_path(), collections_path)
796773
if roles_path != self.config.default_roles_path:
797774
self._update_env("ANSIBLE_ROLES_PATH", roles_path)

0 commit comments

Comments
 (0)