Ansible Pilot

Streamlining Notifications with Ansible: Sending Email Reports

Simplify Notifications: Seamlessly Integrate Gmail for Effortless Communication with community.general.mail module.

August 17, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Introduction

Automation has become the cornerstone of efficient IT operations in today’s fast-paced technological landscape. Ansible, a powerful open-source automation tool, empowers organizations to simplify complex tasks and workflows. One such task is sending email notifications, an essential component of keeping stakeholders informed about various processes and events within a system. In this article, we’ll explore a practical example of how to use Ansible to send email reports using a playbook.

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

The Ansible Playbook: Sending Email Reports

The provided Ansible playbook snippet demonstrates how to send email reports using the community.general.mail module. This module offers a straightforward way to interact with email servers and send notifications from within an Ansible playbook.

---
- name: Send email
  hosts: localhost
  tasks:
    - name: Send email report
      community.general.mail:
        host: smtp.gmail.com
        port: 587
        username: [email protected]
        password: mysecret
        to: "First Last <[email protected]>"
        subject: Ansible-Playbook Report
        body: Ansible Playbook notify
      delegate_to: localhost

The playbook snippet consists of three primary sections:

  1. Play Definition: The playbook begins with defining the play’s name and target hosts. In this case, the play is executed on the localhost.

  2. Task Definition: Inside the play, there’s a task defined under the tasks section. This task is named “Send email report” and utilizes the community.general.mail module to send an email. Please note that we need the community.general collection installed in our system:

ansible-galaxy collection install community.general
  1. Module Configuration: The Ansible module includes various parameters to send the email. These parameters include the SMTP server’s hostname (smtp.gmail.com), port (587), sender’s email ([email protected]), sender’s password (mysecret), recipient’s email ([email protected]), email subject (Ansible-Playbook Report), and the email body (Ansible Playbook notify).

The delegate_to parameter specifies that the task should be executed on the localhost.

Understanding the Use Case

This Ansible playbook snippet is designed to send an email report, which can have multiple applications:

Configuration and Customization

To use this playbook, you need to replace the placeholder values with your actual information:

Running the Playbook Once you’ve customized the playbook to your needs, save it in a mail.yml file and execute it using the ansible-playbook command:

ansible-playbook mail.yml

Ansible will execute the playbook tasks and utilize the community.general.mail module to send the email report to the specified recipient.

Conclusion

The provided Ansible playbook snippet showcases how automation can simplify sending email notifications. By leveraging Ansible’s capabilities, you can enhance organizational communication, keep stakeholders informed, and streamline crucial operational workflows. Whether it’s deployment notifications, error alerts, or task completion reports, Ansible empowers you to manage notifications efficiently and effectively, contributing to a more responsive and informed IT environment.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases