clean up ansible-pull logging and lock against concurrent runs

This commit is contained in:
2026-07-12 15:30:07 -05:00
parent 3067b21276
commit cccc72cd6f
3 changed files with 31 additions and 8 deletions
+10 -7
View File
@@ -4,21 +4,24 @@
. "${HOME}/.ansible-venv/bin/activate"
SCRIPT_NAME="$(basename "${0}")"
GIT_REPO_BASE="https://gitea.admin-a.hpetersenfamily.com/heath/ansible"
GIT_REPO="${GIT_REPO_BASE}.git"
LOG_FILE="/var/log/ansible-pull.sh.log"
LOG_DIR="/var/log/ansible-pull.sh"
LOG_FILE="${LOG_DIR}/ansible-pull.sh.log"
LOCK_FILE="/tmp/ansible-pull.sh.lock"
# - Redirect all further output to the log file and create a lock file
exec >>"${LOG_FILE}" 2>&1 9>"${LOCK_FILE}"
# - Redirect all further output to the log file
exec >>"${LOG_FILE}" 2>&1
# - Log that we've gotten this far
echo
echo "$(basename "${0}"): $(date "+%Y-%m-%d %H:%M:%S")"
echo "----------"
echo -n "$(basename "${0}"): $(date "+%Y-%m-%d %H:%M:%S")"
# - If we can't lock the lock file, don't proceed
if ! flock -n 9; then echo "ERROR - Another copy of ansible-pull.sh is already running! Exiting..." ; exit 1 ; fi
exec 9>"${LOCK_FILE}"
if ! flock -n 9 ; then echo " - ERROR - Another copy of ${SCRIPT_NAME} is already running! Exiting..." ; exit 1 ; fi
# - Do our work
echo
ansible-pull --only-if-changed --url "${GIT_REPO}" --checkout main "${@}"