Skip to content

Commit

Permalink
Add has_playbook method to Runtime
Browse files Browse the repository at this point in the history
Add functionality for detecting if Ansible can find specific
playbooks, caching the results. This is useful as they can reside
inside collections.

Related: ansible/ansible-lint#4141
  • Loading branch information
ssbarnea committed May 8, 2024
1 parent 0fc3578 commit 552dcc5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
21 changes: 21 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,26 @@ 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:
msg = f"has_playbook returned false for '{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-exit.yml")
# call twice to ensure cache is used:
assert not runtime.has_playbook("this-does-not-exit.yml")

assert not runtime.has_playbook("this-does-not-exit.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

0 comments on commit 552dcc5

Please sign in to comment.