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 <[email protected]>
        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 <[email protected]>
        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 <[email protected]>, John Cairins <[email protected]>
        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. The file needs to be on the

    - 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 <[email protected]>
        subject: Change log
        body: "{{ lookup('file', '/var/www/release.txt') }}"
    
    
  • Sending an HTML email

    To send an HTML mail, you would need to use the “subtype” option. By default, the mail module sends emails as Text.

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

 

Always encrypt your email username and password!

 

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.

 

If you are planning to use the module, in production have a read on the Ansible tips and practices.

 

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

I’m a passionate engineer based in London.
Currently, I’m working as a Cloud Consultant at Contino.

Aside my full time job, I either work on my own startup projects or you will see me in a HIIT class 🙂