Skip to content

Commit 208c1b5

Browse files
committed
Merge pull request #7 from gini/fix_no_log_and_invocation_key
Handle no_log: True and missing invocation key gracefully
2 parents 941c70b + 28e0ef6 commit 208c1b5

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

datadog_callback.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,43 @@ def pluralize(number, noun):
110110

111111
return "{0} {1}s".format(number, noun)
112112

113+
# format helper for event_text
114+
@staticmethod
115+
def format_result(res):
116+
msg = "$$$\n{0}\n$$$\n".format(res['msg']) if res.get('msg') else ""
117+
module_name = 'undefined'
118+
119+
if res.get('censored'):
120+
event_text = res.get('censored')
121+
elif not res.get('invocation'):
122+
event_text = msg
123+
else:
124+
event_text = "$$$\n{0}[{1}]\n$$$\n".format(res['invocation']['module_name'], res['invocation']['module_args'])
125+
event_text += msg
126+
module_name = 'module:{0}'.format(res['invocation']['module_name'])
127+
128+
return event_text, module_name
129+
113130
### Ansible callbacks ###
114131
def runner_on_failed(self, host, res, ignore_errors=False):
115-
event_text = "$$$\n{0}[{1}]\n$$$\n".format(res['invocation']['module_name'], res['invocation']['module_args'])
116-
event_text += "$$$\n{0}\n$$$\n".format(res['msg'])
132+
event_text, module_name = self.format_result(res)
117133
self.send_task_event(
118134
'Ansible task failed on "{0}"'.format(host),
119135
alert_type='error',
120136
text=event_text,
121-
tags=['module:{0}'.format(res['invocation']['module_name'])],
137+
tags=[module_name],
122138
host=host,
123139
)
124140

125141
def runner_on_ok(self, host, res):
126142
# Only send an event when the task has changed on the host
127143
if res.get('changed'):
128-
event_text = "$$$\n{0}[{1}]\n$$$\n".format(res['invocation']['module_name'], res['invocation']['module_args'])
144+
event_text, module_name = self.format_result(res)
129145
self.send_task_event(
130146
'Ansible task changed on "{0}"'.format(host),
131147
alert_type='success',
132148
text=event_text,
133-
tags=['module:{0}'.format(res['invocation']['module_name'])],
149+
tags=[module_name],
134150
host=host,
135151
)
136152

0 commit comments

Comments
 (0)