Project Overview
The goal of this project is to automate sending notifications via email, WhatsApp, and SMS using Ansible. By leveraging Gmail and Twilio’s APIs, notifications can be sent across different channels from a centralized, automated system. This document outlines the steps for setting up and running the playbook for this purpose.
Objective
Send an email notification using Ansible.
Send a WhatsApp message using Twilio's API.
Send an SMS alert using Twilio's API.
Prerequisites
Before starting, ensure you have the following in place:
Ansible installed on your control machine.
Gmail Account with an app-specific password (configured for email notifications).
Twilio Account to send WhatsApp and SMS messages.
Python installed on the control machine.
Step-by-Step Guide
Step 1: Prepare the Inventory File
Since this playbook will run locally, the inventory file should specify localhost. Create a file named hosts
with the following content:
[localhost]
127.0.0.1
Step 2: Set Up Gmail for Email Notifications
Enable Two-Factor Authentication (2FA) on your Gmail account.
Generate an app-specific password for Ansible in your Gmail account settings. This password will be used for secure SMTP access in the playbook.
Step 3: Twilio Setup for SMS and WhatsApp Notifications
Sign up for a Twilio account at Twilio.
Obtain your Account SID and Auth Token from the Twilio Console.
Verify your phone number in Twilio for SMS.
Activate Twilio Sandbox for WhatsApp to enable WhatsApp notifications during development.
Step 4: Write the Ansible Playbook
Create a file called send_notifications.yml
with the following content:
---
- 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 Notification
Uses Ansible’s mail module to send an email through Gmail’s SMTP server.
Requires your Gmail email address and an app-specific password.
Task 2: Send WhatsApp Notification via Twilio
Sends a WhatsApp message through Twilio’s API using the
command
module to invoke acurl
command.Replaces placeholders with Twilio Account SID, Auth Token, and the recipient’s WhatsApp number.
Task 3: Send SMS Notification via Twilio
Similar to Task 2 but sends an SMS message using Twilio’s API.
Uses Twilio Account SID, Auth Token, and phone numbers for configuration.
Step 5: Run the Playbook
Once you’ve updated the playbook with your personal credentials, run it with the following command:
ansible-playbook -i hosts send_notifications.yml
Verification
After running the playbook, verify the following:
An email has been received in the specified inbox.
A WhatsApp message was sent to your WhatsApp number.
An SMS alert was received on your phone.
Ansible Playbook Breakdown
Email Task: Uses Gmail SMTP to send an email. Ansible’s
mail
module simplifies the process by handling the SMTP connection and security.WhatsApp Task: Sends a WhatsApp message using Twilio’s API via a
curl
command. This approach works in Twilio’s sandbox mode, which is ideal for development and testing.SMS Task: Uses Twilio’s API to send an SMS message. The process is similar to sending a WhatsApp message but requires a regular phone number instead.
Benefits of Using Ansible for Notifications
Centralized Management: Manage notifications (email, WhatsApp, SMS) from one Ansible playbook.
Automation: Ensure timely communication with automated, recurring notifications.
Cross-Channel Integration: Reach users across different channels (email, WhatsApp, SMS) from the same playbook.
Effortless Scalability: Automate notifications at any scale, from a few messages to hundreds.
Error-Free Communication: Automation reduces manual errors, ensuring accurate and reliable messaging.
Conclusion
This project demonstrates how to create an Ansible playbook that automates notifications via Gmail and Twilio APIs for email, WhatsApp, and SMS alerts. By integrating Ansible with these third-party services, you can set up an efficient, scalable, and easy-to-maintain communication system.