From f926b7eb0d7bfe84e799c60bf371297fc32bda26 Mon Sep 17 00:00:00 2001 From: dvirlabs <114520947+dvirlabs@users.noreply.github.com> Date: Wed, 22 Apr 2026 21:50:39 +0300 Subject: [PATCH] Update drift-check to let the pipeline update also what files changed --- ansible/playbooks/drift-check.yml | 1 - update-gitops-status.sh | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ansible/playbooks/drift-check.yml b/ansible/playbooks/drift-check.yml index 463bf0c..707876d 100644 --- a/ansible/playbooks/drift-check.yml +++ b/ansible/playbooks/drift-check.yml @@ -116,7 +116,6 @@ - name: Output structured list of drifted files for GitOps status server ansible.builtin.debug: msg: "DRIFTED_FILES={{ drifted_files | join(',') }}" - when: drift_detected - name: Output sync status marker for parsing ansible.builtin.debug: diff --git a/update-gitops-status.sh b/update-gitops-status.sh index cf0a108..0f65a96 100644 --- a/update-gitops-status.sh +++ b/update-gitops-status.sh @@ -97,29 +97,34 @@ if [ "$DRIFT_RC" -eq 0 ]; then else SYNC_STATUS="OUT_OF_SYNC" echo " ✗ Status: OUT OF SYNC - configuration drift detected" +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 +if grep -q "DRIFTED_FILES=" "$PLAYBOOK_LOG"; then + 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) - # Extract structured drifted files from playbook output - # The drift-check.yml playbook outputs: DRIFTED_FILES=file1,file2,file3 - if grep -q "DRIFTED_FILES=" "$PLAYBOOK_LOG"; then - DRIFTED_FILES_STR=$(grep "DRIFTED_FILES=" "$PLAYBOOK_LOG" | head -1 | sed 's/.*DRIFTED_FILES=//' | sed 's/\x1b\[[0-9;]*m//g') - + if [ -n "$DRIFTED_FILES_STR" ]; then # Parse comma-separated list into array IFS=',' read -ra CHANGED_FILES <<<"$DRIFTED_FILES_STR" - # Clean up whitespace and convert paths + # Clean up whitespace for i in "${!CHANGED_FILES[@]}"; do CHANGED_FILES[$i]=$(echo "${CHANGED_FILES[$i]}" | xargs) # Convert full paths to relative paths for cleaner output - # /etc/rsyslog.conf -> rsyslog.conf - # /etc/rsyslog.d/30-lab.conf -> rsyslog.d/30-lab.conf if [[ "${CHANGED_FILES[$i]}" == /etc/rsyslog.conf ]]; then CHANGED_FILES[$i]="rsyslog.conf" elif [[ "${CHANGED_FILES[$i]}" == /etc/rsyslog.d/* ]]; then CHANGED_FILES[$i]=$(echo "${CHANGED_FILES[$i]}" | sed 's|^/etc/||') fi - echo " - Drift detected in: ${CHANGED_FILES[$i]}" + if [ "$SYNC_STATUS" = "OUT_OF_SYNC" ]; then + echo " - Drift detected in: ${CHANGED_FILES[$i]}" + fi done DRIFT_COUNT=${#CHANGED_FILES[@]}