mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-27 04:41:26 -07:00
parent
de18bcb95f
commit
5d3d9cfe0d
3 changed files with 29 additions and 18 deletions
|
@ -19,6 +19,7 @@ import os
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from ansible.utils.unicode import to_bytes
|
||||||
from ansible.plugins.callback import CallbackBase
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
|
||||||
# NOTE: in Ansible 1.2 or later general logging is available without
|
# NOTE: in Ansible 1.2 or later general logging is available without
|
||||||
|
@ -60,9 +61,10 @@ class CallbackModule(CallbackBase):
|
||||||
|
|
||||||
path = os.path.join("/var/log/ansible/hosts", host)
|
path = os.path.join("/var/log/ansible/hosts", host)
|
||||||
now = time.strftime(self.TIME_FORMAT, time.localtime())
|
now = time.strftime(self.TIME_FORMAT, time.localtime())
|
||||||
fd = open(path, "a")
|
|
||||||
fd.write(self.MSG_FORMAT % dict(now=now, category=category, data=data))
|
msg = to_bytes(self.MSG_FORMAT % dict(now=now, category=category, data=data))
|
||||||
fd.close()
|
with open(path, "ab") as fd:
|
||||||
|
fd.write(msg)
|
||||||
|
|
||||||
def runner_on_failed(self, host, res, ignore_errors=False):
|
def runner_on_failed(self, host, res, ignore_errors=False):
|
||||||
self.log(host, 'FAILED', res)
|
self.log(host, 'FAILED', res)
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
import os
|
import os
|
||||||
import smtplib
|
import smtplib
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from ansible.utils.unicode import to_bytes
|
||||||
from ansible.plugins.callback import CallbackBase
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
|
||||||
def mail(subject='Ansible error mail', sender=None, to=None, cc=None, bcc=None, body=None, smtphost=None):
|
def mail(subject='Ansible error mail', sender=None, to=None, cc=None, bcc=None, body=None, smtphost=None):
|
||||||
|
@ -35,21 +37,27 @@ def mail(subject='Ansible error mail', sender=None, to=None, cc=None, bcc=None,
|
||||||
|
|
||||||
smtp = smtplib.SMTP(smtphost)
|
smtp = smtplib.SMTP(smtphost)
|
||||||
|
|
||||||
content = 'From: %s\n' % sender
|
b_sender = to_bytes(sender)
|
||||||
content += 'To: %s\n' % to
|
b_to = to_bytes(to)
|
||||||
if cc:
|
b_cc = to_bytes(cc)
|
||||||
content += 'Cc: %s\n' % cc
|
b_subject = to_bytes(subject)
|
||||||
content += 'Subject: %s\n\n' % subject
|
b_body = to_bytes(body)
|
||||||
content += body
|
|
||||||
|
|
||||||
addresses = to.split(',')
|
b_content = b'From: %s\n' % b_sender
|
||||||
|
b_content += b'To: %s\n' % b_to
|
||||||
if cc:
|
if cc:
|
||||||
addresses += cc.split(',')
|
b_content += b'Cc: %s\n' % b_cc
|
||||||
if bcc:
|
b_content += b'Subject: %s\n\n' % b_subject
|
||||||
addresses += bcc.split(',')
|
b_content += b_body
|
||||||
|
|
||||||
for address in addresses:
|
b_addresses = b_to.split(b',')
|
||||||
smtp.sendmail(sender, address, content)
|
if b_cc:
|
||||||
|
b_addresses += b_cc.split(b',')
|
||||||
|
if b_bcc:
|
||||||
|
b_addresses += b_bcc.split(b',')
|
||||||
|
|
||||||
|
for b_address in b_addresses:
|
||||||
|
smtp.sendmail(b_sender, b_address, b_content)
|
||||||
|
|
||||||
smtp.quit()
|
smtp.quit()
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ import os
|
||||||
|
|
||||||
from ansible.plugins.callback import CallbackBase
|
from ansible.plugins.callback import CallbackBase
|
||||||
from ansible.utils.path import makedirs_safe
|
from ansible.utils.path import makedirs_safe
|
||||||
|
from ansible.utils.unicode import to_bytes
|
||||||
from ansible.constants import TREE_DIR
|
from ansible.constants import TREE_DIR
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,12 +45,12 @@ class CallbackModule(CallbackBase):
|
||||||
def write_tree_file(self, hostname, buf):
|
def write_tree_file(self, hostname, buf):
|
||||||
''' write something into treedir/hostname '''
|
''' write something into treedir/hostname '''
|
||||||
|
|
||||||
|
buf = to_bytes(buf)
|
||||||
try:
|
try:
|
||||||
makedirs_safe(self.tree)
|
makedirs_safe(self.tree)
|
||||||
path = os.path.join(self.tree, hostname)
|
path = os.path.join(self.tree, hostname)
|
||||||
fd = open(path, "w+")
|
with open(path, 'wb+') as fd:
|
||||||
fd.write(buf)
|
fd.write(buf)
|
||||||
fd.close()
|
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
self._display.warnings("Unable to write to %s's file: %s" % (hostname, str(e)))
|
self._display.warnings("Unable to write to %s's file: %s" % (hostname, str(e)))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue