Improve the mail callback to support differing failures

Since callbacks are called with different argument-types, we have to be careful. We support two different distinct cases:

 - The error information can be in one ore more of the following items (msg, stderr or stdout)
 - The res/msg value returned can be a string or a list
pull/1648/head
Dag Wieers 12 years ago
parent 37bdee331c
commit 8323a03fa1

@ -51,33 +51,45 @@ class CallbackModule(object):
def runner_on_failed(self, host, res, ignore_errors=False): def runner_on_failed(self, host, res, ignore_errors=False):
if ignore_errors: if ignore_errors:
return return
sender = 'Ansible error on %s <root>' % host sender = '"Ansible: %s" <root>' % host
subject = 'Failure: %s' % res['msg'].split('\n')[0] subject = 'Failed: %(module_name)s %(module_args)s' % res['invocation']
mail(sender=sender, subject=subject, body = 'The following task failed for host ' + host + ':\n\n%(module_name)s %(module_args)s\n\n' % res['invocation']
body='''The following task failed for host %s: if 'stdout' in res.keys() and res['stdout']:
subject = res['stdout'].strip('\r\n').split('\n')[-1]
%s %s body += 'with the following output in standard output:\n\n' + res['stdout'] + '\n\n'
if 'stderr' in res.keys() and res['stderr']:
with the following error message: subject = res['stderr'].strip('\r\n').split('\n')[-1]
body += 'with the following output in standard error:\n\n' + res['stderr'] + '\n\n'
%s if 'msg' in res.keys() and res['msg']:
subject = res['msg'].strip('\r\n').split('\n')[0]
A complete dump of the error: body += 'with the following message:\n\n' + res['msg'] + '\n\n'
body += 'A complete dump of the error:\n\n' + str(res)
%s''' % (host, res['invocation']['module_name'], res['invocation']['module_args'], res['msg'], res) mail(sender=sender, subject=subject, body=body)
)
def runner_on_error(self, host, msg): def runner_on_error(self, host, msg):
sender = 'Ansible: %s <root>' % host sender = '"Ansible: %s" <root>' % host
subject = 'Error: %s' % res['msg'].split('\n')[0] subject = 'Error: %s' % msg.strip('\r\n').split('\n')[0]
mail(sender=sender, subject=subject, body=msg) body = 'An error occured for host ' + host + ' with the following message:\n\n' + msg
mail(sender=sender, subject=subject, body=body)
def runner_on_unreachable(self, host, res): def runner_on_unreachable(self, host, res):
sender = 'Ansible: %s <root>' % host sender = '"Ansible: %s" <root>' % host
subject = 'Unreachable: %s' % res.split('\n')[0] if isinstance(res, basestring):
mail(sender=sender, subject=subject, body=res) 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')[0]
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): def runner_on_async_failed(self, host, res, jid):
sender = 'Ansible: %s <root>' % host sender = '"Ansible: %s" <root>' % host
subject = 'Async failure: %s' % res.split('\n')[0] if isinstance(res, basestring):
mail(sender=sender, subject=subject, body=res) 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')[0]
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)

Loading…
Cancel
Save