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.
Links
The Ansible Playbook: Sending Email Reports
The provided Ansible playbook snippet Playbooknstrates 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:
Play Definition: The playbook begins with defining the play’s name and target hosts. In this case, the play is executed on the
localhost
.Task Definition: Inside the play, there’s a task defined under the
tasks
section. This task is named “Send email report” and utilizes thecommunity.general.mail
module to send an email. Please note that we need thecommunity.general
collection installed in our system:
ansible-galaxy collection install community.general
- 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:
Deployment Notifications: In a continuous integration/continuous deployment (CI/CD) pipeline, this playbook can be used to notify team members about the successful deployment of a new version.
Error Alerts: By integrating this playbook into your error-handling process, you can automatically send notifications when critical errors occur in your system.
Task Completion Reports: Long-running tasks or batch processes can be configured to send a notification upon completion.
Configuration and Customization
To use this playbook, you need to replace the placeholder values with your actual information:
[email protected]
andmysecret
: Replace these with your Gmail account credentials or an app-specific password.First Last <[email protected]>
: Replace this with the recipient’s name and email address.Ansible-Playbook Report
andAnsible Playbook notify
: Customize the subject and body of the email to match your specific use case.
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 Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate