Hello Ansible Team/Users,
I have a Playbook which is expected to send an email, as below. When run, it errors out with "Unable to Connect
mailer.domain.com:25: Connection unexpectedly closed: timed out".
---
- hosts: localhost
gather_facts: yes
vars_prompt:
- name: file
prompt: "Please enter the name of the .zip file to email!"
private: no
tasks:
- name: Sending Email
mail:
host: mailer.domain.com
port: 25
subject: Ansible Test
from: FromAddress@DOMAIN.COM
to:
- ToAddress@DOMAIN.COM
cc:
- CCAddress@DOMAIN.COM
subtype: html
secure: starttls
timeout: 120
attach:
- "~/{{ file }}.zip"
Returns the below ERROR.
[bharath@acm ~]$ ansible-playbook test.yml -vvvv
ansible-playbook 2.7.4
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/nfs-home/bharath/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Sep 12 2018, 05:31:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
/home/nfs-home/bharath/hosts did not meet host_list requirements, check plugin documentation if this is unexpected
/home/nfs-home/bharath/hosts did not meet script requirements, check plugin documentation if this is unexpected
Parsed /home/nfs-home/bharath/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc
PLAYBOOK: test.yml ***************************************************************************************************************************************************************************************
1 plays in test.yml
Please enter the name of the .zip file to email!!: test
PLAY [localhost] ***************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
task path: /home/nfs-home/bharath/test.yml:2
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: bharath
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /tmp/.ansible-${USER}/ansible-tmp-1545085312.03-180815852497310 `" && echo ansible-tmp-1545085312.03-180815852497310="` echo /tmp/.ansible-${USER}/ansible-tmp-1545085312.03-180815852497310 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<127.0.0.1> PUT /home/nfs-home/bharath/.ansible/ansible-local-53102i8q99p/tmpDy7Vr0 TO /tmp/.ansible-bharath/ansible-tmp-1545085312.03-180815852497310/AnsiballZ_setup.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /tmp/.ansible-bharath/ansible-tmp-1545085312.03-180815852497310/ /tmp/.ansible-bharath/ansible-tmp-1545085312.03-180815852497310/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /tmp/.ansible-bharath/ansible-tmp-1545085312.03-180815852497310/AnsiballZ_setup.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /tmp/.ansible-bharath/ansible-tmp-1545085312.03-180815852497310/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers
TASK [Sending Email] **********************************************************************************************************************************************************************************
task path: /home/nfs-home/bharath/test.yml:12
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: bharath
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /tmp/.ansible-${USER}/ansible-tmp-1545085313.79-80160480232068 `" && echo ansible-tmp-1545085313.79-80160480232068="` echo /tmp/.ansible-${USER}/ansible-tmp-1545085313.79-80160480232068 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/notification/mail.py
<127.0.0.1> PUT /home/nfs-home/bharath/.ansible/ansible-local-53102i8q99p/tmpACwk7J TO /tmp/.ansible-bharath/ansible-tmp-1545085313.79-80160480232068/AnsiballZ_mail.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /tmp/.ansible-bharath/ansible-tmp-1545085313.79-80160480232068/ /tmp/.ansible-bharath/ansible-tmp-1545085313.79-80160480232068/AnsiballZ_mail.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /tmp/.ansible-bharath/ansible-tmp-1545085313.79-80160480232068/AnsiballZ_mail.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /tmp/.ansible-bharath/ansible-tmp-1545085313.79-80160480232068/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_mail_payload_fe4hgc/__main__.py", line 263, in main
code, smtpmessage = smtp.connect(host, port)
File "/usr/lib64/python2.7/smtplib.py", line 316, in connect
(code, msg) = self.getreply()
File "/usr/lib64/python2.7/smtplib.py", line 364, in getreply
+ str(e))
SMTPServerDisconnected: Connection unexpectedly closed: timed out
fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"attach": [
"~/test.zip"
],
"bcc": [],
"body": null,
"cc": [
"CCAd...@DOMAIN.COM"
],
"charset": "utf-8",
"from": "FromA...@DOMAIN.COM",
"headers": [],
"host": "mailer.domain.com",
"password": null,
"port": 25,
"secure": "never",
"sender": "FromA...@DOMAIN.COM",
"subject": "Ansible Test",
"subtype": "html",
"timeout": 120,
"to": [
"ToAd...@DOMAIN.COM"
],
"username": null
}
},
"msg": "Unable to Connect mailer.domain.com:25: Connection unexpectedly closed: timed out",
"rc": 1
}
to retry, use: --limit @/home/nfs-home/bharath/test.retry
PLAY RECAP ***************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1
However, the below simple python code can send an email successfully.
#!/usr/bin/python
import smtplib
sender = 'FromA...@DOMAIN.COM'
receivers = ['ToAd...@DOMAIN.COM']
message = """
Ansible Test
"""
try:
smtpObj = smtplib.SMTP('mailer.domain.com', 25)
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Can anyone please let me know where am I going wrong. Or, any help is appreciated. Thanks in advance.