If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The mail module can be used to send email. In this example, an email will be sent to john.doe@example.com. This assumes that the control node (in this example) is configured to be able to send emails using SMTP server smtp.example.com, and that the SMTP server is using port 25 to send email.
---
- hosts: localhost
tasks:
- mail:
host: smtp.example.com
port: 25
from: "no-reply@example.com"
to: "John Doe <john.doe@example.com>"
cc: "Jane Doe <jane.doe@example.com>"
subject: Example Subject
body: "Line one \n Line two \n Line three"
...
When running this on a managed node, you may need to use delegate_to to have the email sent using the control node (that's your Ansible server).
---
- hosts: all
tasks:
- mail:
host: smtp.example.com
port: 25
from: "no-reply@example.com"
to: "John Doe <john.doe@example.com>"
cc: "Jane Doe <jane.doe@example.com>"
subject: Example Subject
body: "Line one \n Line two \n Line three"
delegate_to: localhost
...
Often, an SMTP server is configured with some type of security, such as starttls and authentication. In this scenario, the secure, username and password keys can be used.
---
- hosts: all
tasks:
- mail:
host: smtp.example.com
port: 587
secure: starttls
username: your_username
password: your_password
from: no-reply@example.com
to: "John Doe <john.doe@example.com>"
cc: "Jane Doe <jane.doe@example.com>"
subject: Example Subject
body: "Line one \n Line two \n Line three"
delegate_to: localhost
...
If the email is successfully sent, the play should return the following.
PLAY [all]
TASK [Gathering Facts]
ok: [server1.example.com]
TASK [mail]
ok: [server1.example.com -> localhost]
PLAY RECAP
server1.example.com : ok=2 changed=0 unreachable=0 failed=0
Did you find this article helpful?
If so, consider buying me a coffee over at