赤色烙印 发表于 2018-7-30 13:01:32

ansible调用callbacks插件实现结果nosql输出回调

#xiaorui.cc  
原文:http://rfyiamcool.blog.51cto.com/1030776/1440624
  

  
import smtplib
  

  
def mail(subject='Ansible error mail', sender='<root>', to='root', cc=None, bcc=None, body=None):
  
    if not body:
  
      body = subject
  

  
    smtp = smtplib.SMTP('localhost')
  

  
    content = 'From: %s\n' % sender
  
    content += 'To: %s\n' % to
  
    if cc:
  
      content += 'Cc: %s\n' % cc
  
    content += 'Subject: %s\n\n' % subject
  
    content += body
  

  
    addresses = to.split(',')
  
    if cc:
  
      addresses += cc.split(',')
  
    if bcc:
  
      addresses += bcc.split(',')
  

  
    for address in addresses:
  
      smtp.sendmail(sender, address, content)
  

  
    smtp.quit()
  

  

  
class CallbackModule(object):
  

  
    """
  
    This Ansible callback plugin mails errors to interested parties.
  
    """
  

  
    def runner_on_failed(self, host, res, ignore_errors=False):
  
      if ignore_errors:
  
            return
  
      sender = '"Ansible: %s" <root>' % host
  
      subject = 'Failed: %(module_name)s %(module_args)s' % res['invocation']
  
      body = 'The following task failed for host ' + host + ':\n\n%(module_name)s %(module_args)s\n\n' % res['invocation']
  
      if 'stdout' in res.keys() and res['stdout']:
  
            subject = res['stdout'].strip('\r\n').split('\n')[-1]
  
            body += 'with the following output in standard output:\n\n' + res['stdout'] + '\n\n'
  
      if 'stderr' in res.keys() and res['stderr']:
  
            subject = res['stderr'].strip('\r\n').split('\n')[-1]
  
            body += 'with the following output in standard error:\n\n' + res['stderr'] + '\n\n'
  
      if 'msg' in res.keys() and res['msg']:
  
            subject = res['msg'].strip('\r\n').split('\n')
  
            body += 'with the following message:\n\n' + res['msg'] + '\n\n'
  
      body += 'A complete dump of the error:\n\n' + str(res)
  
      mail(sender=sender, subject=subject, body=body)
  

  
    def runner_on_unreachable(self, host, res):
  
      sender = '"Ansible: %s" <root>' % host
  
      if isinstance(res, basestring):
  
            subject = 'Unreachable: %s' % res.strip('\r\n').split('\n')[-1]
  
            body = 'An error occured for host ' + host + ' with the following message:\n\n' + res
  
      else:
  
            subject = 'Unreachable: %s' % res['msg'].strip('\r\n').split('\n')
  
            body = 'An error occured for host ' + host + ' with the following message:\n\n' + \
  
                   res['msg'] + '\n\nA complete dump of the error:\n\n' + str(res)
  
      mail(sender=sender, subject=subject, body=body)
  

  
    def runner_on_async_failed(self, host, res, jid):
  
      sender = '"Ansible: %s" <root>' % host
  
      if isinstance(res, basestring):
  
            subject = 'Async failure: %s' % res.strip('\r\n').split('\n')[-1]
  
            body = 'An error occured for host ' + host + ' with the following message:\n\n' + res
  
      else:
  
            subject = 'Async failure: %s' % res['msg'].strip('\r\n').split('\n')
  
            body = 'An error occured for host ' + host + ' with the following message:\n\n' + \
  
                   res['msg'] + '\n\nA complete dump of the error:\n\n' + str(res)
  
      mail(sender=sender, subject=subject, body=body)
页: [1]
查看完整版本: ansible调用callbacks插件实现结果nosql输出回调