Ansible - mail module (send email)

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 This assumes that the control node (in this example) is configured to be able to send emails using SMTP server, and that the SMTP server is using port 25 to send email.

- mail:
    port: 25
    from: ""
    to: "John Doe <>"
    cc: "Jane Doe <>"
    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: ""
    port: "587"
    secure: "starttls"
    username: "your_username"
    password: "your_password"
    from: ""
    to: "John Doe <>"
    cc: "Jane Doe <>"
    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: []

TASK [mail]
ok: [ -> localhost]

PLAY RECAP   : ok=2  changed=0  unreachable=0  failed=0


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter b1c50 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |