As I'm using ansible to set cron tasks and as I want to avoid having the same tasks running exactly at the same time on all my server, I'm looking for an elegant solution to generate cron "randomly" by keeping the indempotency when I re-run my ansible role.
I've made this working script :
#!/bin/bash
if [ $# -lt 1 ]
then
echo "Syntax error : at least one argument is needed"
echo "Syntax: $0 string [ -hmbf ]"
echo "-h : display only the hour"
echo "-m : display only the minute"
echo "-b : display both hour and minute"
echo "-f : display both hour and minute in cron format"
exit
fi
case $2 in
-h|--hour)
onlyhour=true
;;
-m|--minute)
onlyminute=true
;;
-b|--both)
bothhourminute=true
;;
-f|--full)
fullcron=true
;;
*)
# unknown option
;;
esac
n=$(md5sum <<< "$1")
number=$((0x${n%% *}))
number=${number#-}
position=0
while [ $position -lt ${#number} ]; do
onenumber=${number:$position:1}
twonumber=${number:$position:2}
if [[ $onenumber -ge 0 && $onenumber -le 6 && -z ${hour+x} ]]; then hour=$onenumber; fi
if [[ $twonumber -ge 0 && $twonumber -le 59 && -z ${minute+x} ]]; then minute=$twonumber; fi
let position=position+1
if [[ -n "$hour" && -n "$minute" ]]
then
if [ "$onlyhour" = true ] ; then
echo $hour
exit 0
elif [ "$onlyminute" = true ] ; then
echo $minute
exit 0
elif [ "$fullcron" = true ] ; then
echo $minute $hour '* * *'
exit 0
else
echo $minute $hour
exit 0
fi
fi
done
The idea is to call the script and give the server name as argument.
I 'm nevertheless not convinced by my own solution. Is there a better way to do it ? I'm looking for something directly availbale in Ansible integrated)
Aucun commentaire:
Enregistrer un commentaire