Multi-Channel Notification Playbook: Ansible Automation for Email, WhatsApp, and SMS Messaging

Multi-Channel Notification Playbook: Ansible Automation for Email, WhatsApp, and SMS Messaging

Ansible playbook to send email, WhatsApp, and SMS messages. Let's create a playbook that demonstrates how to send messages through these three channels.

---
- name: Send messages via email, WhatsApp, and SMS
  hosts: localhost
  connection: local
  gather_facts: false

  vars:
    email_recipient: "recipient@example.com"
    whatsapp_number: "+1234567890"
    sms_number: "+1987654321"
    message: "Hello from Ansible! This is a test message."

  tasks:
    - name: Send email
      community.general.mail:
        host: smtp.gmail.com
        port: 587
        username: "your_email@gmail.com"
        password: "your_email_password"
        to: "{{ email_recipient }}"
        subject: "Ansible Notification"
        body: "{{ message }}"
      delegate_to: localhost

    - name: Send WhatsApp message
      uri:
        url: "https://graph.facebook.com/v13.0/YOUR_WHATSAPP_PHONE_NUMBER_ID/messages"
        method: POST
        body_format: json
        body:
          messaging_product: "whatsapp"
          to: "{{ whatsapp_number }}"
          type: "text"
          text:
            body: "{{ message }}"
        headers:
          Authorization: "Bearer YOUR_WHATSAPP_API_TOKEN"
          Content-Type: "application/json"
      delegate_to: localhost

    - name: Send SMS
      twilio:
        account_sid: "YOUR_TWILIO_ACCOUNT_SID"
        auth_token: "YOUR_TWILIO_AUTH_TOKEN"
        from_number: "YOUR_TWILIO_PHONE_NUMBER"
        to_number: "{{ sms_number }}"
        body: "{{ message }}"
      delegate_to: localhost

This playbook demonstrates how to send messages using email, WhatsApp, and SMS. Here's a breakdown of each task:

  1. Email:

    • Uses the community.general.mail module to send an email.

    • You'll need to replace the SMTP server details, username, and password with your own email provider's information.

  2. WhatsApp:

    • Uses the uri module to send a POST request to the WhatsApp Business API.

    • You'll need to replace YOUR_WHATSAPP_PHONE_NUMBER_ID and YOUR_WHATSAPP_API_TOKEN with your actual WhatsApp Business API credentials.

  3. SMS:

    • Uses the twilio module to send an SMS.

    • You'll need to replace YOUR_TWILIO_ACCOUNT_SID, YOUR_TWILIO_AUTH_TOKEN, and YOUR_TWILIO_PHONE_NUMBER with your Twilio account details.

To use this playbook:

  1. Make sure you have the necessary Ansible collections installed:

     ansible-galaxy collection install community.general
    
  2. Install the required Python libraries:

     pip install twilio
    
  3. Replace the placeholder values in the playbook with your actual credentials and API tokens.

  4. Save the playbook to a file (e.g., send_messages.yml).

  5. Run the playbook:

     ansible-playbook send_messages.yml
    

Remember to keep your API tokens and credentials secure and never share them publicly. It's a good practice to use Ansible Vault or environment variables to manage sensitive information.