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

  • https://docs.ansible.com/ansible/latest/collections/community/general/mail_module.html

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.

``yaml

---

  • 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:

`bash

ansible-galaxy collection install community.general

`

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