Ansible 邮件模块发送邮件失败
Ansible mail module fails to send email
我可以使用以下命令从我的 Linux 主机发送电子邮件:
echo 'These are contents of my mail' | mailx -s 'This is my email subject' -a last2day_access_log catherine@gmail.com
同时远程登录到本地主机 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
你能推荐一下吗?
根据错误消息告诉您的内容:
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
我可以使用以下命令从我的 Linux 主机发送电子邮件:
echo 'These are contents of my mail' | mailx -s 'This is my email subject' -a last2day_access_log catherine@gmail.com
同时远程登录到本地主机 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
你能推荐一下吗?
根据错误消息告诉您的内容:
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