Automating Notifications with Ansible: Email, WhatsApp, and SMS
In today’s interconnected world, effective communication is key. As a DevOps engineer or system administrator, you often need to send notifications about system status, deployment results, or critical alerts. Ansible, a powerful automation tool, can help streamline this process. In this blog post, we’ll create an Ansible playbook that sends notifications via email, WhatsApp, and SMS.
Prerequisites
Before we begin, make sure you have:
- Ansible installed on your control node
- Access to an SMTP server for sending emails
- A Twilio account for sending SMS and WhatsApp messages
- Python
requests
library installed for WhatsApp integration
The Playbook Structure
We’ll create a playbook named send_notifications.yml
with the following structure:
---
- name: Send Notifications
hosts: localhost
vars_files:
- vars/credentials.yml
tasks:
- name: Send Email Notification
# Email task here
- name: Send SMS Notification
# SMS task here
- name: Send WhatsApp Notification
# WhatsApp task here
Sending Email Notifications
For sending emails, we’ll use Ansible’s built-in mail
module:
- name: Send Email Notification
mail:
host: smtp.gmail.com
port: 587
username: "{{ email_username }}"
password: "{{ email_password }}"
to: recipient@example.com
subject: "Ansible Notification"
body: "This is a test email sent from Ansible."
delegate_to: localhost
Sending SMS Notifications
For SMS, we’ll use the Twilio module:
- name: Send SMS Notification
twilio:
account_sid: "{{ twilio_account_sid }}"
auth_token: "{{ twilio_auth_token }}"
from_number: "{{ twilio_phone_number }}"
to_number: "+1234567890"
msg: "This is a test SMS sent from Ansible."
Sending WhatsApp Notifications
Ansible doesn’t have a built-in module for WhatsApp, so we’ll use the uri
module to interact with the Twilio API:
- name: Send WhatsApp Notification
uri:
url: "https://api.twilio.com/2010-04-01/Accounts/{{ twilio_account_sid }}/Messages.json"
method: POST
user: "{{ twilio_account_sid }}"
password: "{{ twilio_auth_token }}"
body_format: form-urlencoded
body:
From: "whatsapp:{{ twilio_whatsapp_number }}"
To: "whatsapp:+1234567890"
Body: "This is a test WhatsApp message sent from Ansible."
status_code: 201
register: whatsapp_response
Securing Credentials
To keep our credentials secure, we’ll store them in a separate file vars/credentials.yml
:
email_username: your_email@gmail.com
email_password: your_email_password
twilio_account_sid: your_twilio_account_sid
twilio_auth_token: your_twilio_auth_token
twilio_phone_number: your_twilio_phone_number
twilio_whatsapp_number: your_twilio_whatsapp_number
Make sure to encrypt this file using ansible-vault
:
ansible-vault encrypt vars/credentials.yml
Running the Playbook
To run the playbook:
ansible-playbook send_notifications.yml --ask-vault-pass
Conclusion
With this Ansible playbook, you can now easily send notifications across multiple channels. This can be incredibly useful for:
- Alerting on system failures or critical events
- Notifying team members about deployment status
- Sending regular reports or updates
Remember to customize the message content and recipient details based on your specific use case. You can also extend this playbook to include conditions, so notifications are only sent when certain criteria are met.By leveraging Ansible’s automation capabilities, you’ve created a powerful, multi-channel notification system that can be easily integrated into your existing workflows and playbooks.
So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.
Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”
Connect me on Social Media: https://linktr.ee/mdshamsfiroz
Happy coding! Happy learning!