Skip to content

Commit

Permalink
cmtinv: Better parameter checking for playbooks
Browse files Browse the repository at this point in the history
Don't silently abort on non-existing playbooks;
tell the user why they were ignored instead,
and show how to list the valid playbooks.

Signed-off-by: David Weinehall <[email protected]>
  • Loading branch information
taotriad committed Dec 16, 2023
1 parent be6df25 commit 505d606
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion about.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
ADMIN_PROGRAM_VERSION = "0.8.3"

INVENTORY_PROGRAM_NAME = "cmtinv"
INVENTORY_PROGRAM_VERSION = "0.4.3"
INVENTORY_PROGRAM_VERSION = "0.4.4"

# We don't support Python-versions older than 3.8
version_info = sys.version_info
Expand Down
33 changes: 27 additions & 6 deletions cmtinv
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,8 @@ def list_playbooks(options: List[Tuple[str, str]], args: List[str]) -> int:
description = deep_get(data, DictPath("description"), "<unset>")
category = deep_get(data, DictPath("category"), "<unset>")
read_only = str(deep_get(data, DictPath("read_only"), False))
if category in ("__DISABLED__", "__INVALID__"):
continue
rows.append([name, description, category, read_only])
for row in rows:
for i, field in enumerate(row):
Expand Down Expand Up @@ -970,13 +972,32 @@ def run_playbook(options: List[Tuple[str, str]], args: List[str]) -> int:
sys.exit(errno.ENOENT)

playbooks = populate_playbooks()
if playbook in playbooks and deep_get(playbooks, DictPath(f"{playbook}#category"), "") not in ("__DISABLED__", "__INVALID__"):
playbook_path = deep_get(playbooks, DictPath(f"{playbook}#playbook"), "")
retval, ansible_results = ansible_run_playbook_on_selection(playbook_path, selection = selection, verbose = verbose)
if verbose:
ansithemeprint([ANSIThemeString("", "default")])
if playbook not in playbooks:
ansithemeprint([ANSIThemeString("Error", "error"),
ANSIThemeString(": “", "default"),
ANSIThemeString(f"{playbook}", "argument"),
ANSIThemeString("“ is not a valid playbook; use “", "default"),
ANSIThemeString(f"{about.INVENTORY_PROGRAM_NAME}", "programname"),
ANSIThemeString(" list-playbooks", "command"),
ANSIThemeString("“ to list valid playbooks.", "default")], stderr = True)
sys.exit(errno.ENOENT)

if deep_get(playbooks, DictPath(f"{playbook}#category"), "") in ("__DISABLED__", "__INVALID__"):
ansithemeprint([ANSIThemeString("Error", "error"),
ANSIThemeString(": The playbook “", "default"),
ANSIThemeString(f"{playbook}", "argument"),
ANSIThemeString("“ is either disabled or invalid; use “", "default"),
ANSIThemeString(f"{about.INVENTORY_PROGRAM_NAME}", "programname"),
ANSIThemeString(" list-playbooks", "command"),
ANSIThemeString("“ to list valid playbooks.", "default")], stderr = True)
sys.exit(errno.EINVAL)

playbook_path = deep_get(playbooks, DictPath(f"{playbook}#playbook"), "")
retval, ansible_results = ansible_run_playbook_on_selection(playbook_path, selection = selection, verbose = verbose)
if verbose:
ansithemeprint([ANSIThemeString("", "default")])

ansible_print_play_results(retval, ansible_results, verbose = verbose)
ansible_print_play_results(retval, ansible_results, verbose = verbose)

def ping(options: List[Tuple[str, str]], args: List[str]) -> int:
"""
Expand Down

0 comments on commit 505d606

Please sign in to comment.