Send Email From Linux and Windows Servers Using Ansible

Introduction

In this article, we will see how to send email using Ansible. Emails are usually send using the Simple Mail Transfer Protocol (SMTP).  

Ansible provides a module for Linux to send email.  For windows, we need to use the powershell command as module is not available. You should have the correct SMTP server details while setting up the email client. Lets see in detail how to send mail in Linux and Windows systems.

Linux

We have an Ansible module named mail for sending an email.
Below is an example for the mail module. Make sure to update the SMTP server name, port number and email addresses.

- mail:
   host: smtpservername.domain.com	
   port: 25
   username: user@domain.com
   password: mypass
   subject: Report
   body: The compliance report for all app servers
   from: john@domain.com
   to: sam.top@domain.com, peter.lord@domain.com
   cc: jim.door@domain.com
   attach: /var/report.csv
   charset: utf8

Only if SMTP requires authentication, add the username and password. If not, you can ignore both of these parameters. Make sure to add the password in the vault and encrypt it. Do not add the password as plain text.
Refer Ansible Vault Guide

Windows

Ansible doesn't have a mail module for windows. You have to use the powershell command usingwin_shell  to achieve this. Below is an example.

Make sure to update the snmp server details, port number and email addresses.

win_shell: |
 $smtp_server="smtpservername.domain.com"
 $email_from="admin@domain.com"
 $email_to="john.lord@domain.com"
 $email_sub="Report"
 $email_body="The compliance report for all app servers"
 $SMTPClient= New-Object Net.Mail.SmtpClient($smtp_server)
 $email_msg= New-Object System.Net.Mail.MailMessage
 $email_msg.From= $email_from
 $email_msg.Subject= $email_sub
 $email_msg.Body= $email_body
 $email_msg.To.Add($email_to)
 $SMTPClient.Port=25 
 $SMTPClient.Send($email_msg)

Troubleshooting Issues

In case you face any issues during the execution from ansible, try to execute the command from the windows server manually via powershell.

Below are few errors while executing in the powershell.

Error 1: The SMTP server requires a secure connection or the client was not authenticated. The server response was: Must issue a STARTTLS command first

Error 2: Server does not support secure connections.

Error 3: Mailbox name not allowed. The server response was: Relaying disallowed as admin@domain.com

Solution: These errors may be due to incorrect port number,  authentication issues on smtp server or SSL issues. You might need to add few more parameters for SMTP authentication. Below is an example where authentication is required.

win_Shell:
 $smtp_server="smtp.domain.in"
 $email_from="admin@domain.com"
 $email_to="john.lord@domain.com"
 $email_sub="Report"
 $email_body="The compliance report for all app servers"
 $smtp_user="admin@domain.com"
 $smtp_pass= "mypass"
 $SMTPClient= New-Object Net.Mail.SmtpClient($smtp_server)
 $email_msg= New-Object System.Net.Mail.MailMessage
 $email_msg.From= $email_from
 $email_msg.Subject= $email_sub
 $email_msg.Body= $email_body
 $email_msg.To.Add($email_to)
 $SmtpClient.UseDefaultCredentials="false"
 $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($smtp_user,$smtp_pass);
 $SMTPClient.Port=25
 $SMTPClient.EnableSsl="false" 
 $SMTPClient.Send($email_msg)

Refer the SmtpClient documentation in Microsoft site for more details.

Vipin

Vipin

I am a dreamer. I admire the web. I admire anything about the web.