Skip to content

Instantly share code, notes, and snippets.

@NQevxvEtg
Last active June 9, 2026 17:50
Show Gist options
  • Select an option

  • Save NQevxvEtg/3ff2a791ff55456dcea644ed470ca46b to your computer and use it in GitHub Desktop.

Select an option

Save NQevxvEtg/3ff2a791ff55456dcea644ed470ca46b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Exit immediately if any command fails
set -e
# --- CONFIGURATION ---
FAILED_LIST="failed_servers.txt"
ENV_DIR="./environments"
OUTPUT_FILE="classified_failed_servers.txt"
TEMP_FILE="matched_raw.tmp"
# --- VALIDATION ---
if [ ! -f "$FAILED_LIST" ]; then
echo "Error: Failed server list '$FAILED_LIST' not found." >&2
exit 1
fi
if [ ! -d "$ENV_DIR" ]; then
echo "Error: Environments directory '$ENV_DIR' not found." >&2
exit 1
fi
echo "=========================================="
echo " Starting YAML Server Classification"
echo "=========================================="
# Clean up any leftover temp or output files from previous runs
rm -f "$TEMP_FILE" "$OUTPUT_FILE"
echo -n "Processing environment files... "
# --- PROCESSING ---
for env_file in "$ENV_DIR"/*.yml "$ENV_DIR"/*.yaml; do
# Ensure the glob actually found files
[ -e "$env_file" ] || continue
[ -d "$env_file" ] && continue
env_name=$(basename "$env_file")
# 1. Clean the YAML lines (strip spaces, hyphens, colons, and structural keys)
# 2. Grep for exact matches against the failed list
# 3. Append the environment filename to the matched host
awk '
{
gsub(/^[ \t-]+/, ""); # Strip leading spaces and hyphens
gsub(/:.*/, ""); # Strip trailing colons and anything after them
gsub(/\047/, ""); # Safely strip single quotes using octal code
gsub(/\042/, ""); # Safely strip double quotes using octal code
# Skip empty lines or top-level YAML structural keys
if ($0 == "" || $0 == "all" || $0 == "hosts" || $0 == "hostnames") {
next;
}
print $0
}' "$env_file" | \
grep -Fxf "$FAILED_LIST" | \
awk -v fname="$env_name" '{print $0 " , " fname}' >> "$TEMP_FILE" || true
done
echo "Done."
# --- SORTING & CLEANUP ---
if [ -f "$TEMP_FILE" ] && [ -s "$TEMP_FILE" ]; then
echo "Sorting and saving results to '$OUTPUT_FILE'..."
# Sort naturally (-V) based on the server name
sort -V "$TEMP_FILE" -o "$OUTPUT_FILE"
rm -f "$TEMP_FILE"
echo "=========================================="
echo " Success! Process complete."
echo " Total classified lines: $(wc -l < "$OUTPUT_FILE")"
echo "=========================================="
else
echo "=========================================="
echo " Finished: No matching servers found."
echo "=========================================="
rm -f "$TEMP_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment