Skip to content

Instantly share code, notes, and snippets.

@JeffreyVdb
Created March 26, 2025 11:58
Show Gist options
  • Save JeffreyVdb/351c0ec28fbf232c2e8f26433ef7ad93 to your computer and use it in GitHub Desktop.
Save JeffreyVdb/351c0ec28fbf232c2e8f26433ef7ad93 to your computer and use it in GitHub Desktop.
Summary of tfchanges using a plan file and jq
#!/bin/bash
# Exit immediately if any command fails.
set -e
# Treat any non-zero exit status in a pipeline as an error.
set -o pipefail
# Make unset variables an error.
set -u
# --- Configuration ---
# The path to the Terraform plan file. Change this if needed.
PLAN_FILE="/tmp/plan.out"
# --- Functions ---
# Get ANSI color codes using tput (most portable method).
get_color() {
case "$1" in
red) tput setaf 1 ;;
green) tput setaf 2 ;;
yellow) tput setaf 3 ;;
cyan) tput setaf 6 ;;
blue) tput setaf 4 ;; # Keep this, might use it elsewhere in future.
white) tput setaf 7 ;;
reset) tput sgr0 ;;
*)
echo "Invalid color name: $1" >&2
exit 1
;;
esac
}
# --- Main Script ---
# Check if the plan file exists.
if [[ ! -f "$PLAN_FILE" ]]; then
echo "Error: Plan file not found: $PLAN_FILE" >&2
exit 1
fi
# Check that terraform is installed
if ! command -v terraform &>/dev/null; then
echo "terraform could not be found"
exit
fi
#Check jq is installed.
if ! command -v jq &>/dev/null; then
echo "jq could not be found"
exit
fi
# --- Generate the detailed, colorized output ---
terraform show -json "$PLAN_FILE" | jq -r --arg red "$(get_color red)" --arg green "$(get_color green)" --arg yellow "$(get_color yellow)" --arg cyan "$(get_color cyan)" --arg white "$(get_color white)" --arg reset "$(get_color reset)" '
.resource_changes[] |
select(.change.actions | any(. != "no-op")) |
[
(
if .change.actions | any(. == "create") and any(. == "delete") then ($cyan + "±" + $reset)
elif .change.actions | any(. == "create") then ($green + "+" + $reset)
elif .change.actions | any(. == "delete") then ($red + "-" + $reset)
elif .change.actions | any(. == "update") then ($yellow + "~" + $reset)
else ($white + "." + $reset)
end
),
"|",
.address
] |
join(" ")
'
# --- Calculate and display the summary ---
summary=$(terraform show -json "$PLAN_FILE" | jq -r --arg red "$(get_color red)" --arg green "$(get_color green)" --arg yellow "$(get_color yellow)" --arg reset "$(get_color reset)" '
reduce .resource_changes[] as $change (
{ create: 0, update: 0, delete: 0 }; # Initial state
. as $counts | # Store the current state in $counts
$change.change.actions as $actions |
if $actions | any(. == "create") and any(. == "delete") then .create += 1 | .delete += 1
elif $actions | any(. == "create") then .create += 1
elif $actions | any(. == "delete") then .delete += 1
elif $actions | any(. == "update") then .update += 1
else .
end
) |
[
("Added: " + ($green + (.create | tostring) + $reset)),
("Changed: " + ($yellow + (.update | tostring) + $reset)), # Changed to yellow
("Deleted: " + ($red + (.delete | tostring) + $reset))
] |
.[]
')
# Display Summary
echo "----------"
echo "$summary"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment