How to send an email with Ansible

10,204

Looking at the source code for the mail module ( https://github.com/ansible/ansible/blob/d1effecb2ef073e478c67a7ca39cf56708a66a48/library/notification/mail ) it doesn't look like it supports SMTP authentication.

It shouldn't be too hard to add support for it however. It would require adding the username and password parameters to the module, detecting if they've both been supplied, and if so, calling smtp.login() with those parameters.

In fact, it looks like there's two pull requests to do exactly that at the moment here

https://github.com/ansible/ansible/pull/7213

and here

https://github.com/ansible/ansible/pull/6667

So support will most likely be added in dev soon.

Share:
10,204
jgg
Author by

jgg

Updated on June 12, 2022

Comments

  • jgg
    jgg almost 2 years

    I'm trying to send an email using Ansible, but I can't understand how it works as I don't know how to provide user and password for such service (not specified in the documentation).

    Both my machine and the email server are in the same network, but I need to be authenticated in order to send the email.

    This is my yml file:

    --- 
    - name: Testing email
      hosts: localhost
      tasks: 
        - name: Send email
          local_action: mail
            host=mail.server.com
            port=993
            subject="Ansible test mail"
            body="Testing email"
            from=my@email
            to="y@email
            charset=utf8
    

    And this is the related content of the hosts' file:

    [localhost]
    localhost ansible_connection=local
    

    Any idea about how should I configure it? Thanks in advance.