Automating Notifications with Ansible: Sending Emails, WhatsApp, and SMS Messages
In the world of automation, there’s a growing need for communication through multiple channels, such as email, WhatsApp, and SMS, to notify users about updates or events. With Ansible, automating these notifications becomes straightforward and powerful. This blog demonstrates how to build an Ansible playbook that sends notifications via email, WhatsApp, and SMS.
Project Overview
The objective of this project is to:
- Send an email notification using Ansible.
- Send a WhatsApp message.
- Send an SMS alert using available APIs.
We’ll use third-party services like Gmail for emails, Twilio for WhatsApp and SMS messages, and Python scripts for integration. Let’s dive into how to set up this system.
Prerequisites
Before you start, ensure the following:
- Ansible is installed on your control machine.
- A Gmail account with app-specific password for email notifications.
- A Twilio account for WhatsApp and SMS messages.
- Python installed on your control machine.
Step-by-Step Guide
1. Prepare the Inventory File
This file lists the server(s) where the playbook will run. Since this is a notification task, you will mostly run this playbook locally, so the inventory will look like this:
[localhost]
127.0.0.1
2. Set Up Gmail for Email Notifications
To use Gmail’s SMTP for sending emails, you need to generate an app-specific password for Ansible. You can do this by enabling two-factor authentication and generating an app password in your Gmail account settings.
3. Twilio Setup for SMS and WhatsApp Notifications
- Sign up for a Twilio account at Twilio.
- Get your Account SID and Auth Token.
- For SMS, you need to verify your phone number in Twilio.
- For WhatsApp, activate Twilio’s sandbox mode for sending WhatsApp messages.
4. Write the Ansible Playbook
Below is the Ansible playbook that handles email, WhatsApp, and SMS notifications.
---
- name: Notification Automation Playbook
hosts: localhost
tasks:
# Task 1: Send Email via Gmail SMTP
- name: Send Email Notification
mail:
host: smtp.gmail.com
port: 587
username: 'your_email@gmail.com'
password: 'your_app_password'
to: 'recipient_email@gmail.com'
subject: "Ansible Email Notification"
body: "This is an automated email notification sent using Ansible."
secure: starttls
# Task 2: Send WhatsApp Message via Twilio
- name: Send WhatsApp Notification
command: >
curl -X POST https://api.twilio.com/2010-04-01/Accounts/{{ TWILIO_ACCOUNT_SID }}/Messages.json
--data-urlencode "Body=This is an automated WhatsApp message sent using Ansible."
--data-urlencode "From=whatsapp:+14155238886"
--data-urlencode "To=whatsapp:+your_phone_number"
-u {{ TWILIO_ACCOUNT_SID }}:{{ TWILIO_AUTH_TOKEN }}
# Task 3: Send SMS Message via Twilio
- name: Send SMS Notification
command: >
curl -X POST https://api.twilio.com/2010-04-01/Accounts/{{ TWILIO_ACCOUNT_SID }}/Messages.json
--data-urlencode "Body=This is an automated SMS message sent using Ansible."
--data-urlencode "From=+your_twilio_phone_number"
--data-urlencode "To=+your_phone_number"
-u {{ TWILIO_ACCOUNT_SID }}:{{ TWILIO_AUTH_TOKEN }}
Explanation of the Playbook
Task 1: Send Email via Gmail SMTP:
- This task uses Ansible’s
mail
module to send an email via Gmail’s SMTP server. You need to replaceyour_email@gmail.com
with your actual email, and use the app-specific password instead of the regular Gmail password.
Task 2: Send WhatsApp Notification via Twilio:
- Here we use Twilio’s API to send a WhatsApp message. The
command
module is used to send an HTTP POST request usingcurl
. Replace the placeholders with your Twilio Account SID, Auth Token, and phone numbers.
Task 3: Send SMS Notification via Twilio:
- Similar to the WhatsApp task, this task sends an SMS message using Twilio’s API. You will also need to provide your Twilio credentials and phone numbers.
5. Run the Playbook
Once you have updated the playbook with your own credentials, run it using the following command:
ansible-playbook -i hosts send_notifications.yml
6. Verify the Notifications
Once the playbook completes, verify that:
- You have received an email notification in your inbox.
- A WhatsApp message has been sent to your specified WhatsApp number.
- An SMS alert has been sent to your phone.
Ansible Playbook Breakdown
- Email Task: Uses Gmail’s SMTP server to send an email. The
mail
module simplifies the process by handling SMTP communication in the background. - WhatsApp Task: Sends a WhatsApp message using Twilio’s API by invoking a
curl
command. Twilio’s sandbox feature allows you to send WhatsApp messages during development. - SMS Task: Uses the same Twilio API as the WhatsApp task but sends a regular SMS message.
Benefits of Using Ansible for Notifications
- Centralized Management: With Ansible, you can manage all types of notifications (emails, WhatsApp, and SMS) from a single playbook, making it easy to modify or scale.
- Automation: Automating notifications ensures timely communication, whether for alerts, updates, or confirmations, without the need for manual intervention.
- Cross-Channel Integration: Ansible allows you to send messages across different channels (email, WhatsApp, SMS) from the same playbook, providing flexibility in reaching users.
- Effortless Scalability: Whether you’re sending a few messages or hundreds, Ansible allows you to automate this process, making it easy to scale notifications.
- Error-Free Communication: Automating notifications reduces the chances of human error, ensuring that all messages are sent to the right recipients with the correct content.
Conclusion
In this blog, we demonstrated how to create an Ansible playbook to send automated notifications via email, WhatsApp, and SMS. By integrating Ansible with Gmail and Twilio APIs, you can quickly set up a communication system that is scalable, efficient, and easy to maintain.