Automated Notification System with Ansible, email, sms and whatsapp

Automated Notification System with Ansible, email, sms and whatsapp

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

  1. Send an email notification using Ansible.

  2. Send a WhatsApp message using Twilio's API.

  3. Send an SMS alert using Twilio's API.


Prerequisites

Before starting, ensure you have the following in place:

  1. Ansible installed on your control machine.

  2. Gmail Account with an app-specific password (configured for email notifications).

  3. Twilio Account to send WhatsApp and SMS messages.

  4. 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

  1. Enable Two-Factor Authentication (2FA) on your Gmail account.

  2. 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

  1. Sign up for a Twilio account at Twilio.

  2. Obtain your Account SID and Auth Token from the Twilio Console.

  3. Verify your phone number in Twilio for SMS.

  4. 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

  1. 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.

  2. Task 2: Send WhatsApp Notification via Twilio

    • Sends a WhatsApp message through Twilio’s API using the command module to invoke a curl command.

    • Replaces placeholders with Twilio Account SID, Auth Token, and the recipient’s WhatsApp number.

  3. 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

  1. Centralized Management: Manage notifications (email, WhatsApp, SMS) from one Ansible playbook.

  2. Automation: Ensure timely communication with automated, recurring notifications.

  3. Cross-Channel Integration: Reach users across different channels (email, WhatsApp, SMS) from the same playbook.

  4. Effortless Scalability: Automate notifications at any scale, from a few messages to hundreds.

  5. 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.