The whole automation idea is to minimize or exclude humans from the process. However, you need to notify users about the progress, request to perform some out-of-reach activity, or just report the results. System administrators may be happy with text or even JSON bodies, but sometimes you should do it in style.

The opening post mentions email messages as one of the reporting tools.  It may be not the most reliable tool, yet it bears a few distinctive characteristics:

  • You can target email messages to a specific audience, with no overheads or additional integrations. Send to the named recipients or a distribution list and your report will be delivered to the right hands  
  • Users can consume reports literally everywhere. The ability to receive and send emails is one of the key characteristics of any "smart" device.
  • With some help from designers and little knowledge of the HTML, you can deliver messages suitable for stakeholders.
  • And the killer feature  - you can send attachments.

I have created a sample playbook that produces an HTML-formatted email with the Ansible logo and attachments.  It's a simplified and sanitized version of the real production playbook. Let's take a quick look at the main task.

- name: Send Email Report to Users
      mail:
        host: smtp.mydomain.com
        subject: " HTML-fromatted Report Example"
        body: "{{ lookup('template','templates/mail_body.html.j2') }}"
        from: "Ansible Host <ansible@mydomain.com>"
        to: "{{ to_emails }}"
        cc: "{{ cc_emails }}"
        attach: "{{ req_zip.stat.path|default([]) }}"
        headers: 'Reply-To=Do.Not.Reply@mydomain.com'
        subtype: html
        charset: utf8
Ansible mail task example

Like many other things in Ansible, you can comprehend it as easily as a regular text. Two attributes are important:

  • subtype -  It should be set to 'html', otherwise you will receive HTML source code.
  • charset   - set it to UTF-8 to make sure your HTML body will go through mail servers with no harm.

I know this is not a fine example of a good-looking e-mail, but it could be the start of your own reporting system.

Image depicts HTML document generated by Ansible playbook. It contains header, links and embeded Ansible logo.
E-mail body produced by template.