The mail module can be used to send email. The ansible-doc mail command can be used to show the Ansible documention on the mail module.
This module is excuted on the managed node (e.g. the target system), meaning that the managed node will initiate the sending of the email. Or, the following can be used to invoke this module on the control node (that's your Ansible server).
In this example, an email will be sent to firstname.lastname@example.org. 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.
- mail: host: smtp.example.com port: 25 from: "email@example.com" to: "John Doe <firstname.lastname@example.org>" cc: "Jane Doe <email@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.
- mail: host: "smtp.example.com" port: "587" secure: "starttls" username: "your_username" password: "your_password" from: "firstname.lastname@example.org" to: "John Doe <email@example.com>" cc: "Jane Doe <firstname.lastname@example.org>" 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