#!/usr/bin/env bash # PreToolUse hook for Bash: forces a permission prompt before any command that # can reach or change a live host. The ask rules in .claude/settings.json match # the command text as Claude usually writes it; this catches the same commands # when they are wrapped (sh -c '...', env VAR=x ..., absolute paths). set -euo pipefail input=$(cat) cmd=$(jq -r '.tool_input.command // empty' <<<"$input") [ -z "$cmd" ] && exit 0 [[ $cmd =~ ^ansible[[:space:]]+--version[[:space:]]*$ ]] && exit 0 # A tool name must start a word. "/" is allowed before it to catch absolute # paths, "-" is not, so the folder name homelab-ansible doesn't match. start='(^|[^[:alnum:]_.-])' end="([[:space:]\"')]|\$)" ansible_re="${start}(ansible-playbook|ansible-pull|ansible-console|ansible-vault|ansible)[[:space:]]" remote_re="${start}(ssh|scp|sftp|rsync)[[:space:]]" proxmox_re="${start}(pct|qm|pvesh|pvesm|vzdump)[[:space:]]" docker_re="${start}docker([[:space:]]+compose)?[[:space:]]+([^|;&]*[[:space:]])?(up|down|start|stop|restart|kill|rm|rmi|run|exec|pull|push|create|build|prune|cp|update|rename|pause|unpause|volume|network|system)$end" what="" if [[ $cmd =~ $ansible_re ]]; then what="${BASH_REMATCH[2]}" elif [[ $cmd =~ $remote_re ]]; then what="${BASH_REMATCH[2]} to a host" elif [[ $cmd =~ $proxmox_re ]]; then what="${BASH_REMATCH[2]} on Proxmox" elif [[ $cmd =~ $docker_re ]]; then what="docker ${BASH_REMATCH[4]}" fi [ -z "$what" ] && exit 0 reason="Live infrastructure: '$what'. Approve only if you want Claude to run this instead of running it yourself." jq -n --arg reason "$reason" '{ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "ask", permissionDecisionReason: $reason } }'