I'm able to send emails from my Linux host using the below command:
echo 'These are contents of my mail' | mailx -s 'This is my email subject' -a last2day_access_log catherine@gmail.com
Also telnet to localhost 25 is successfully Connected on the same Linux host.
On the same linux host tried the ansible mail module like below:
- 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
But, I get the below error:
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
Below is my hostname on my Linux host:
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
Can you please suggest?
From what the error message is telling you:
SMTPSenderRefused: (553, '5.5.4 ... Domain name required for sender address root', 'root')
It seems like your mail task is actually trying to sent the mail as <root>
, which, can work, if you only send mails locally, but will most probably be rejected in the outside world.
A fix would be to alter your task and add a proper from
to your email:
- 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