Ansible playbook for host rebooted and started unexpectedly and send an alert

12 views
Skip to first unread message

akhta...@gmail.com

unread,
Oct 14, 2018, 4:56:30 PM10/14/18
to Ansible Project
Hi All, 

I am looking for ansible play book example to send alert to premetheus after a host has rebooted and re-started unexpeditly. Any help would be greatly appreciate it. 

Thanks 
Riaz

Brad Van Orden

unread,
Oct 15, 2018, 6:24:15 AM10/15/18
to Ansible Project
Not sure that is really an ansible task.  I'm sure you could use ansible to set it up.  I use this on my systems to get notification: 
cd /etc/systemd/system
if [[ ! rebootsh.service ]]; then
   cat > rebootsh.service << EOF
[Unit]
Description=Notify sys admin when the server gets rebooted
After=network.target
After=postfix.service

[Service]
Type=simple
ExecStart=/root/bin/reboot.sh
TimeoutStartSec=0
RemainAfterExit=no

[Install]
WantedBy=rescue.target
WantedBy=multi-user.target
WantedBy=graphical.target
EOF
   echo "Added the rebootsh service"
   chmod 444 rebootsh.service
   systemctl enable rebootsh.service
else
   echo "Rebootsh.service already in place"
fi

cd /root/bin
if [[ ! -f reboot.sh ]]; then
   cat > reboot.sh << EOF
#!/bin/bash
#
# Send an e-mail whenever the system starts.
#
DATE=$(/bin/date +'%d%b%y')
TIME=$(/bin/date +'%H:%M%:S')
HOST=$(/bin/hostname -s)
SUBJ="${HOST} has been booted on ${DATE} at ${TIME}"
BODY=$(/bin/mktemp)
echo ""                                       >  ${BODY}
echo "The last five logins were:"             >> ${BODY}
/usr/bin/who /var/log/wmtp | /usr/bin/tail -5 >> ${BODY}
#
/bin/mailx -s "${SUBJ}" ${NOTIFY} < ${BODY}
/bin/rm ${BODY}

You could use something similar I would believe?
Reply all
Reply to author
Forward
0 new messages