Ansible send email notifications with examples

In this article, you will learn how to send emails using Ansible. With emails being one of the most used source communication, it can be important to use them in a project. This is why the mail module provides several options that can make it easy for you.

You’re probably thinking: Why would I send a mail using? a provisioning software?

Well, here are my three favorite use cases:

  • After an installation of a software.
  • Successful git pulls.
  • Informing certain people, when a certain job has been complete.
  • To be used as a notification in your Continous Deployment!

Examples of Ansible Mail Module:

Sending a mail using SMTP (Mandrill, Mailjet, SendGrid etc…)

- name: Send a success email
  mail:
   host: smtp.mandrillapp.com
   port: 587 
   username: 28283aeebd83616c6
   password: 0432eb4224e406
   to: Adam Shields <adam@gmail.com>
   subject: Installation Update
   body: 'The installation is complete.'

You could send emails using different mail providers such as Mandrill, Mailjet or Sendgrid. All you need to do is replace the mail configs.

Sending a mail with an attachment

You can attach multiple files by leaving a space between them. The files must exist on the controller (the machine which the ansible task is executed).

- name: Send the latest report
  mail:
   host: smtp.mandrillapp.com
   port: 587 
   username: 28283aeebd83616c6
   password: 0432eb4224e406
   to: Adam Shields <adam@gmail.com>
   subject: Installation Update
   body: 'Attached is the report of the changes that is live'
   attach: /var/www/reports/latest.csv

Sending an email to multiple users

To send a mail to multiple users you would need to separate them by a comma.

- name: Sending email to multiple recipients
  mail:
   host: smtp.mandrillapp.com
   port: 587 
   username: 28283aeebd83616c6
   password: 0432eb4224e406
   to: Adam Shields <adam@gmail.com>, John Cairins <john@outlook.com>
   subject: Overview of the changes
   body: 'Change 1205 is live.'

Reading contents of a file and including in the email.

Sometimes, you may want to include the content of a file in the message body.
You would need to read the content of the file, using the “lookup” command and include it in the message body.

- mail:
 - name: Sending contents of a file in the email body
   mail:
    host: smtp.mandrillapp.com
    port: 587 
    username: 28283aeebd83616c6
    password: 0432eb4224e406
    to: Adam Shields <adam@gmail.com>
    subject: Change log
    body: "{{ lookup('file', '/var/www/release.txt') }}"

Sending an HTML email

- name: Sending a HTML email
   mail:
    host: smtp.mandrillapp.com
    port: 587
    username: 28283aeebd83616c6
    password: 0432eb4224e406
    to: Adam Shields <adam@gmail.com>
    subject: Overview of the changes
    subtype: html
    body: '
<h1>Change log</h1>
'

There are many more options, which make’s this module very flexible.
Here are some of them:

  • cc: To include recipients in the cc header.
  • bcc: You could use this option, to include recipients in the BCC header.
  • timeout: The default timeout is 20 seconds, but if you feel it needs to be longer or shorter, you could use this option to modify it.
  • header: To add a customized header, this option can be used.

The complete list can be found on the mail module page.

To view the code examples please visit the git repo: https://github.com/infinitypp/ansible-mail-module-examples