首页 > 解决方案 > Ansible 邮件模块发送邮件失败

问题描述

我可以使用以下命令从我的 Linux 主机发送电子邮件:

echo 'These are contents of my mail' | mailx -s 'This is my email subject' -a last2day_access_log catherine@gmail.com

telnet 到 localhost 25 也在同一 Linux 主机上成功连接。

在同一个 linux 主机上尝试了 ansible 邮件模块,如下所示:

- hosts:

     localhost
      tasks:
        - mail:
            host: localhost
            port: 25
            to: 'catherine@gmail.com'
            subject: 'Ansible-report'
            body: 'System {{ ansible_hostname }} has been successfully provisioned.'
          delegate_to: localhost

但是,我收到以下错误:

TASK [mail] ******************************************************************************************
task path: /web/playbooks/test.yml:17
Using module file /usr/lib/python2.7/site-packages/ansible/modules/notification/mail.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: localuser
<localhost> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_zSM8eA/ansible_module_mail.py", line 372, in main
    smtp.sendmail(sender_addr, set(addr_list), composed)
  File "/usr/lib64/python2.7/smtplib.py", line 735, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (553, '5.5.4 <root>... Domain name required for sender address root', 'root')

fatal: [localhost -> localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "attach": null,
            "bcc": null,
            "body": "System mylocalhost has been successfully provisioned.",
            "cc": null,
            "charset": "us-ascii",
            "headers": null,
            "host": "localhost",
            "password": null,
            "port": 25,
            "secure": "try",
            "sender": "root",
            "subject": "Ansible-report",
            "subtype": "plain",
            "timeout": 20,
            "to": "catherine@gmail.com",
            "username": null
        }
    },
    "msg": "Failed to send mail to catherine@gmail.com: (553, '5.5.4 <root>... Domain name required for sender address root', 'root')",
    "rc": 1
}
        to retry, use: --limit @/web/playbooks/test.retry

PLAY RECAP *******************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1

以下是我在 Linux 主机上的主机名:

cat /etc/mail/local-host-names
# local-host-names - include all aliases for your machine here.
mylocalhost

$ uname -a
Linux mylocalhost 3.10.0-1160.11.1.el7.x86_64 #1 SMP Mon Nov 30 13:05:31 EST 2020 x86_64 x86_64 x86_64 GNU/Linux

你能建议吗?

标签: emailansiblesmtpruntime-error

解决方案


根据错误消息告诉您的内容:

SMTPSenderRefused: (553, '5.5.4 <root>... Domain name required for sender address root', 'root')

看起来您的邮件任务实际上是在尝试将邮件发送为<root>,如果您只在本地发送邮件,这可以工作,但很可能会在外界被拒绝。

解决方法是更改​​您的任务并from在您的电子邮件中添加一个正确的:

- mail:
    host: localhost
    port: 25
    from: 'someone@example.org'
    to: 'catherine@gmail.com'
    subject: 'Ansible-report'
    body: 'System {{ ansible_hostname }} has been successfully provisioned.'
  delegate_to: localhost

推荐阅读