This setup lets you run your Ansible playbook automatically (for example, via a daily cron job) and receive notifications from your ntfy.sh server about the status.
To do this, the we can use the callback plugin Ansible feature.
You can run the playbook and set the required environment variables however you like.
I chose to do it in a bash script ansible-playbook.sh, and run that script in my cron job.
⚙️ ✅ Ansible Complete - 2 Changes
Changed Tasks:
[inventory_hostname] role1 : Task1
[inventory_hostname] role2 : Task2
Duration: 0:05:00
⚙️ ❌
Unreachable Hosts:
inventory_hostname
Duration: 0:04:20
- Place
ntfy_callback.pyinside thecallback_pluginsdirectory in your Ansible project. - Make sure the python
requestspackage is installed:- With a package manager:
sudo apt install python3-requests - Or with pip:
pip3 install requests
- With a package manager:
- By default, the plugin only sends notifications on changes or errors.
- To notify every run, remove lines 80–81 in
ntfy_callback.py.
- To notify every run, remove lines 80–81 in
- Enable the plugin either by:
- setting environment variable:
ANSIBLE_CALLBACKS_ENABLED="ntfy_callback" - adding to your playbook
ansible.cfgfile:
callbacks_enabled = ntfy_callback
- setting environment variable:
- Define ntfy environment variables according to your setup:
NTFY_URL="https://<ntfy.sh/topic>"
NTFY_USER="<myuser>"
NTFY_PASSWORD="<mypassword>"
A password is required. If you prefer not to use one, you can likely remove it from the script. I have not tested this.
- Optionally, define a log file:
LOG_FILE="/var/log/ansible/<playbook>.log"
- Make sure this directory exists and is writable by the user running
ansible-playbook. - It’s recommended to use logrotate to manage file sizes over time.