diff --git a/.woodpecker.yml b/.woodpecker.yml index becb23e..fbc0acf 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -109,6 +109,10 @@ steps: # Increase file descriptor limit for Ansible (max safe value) ulimit -n 65536 + # Disable Ansible callbacks to reduce file watchers and prevent inotify exhaustion + export ANSIBLE_CALLBACKS_ENABLED="" + export ANSIBLE_GATHERING=explicit + # Install dependencies: curl for HTTP requests, jq for JSON formatting apk add --no-cache curl jq > /dev/null 2>&1 @@ -161,6 +165,10 @@ steps: # Increase file descriptor limit for Ansible (max safe value) ulimit -n 65536 + # Disable Ansible callbacks to reduce file watchers and prevent inotify exhaustion + export ANSIBLE_CALLBACKS_ENABLED="" + export ANSIBLE_GATHERING=explicit + # Install dependencies: curl for HTTP requests, jq for JSON formatting apk add --no-cache curl jq bash > /dev/null 2>&1 @@ -189,6 +197,8 @@ steps: # Read the generated JSON or re-run drift check echo "==> Verifying drift status for pipeline result..." set +e + ANSIBLE_CALLBACKS_ENABLED="" \ + ANSIBLE_GATHERING=explicit \ ansible-playbook -i ansible/inventory/hosts.yml ansible/playbooks/drift-check.yml > /dev/null 2>&1 DRIFT_RC=$? set -e diff --git a/ansible/playbooks/drift-check.yml b/ansible/playbooks/drift-check.yml index c0023a0..9cb69b9 100644 --- a/ansible/playbooks/drift-check.yml +++ b/ansible/playbooks/drift-check.yml @@ -112,12 +112,13 @@ # ───────────────────────────────────────────────────────────────────────── # Debug output: Show structured drifted files for parsing - # Format: DRIFTED_FILES: file1, file2, file3 + # Format: DRIFTED_FILES=file1,file2,file3 (or empty if no drift) # This makes it easy for update-gitops-status.sh to extract changed files + # ALWAYS output this line for reliable parsing, even when empty # ───────────────────────────────────────────────────────────────────────── - name: Output structured list of drifted files for GitOps status server ansible.builtin.debug: - msg: "DRIFTED_FILES={{ drifted_files | join(',') }}" + msg: "DRIFTED_FILES={{ drifted_files | join(',') if drifted_files | length > 0 else '' }}" - name: Output sync status marker for parsing ansible.builtin.debug: diff --git a/update-gitops-status.sh b/update-gitops-status.sh index 58a8d8c..c9f28de 100644 --- a/update-gitops-status.sh +++ b/update-gitops-status.sh @@ -64,7 +64,13 @@ echo "Step 1/4: Running drift-check playbook..." # Capture playbook output to a temp file for parsing PLAYBOOK_LOG=$(mktemp) -trap "rm -f $PLAYBOOK_LOG" EXIT +KEEP_LOG="${KEEP_PLAYBOOK_LOG:-false}" +if [ "$KEEP_LOG" = "true" ]; then + PLAYBOOK_LOG="./drift-check-output.log" + echo " Playbook output will be saved to: $PLAYBOOK_LOG" +else + trap "rm -f $PLAYBOOK_LOG" EXIT +fi # Run playbook (no -v flag to avoid file descriptor exhaustion in containers) # Exit code: 0 = synced, non-zero = drift detected (expected) @@ -80,8 +86,10 @@ DRIFT_RC=$? set -e # Show playbook output for debugging (compact) -echo "Playbook output:" -cat "$PLAYBOOK_LOG" | tail -20 +echo "Playbook output (last 25 lines):" +cat "$PLAYBOOK_LOG" | tail -25 +echo "" +echo "DEBUG: Full playbook output saved to: $PLAYBOOK_LOG" echo "" # ───────────────────────────────────────────────────────────────────────────────── @@ -105,16 +113,25 @@ fi # Extract structured drifted files from playbook output # The drift-check.yml playbook outputs: DRIFTED_FILES=file1,file2,file3 # Search for the pattern in the output +echo " DEBUG: Searching for DRIFTED_FILES in playbook output..." if grep -q "DRIFTED_FILES=" "$PLAYBOOK_LOG"; then + echo " DEBUG: Found DRIFTED_FILES pattern" DRIFTED_FILES_STR=$(grep "DRIFTED_FILES=" "$PLAYBOOK_LOG" | tail -1) - # Remove ANSI color codes and extract the value - DRIFTED_FILES_STR=$(echo "$DRIFTED_FILES_STR" | sed 's/.*DRIFTED_FILES=//' | sed 's/\x1b\[[0-9;]*m//g' | xargs) + echo " DEBUG: Raw line: $DRIFTED_FILES_STR" - if [ -n "$DRIFTED_FILES_STR" ]; then + # Remove ANSI color codes and extract the value + # Handle both formats: "DRIFTED_FILES=..." and "msg": "DRIFTED_FILES=..." + DRIFTED_FILES_STR=$(echo "$DRIFTED_FILES_STR" | sed 's/.*DRIFTED_FILES=//' | sed 's/\x1b\[[0-9;]*m//g' | sed 's/".*$//' | xargs) + echo " DEBUG: Extracted value: '$DRIFTED_FILES_STR'" + + # Check if the value is an empty list ([] or empty string) + if [ -n "$DRIFTED_FILES_STR" ] && [ "$DRIFTED_FILES_STR" != "[]" ] && [ "$DRIFTED_FILES_STR" != "" ]; then # Parse comma-separated list into array IFS=',' read -ra CHANGED_FILES <<<"$DRIFTED_FILES_STR" - # Clean up whitespace + echo " DEBUG: Parsed ${#CHANGED_FILES[@]} files" + + # Clean up whitespace and normalize paths for i in "${!CHANGED_FILES[@]}"; do CHANGED_FILES[$i]=$(echo "${CHANGED_FILES[$i]}" | xargs) @@ -131,7 +148,24 @@ if grep -q "DRIFTED_FILES=" "$PLAYBOOK_LOG"; then done DRIFT_COUNT=${#CHANGED_FILES[@]} + else + echo " DEBUG: DRIFTED_FILES is empty or []" fi +else + echo " DEBUG: DRIFTED_FILES not found in playbook output" + echo " DEBUG: Attempting to parse from changed task output..." + + # Fallback: Look for "changed:" indicators in the playbook output + if grep -q "changed: \[" "$PLAYBOOK_LOG"; then + echo " DEBUG: Found changed tasks, but no structured DRIFTED_FILES output" + echo " DEBUG: This might indicate a playbook output format issue" + fi +fi + +# Additional validation: If OUT_OF_SYNC but no files found, show warning +if [ "$SYNC_STATUS" = "OUT_OF_SYNC" ] && [ "$DRIFT_COUNT" -eq 0 ]; then + echo " ⚠️ WARNING: Status is OUT_OF_SYNC but no drifted files were extracted" + echo " ⚠️ This might indicate a parsing issue. Check the playbook output above." fi echo " Total drift count: $DRIFT_COUNT"