Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add has_playbook method to Runtime #364

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class Runtime:
# to do it multiple tilmes will cause runtime warnings from within ansible-core
initialized: bool = False
plugins: Plugins
_has_playbook_cache: dict[tuple[str, Path | None], bool] = {}

def __init__(
self,
Expand Down Expand Up @@ -432,6 +433,28 @@ def version_in_range(
return False
return True

def has_playbook(self, playbook: str, *, basedir: Path | None = None) -> bool:
"""Return true if ansible can load a given playbook.

This is also used for checking if playbooks from within collections
are present and if they pass syntax check.
"""
if (playbook, basedir) in self._has_playbook_cache:
return self._has_playbook_cache[playbook, basedir]

proc = self.run(["ansible-playbook", "--syntax-check", playbook], cwd=basedir)
result = proc.returncode == 0
if not result:
if not basedir:
basedir = Path()
msg = f"has_playbook returned false for '{basedir / playbook}' due to syntax check returning {proc.returncode}"
logging.debug(msg)

# cache the result
self._has_playbook_cache[playbook, basedir] = result

return result

def install_collection(
self,
collection: str | Path,
Expand Down
13 changes: 13 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,16 @@ def test_get_galaxy_role_name_invalid() -> None:
"role_name": False, # <-- invalid data, should be string
}
assert _get_galaxy_role_name(galaxy_infos) == ""


def test_runtime_has_playbook() -> None:
"""Tests has_playbook method."""
runtime = Runtime(require_module=True)

assert not runtime.has_playbook("this-does-not-exist.yml")
# call twice to ensure cache is used:
assert not runtime.has_playbook("this-does-not-exist.yml")

assert not runtime.has_playbook("this-does-not-exist.yml", basedir=Path())
# this is part of community.molecule collection
assert runtime.has_playbook("community.molecule.validate.yml")
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ setenv =
PIP_DISABLE_PIP_VERSION_CHECK = 1
PIP_CONSTRAINT = {toxinidir}/.config/constraints.txt
PRE_COMMIT_COLOR = always
PYTEST_REQPASS = 93
PYTEST_REQPASS = 94
FORCE_COLOR = 1
allowlist_externals =
ansible
Expand Down
Loading