Published on 2019-09-25· Updated on 2024-01-25
The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.
Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs. Ansible comes with 20+ built-in modules and more than 3000+ functionalities. Here is the Ansible installation guide.
To install Ansible on RHEL/CentOS, run the following command:
$ sudo yum install ansible
Record the completion of any automated task, i.e., a playbook, and send a detailed report to the stakeholders so they act accordingly:
Ansible can send emails with attachments via:
Click here to know more about the email module in Ansible.
Let's understand the different parameters used for making an SMTP connection and to send an email.
host: The address of the mail server. Default is localhost. port: The port number of the mail server to connect. Mostly 25, 465, 587. username: If SMTP requires a username. password: If SMTP requires a password. timeout: Sets the timeout in seconds for connection attempts.
secure = always. Sends email only if connection is encrypted. It fails if the server doesn't accept the encrypted connection. secure = never. Before sending an email, the connection doesn't attempt to set up a secure SSL/TLS session. secure = try. Before trying to send an email, the connection attempts to set up a secure SSL/TLS session. secure = starttls. Before sending an email, the connection tries to upgrade to a secure SSL/TLS connection. The connection fails in case if unable to do so.
headers : A list of headers that needs to go with the message. from : The email address from which the mail is sent. Default is root. to : The email address(es) of the recipient to whom the mail sent. cc : The email address(es) of the recipient to whom the mail copied. bcc : The email address(es) of the recipient to whom the mail 'blind' copied.
subject : The subject of the email to send (mandatory). subtype : The minor mime type, can be either plain or HTML. The major type is always text.
charset : The character set of the email to send. Default is UTF-8 body : The body of the email to send. attach : A list of files (full path) to attach to the mail. The content-type should be set to "application/octet-stream" for all the attached files.
- hosts: - localhost tasks: - name: Sending an e-mail using Gmail SMTP servers mail: host: smtp.gmail.com port: 587 username: [email protected] password: your-password to: recipient-name <recipient-email@domain> subject: Ansible Report body: System [[ ansible_hostname ]] has been successfully provisioned. delegate_to: localhost
- hosts: - localhost tasks: - name: Send Emails to a bunch of users, with Playbook Report as an attachment. mail: host: localhost port: 25 subject: Ansible Playbook Report body: This is an Email generated using Ansible after execution of task. from: from-email@domain (Ansible Automates) to: - to-email-name-1 <to-email-1@domain> - to-email-name-2 <to-email-1@domain> cc: cc-name <cc-email@domain> attach: - <enter-path-of-your-file-to-be-attached> headers: - Reply-To=reply-to-email@domain - X-Special="Write something special about this Email" charset: us-ascii delegate_to: localhost
Note: Please check the host-port connection using the ping/telnet command if you get an error saying “socket.error: [Errno 111] connection refused.”
You will now be able to send email notifications from your Ansible setup. If other errors pop up, refer to the below error list.
Error 1: When the mailing service was not running on Port 25 OR, the server refused the connection, an exception occurred during the task execution. To see the full traceback, use -vvv. The error was:
socket.error: [Errno 111] Connection refused fatal: [localhost -> localhost]: FAILED! => array ( 'changed' => false, 'module_stderr' => 'Traceback (most recent call last): File ".ansible/tmp/ansible-tmp-1569048408.38-18927787866074/AnsiballZ_mail.py", line 114, in _ansiballz_main() File ".ansible/tmp/ansible-tmp-1569048408.38-18927787866074/AnsiballZ_mail.py", line 106, in _ansiballz_main invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS) File ".ansible/tmp/ansible-tmp-1569048408.38-18927787866074/AnsiballZ_mail.py", line 49, in invoke_module imp.load_module(\'__main__\', mod, module, MOD_DESC) File "/tmp/ansible_mail_payload_fiQfEe/__main__.py", line 397, in File "/tmp/ansible_mail_payload_fiQfEe/__main__.py", line 286, in main File "/usr/lib64/python2.7/smtplib.py", line 315, in connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib64/python2.7/smtplib.py", line 290, in _get_socket return socket.create_connection((host, port), timeout) File "/usr/lib64/python2.7/socket.py", line 571, in create_connection raise err socket.error: [Errno 111] Connection refused', 'module_stdout' => '', 'msg' => 'MODULE FAILURE See stdout/stderr for the exact error', 'rc' => 1, )
Error 2: When Gmail account credentials were incorrect: The error was:
[localhost -> localhost]: FAILED! => {"changed": false, "msg": "Authentication to smtp.gmail.com:587 failed, please check your username and/or password", "rc": 1}
Error 3: When the file to be attached was either missing at a defined location or was inaccessible, an exception occurred during task execution. To see the full traceback, use -vvv. The error was:
IOError: [Errno 2] No such file or directory: '/tmp/ansible.logs' fatal: [localhost -> localhost]: FAILED! => array ( 'changed' => false, 'msg' => 'Failed to send mail: can\'t attach file /tmp/ansible.logs: [Errno 2] No such file or directory: \'/tmp/ansible.logs\'', 'rc' => 1, )
Error 4: When the recipient's email address was incorrect, an exception occurred during task execution. To see the full traceback, use -vvv. The error was:
SMTPRecipientsRefused: {'to-email-1': (550, '5.1.1 <to-email-1>: Recipient address rejected: User unknown in local recipient table')} fatal: [localhost -> localhost]: FAILED! => array ( 'changed' => false, 'msg' => 'Failed to send mail to \'to-email-1\': {\'to-email-1\': (550, \'5.1.1 : Recipient address rejected: User unknown in local recipient table\')}', 'rc' => 1, )
Error 5: When any mandatory parameters are missing, e.g., "Subject" missing: The error was:
[localhost -> localhost]: FAILED! => {"changed": false, "msg": "missing required arguments: subject"}
Few important notification sending modules in Ansible:
If you are facing any issues other than the ones listed in the above tutorial or have suggestions, please feel free to reach us at [email protected]
Excited about the latest in Bulk Email Marketing! Check out this insightful blog on Gmail and Yahoo updates in the email marketing landscape.
Explore the Blog - Here
Stay ahead of the game with valuable insights on optimizing your email campaigns! 📬
Netcorecloud's toolkit is the solution to all your email problems.
Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.
Gaurang Acharya