Playbooks failing on mail module after migrating from Tower to Ansible Automation Platform

Solution Verified - Updated -

Environment

  • Ansible Automation Platform 2.x
  • Ansible Tower 3.8.x

Issue

  • After migrating from Ansible Tower 3.8.x to AAP 2.x, playbooks are failing to send emails with the following error:
ok: [localhost]
TASK [Try to send an email from here] ******************************************
Thursday 12 January 2023  12:41:42 +0000 (0:00:00.831)       0:00:00.908 ****** 
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ConnectionRefusedError: [Errno 111] Connection refused
fatal: [localhost -> localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 102, in <module>\n  File \"<stdin>\", line 94, in _ansiballz_main\n  File \"<stdin>\", line 40, in invoke_module\n  File \"/usr/lib64/python3.8/runpy.py\", line 207, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.8/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib64/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_mail_payload_l0n_y4r8/ansible_mail_payload.zip/ansible/modules/notification/mail.py\", line 398, in <module>\n  File \"/tmp/ansible_mail_payload_l0n_y4r8/ansible_mail_payload.zip/ansible/modules/notification/mail.py\", line …
PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
Thursday 12 January 2023  12:41:43 +0000 (0:00:00.445)       0:00:01.354 ****** 
=============================================================================== 
Gathering Facts --------------------------------------------------------- 0.83s
Try to send an email from here ------------------------------------------ 0.45s

Resolution

  • Add a host attribute to the playbook that specifies an external SMTP relay:
- hosts: localhost
  tasks:
  - name: Send an email
    mail:
      to: recipient@example.com
      host: some.smtp.host
      subject: This is the subject
      body: This is the email body
      attach:
        - /path/to/attachment1.txt

Root Cause

  • When SMTP server is not specified it defaults to localhost. Now, with Ansible Automation Platform 2, localhost means you’re running inside a container, which is ephemeral in nature. This means that localhost is now no longer tied to a particular host and hence the email sending fails.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments