Skip to content

Commit

Permalink
cron: rewrite ansible-daily-run script
Browse files Browse the repository at this point in the history
  • Loading branch information
rpelisse committed Apr 12, 2024
1 parent ab5ebe5 commit 3884281
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions roles/cron/templates/ansible-daily-run.sh.j2
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
#!/bin/bash
set -eo pipefail

readonly LOGFILE=$(mktemp)
trap "rm -f ${LOGFILE}" EXIT
readonly ANSIBLE_LOCAL_REPO=${ANSIBLE_LOCAL_REPO:-'/etc/ansible'}
readonly GIT_BRANCH=${GIT_BRANCH:-'main'}
readonly GIT_REMOTE=${GIT_REMOTE:-'origin'}
readonly ZEUS_STATUS_FILE=${ZEUS_STATUS_FILE:-'/etc/ansible/zeus_daily_run_status.txt'}

rm "${ZEUS_STATUS_FILE}"
touch "${ZEUS_STATUS_FILE}"

readonly LOGFILE="$(mktemp)"
echo "Ansible Daily run on Olympus" > "${LOGFILE}"

cd /etc/ansible
git pull origin olympus >> "${LOGFILE}"
# TODO: update /etc/ansible/vars too
zeus_status() {
if [ -s "${ZEUS_STATUS_FILE}" ]; then
cat "${ZEUS_STATUS_FILE}"
else
echo "Ansible did not ran"
fi
}

set +e
ansible-playbook -D /etc/ansible/zeus.yml 2>&1 >> "${LOGFILE}"
echo "${?}" > /etc/ansible/zeus_daily_run_status.txt
set -e
mail_report() {
local subject=$(head -1 "${LOGFILE}" | sed -e "s;$; (Status: $(zeus_status));")
cat "${LOGFILE}" | \
mailx -r "{{ mailer.to }}" -s "{{ jobs.name }}:${subject}" \
-S smtp="{{ mailer.smtp.host }}:{{mailer.smtp.port }}" "{{ mailer.replyTo }}"
}

run_zeus() {
echo "Set working directory ${ANSIBLE_LOCAL_REPO}."
cd /etc/ansible
echo "Update local Zeus repository ($(pwd))"
git pull "${GIT_REMOTE}" "${GIT_BRANCH}"
ansible-playbook -D /etc/ansible/zeus.yml 2>&1
echo "${?}" > "${ZEUS_STATUS_FILE}"
}

trap mail_report EXIT
trap "rm -f ${LOGFILE}" EXIT

readonly SUBJECT=$(tail -2 "${LOGFILE}" | sed '/^$/d' | head -1)
cat "${LOGFILE}" | \
mailx -r "{{ mailer.to }}" -s "{{ jobs.name }}:${SUBJECT}" -S smtp="{{ mailer.smtp.host }}:{{mailer.smtp.port }}" "{{ mailer.replyTo }}"
run_zeus >> "${LOGFILE}"

0 comments on commit 3884281

Please sign in to comment.