1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

mail callback: fully use Ansible's option handling; deprecate not specifying sender (#4140)

* Fully use Ansible's option handling. Deprecate not specifying sender.

* Update plugins/callback/mail.py

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
This commit is contained in:
Felix Fontein 2022-02-01 10:12:03 +01:00 committed by GitHub
parent b444dc81a1
commit e09254df91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 16 deletions

View file

@ -0,0 +1,4 @@
minor_changes:
- "mail callback plugin - properly use Ansible's option handling to split lists (https://github.com/ansible-collections/community.general/pull/4140)."
deprecated_features:
- "mail callback plugin - not specifying ``sender`` is deprecated and will be disallowed in community.general 6.0.0 (https://github.com/ansible-collections/community.general/pull/4140)."

View file

@ -11,14 +11,16 @@ name: mail
type: notification type: notification
short_description: Sends failure events via email short_description: Sends failure events via email
description: description:
- This callback will report failures via email - This callback will report failures via email.
author: author:
- Dag Wieers (@dagwieers) - Dag Wieers (@dagwieers)
requirements: requirements:
- whitelisting in configuration - whitelisting in configuration
options: options:
mta: mta:
description: Mail Transfer Agent, server that accepts SMTP description:
- Mail Transfer Agent, server that accepts SMTP.
type: str
env: env:
- name: SMTPHOST - name: SMTPHOST
ini: ini:
@ -26,34 +28,47 @@ options:
key: smtphost key: smtphost
default: localhost default: localhost
mtaport: mtaport:
description: Mail Transfer Agent Port, port at which server SMTP description:
- Mail Transfer Agent Port.
- Port at which server SMTP.
type: int
ini: ini:
- section: callback_mail - section: callback_mail
key: smtpport key: smtpport
default: 25 default: 25
to: to:
description: Mail recipient description:
- Mail recipient.
type: list
elements: str
ini: ini:
- section: callback_mail - section: callback_mail
key: to key: to
default: root default: [root]
sender: sender:
description: Mail sender description:
- Mail sender.
- Note that this will be required from community.general 6.0.0 on.
type: str
ini: ini:
- section: callback_mail - section: callback_mail
key: sender key: sender
cc: cc:
description: CC'd recipient description:
- CC'd recipients.
type: list
elements: str
ini: ini:
- section: callback_mail - section: callback_mail
key: cc key: cc
bcc: bcc:
description: BCC'd recipient description:
- BCC'd recipients.
type: list
elements: str
ini: ini:
- section: callback_mail - section: callback_mail
key: bcc key: bcc
notes:
- "TODO: expand configuration options now that plugins can leverage Ansible's configuration"
''' '''
import json import json
@ -89,9 +104,13 @@ class CallbackModule(CallbackBase):
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct) super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
self.sender = self.get_option('sender') self.sender = self.get_option('sender')
if self.sender is None:
self._display.deprecated(
'The sender for the mail callback has not been specified. This will be an error in the future',
version='6.0.0', collection_name='community.general')
self.to = self.get_option('to') self.to = self.get_option('to')
self.smtphost = self.get_option('mta') self.smtphost = self.get_option('mta')
self.smtpport = int(self.get_option('mtaport')) self.smtpport = self.get_option('mtaport')
self.cc = self.get_option('cc') self.cc = self.get_option('cc')
self.bcc = self.get_option('bcc') self.bcc = self.get_option('bcc')
@ -103,18 +122,22 @@ class CallbackModule(CallbackBase):
content = 'Date: %s\n' % email.utils.formatdate() content = 'Date: %s\n' % email.utils.formatdate()
content += 'From: %s\n' % self.sender content += 'From: %s\n' % self.sender
content += 'To: %s\n' % self.to if self.to:
content += 'To: %s\n' % ','.join(self.to)
if self.cc: if self.cc:
content += 'Cc: %s\n' % self.cc content += 'Cc: %s\n' % ','.join(self.cc)
content += 'Message-ID: %s\n' % email.utils.make_msgid() content += 'Message-ID: %s\n' % email.utils.make_msgid()
content += 'Subject: %s\n\n' % subject.strip() content += 'Subject: %s\n\n' % subject.strip()
content += body content += body
addresses = self.to.split(',') addresses = self.to
if self.cc: if self.cc:
addresses += self.cc.split(',') addresses += self.cc
if self.bcc: if self.bcc:
addresses += self.bcc.split(',') addresses += self.bcc
if not addresses:
self._display.warning('No receiver has been specified for the mail callback plugin.')
for address in addresses: for address in addresses:
smtp.sendmail(self.sender, address, to_bytes(content)) smtp.sendmail(self.sender, address, to_bytes(content))