Skip to content

Commit cc70116

Browse files
committed
Merge pull request #19 from DataDog/olivielpeau/fix-no-module-name
Avoid failing when res['invocation'] has no `module_name` key
2 parents 4dc4d82 + 6a7fa34 commit cc70116

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

datadog_callback.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,39 @@ def format_result(res):
156156
event_text = msg
157157
else:
158158
invocation = res['invocation']
159-
event_text = "$$$\n{0}[{1}]\n$$$\n".format(invocation['module_name'], invocation.get('module_args', ''))
159+
module_name = invocation.get('module_name', 'undefined')
160+
event_text = "$$$\n{0}[{1}]\n$$$\n".format(module_name, invocation.get('module_args', ''))
160161
event_text += msg
161-
module_name = 'module:{0}'.format(invocation['module_name'])
162162
if 'module_stdout' in res:
163163
# On Ansible v2, details on internal failures of modules are not reported in the `msg`,
164164
# so we have to extract the info differently
165165
event_text += "$$$\n{0}\n{1}\n$$$\n".format(
166166
res.get('module_stdout', ''), res.get('module_stderr', ''))
167167

168-
return event_text, module_name
168+
module_name_tag = 'module:{0}'.format(module_name)
169+
170+
return event_text, module_name_tag
169171

170172
### Ansible callbacks ###
171173
def runner_on_failed(self, host, res, ignore_errors=False):
172-
event_text, module_name = self.format_result(res)
174+
event_text, module_name_tag = self.format_result(res)
173175
self.send_task_event(
174176
'Ansible task failed on "{0}"'.format(host),
175177
alert_type='error',
176178
text=event_text,
177-
tags=[module_name],
179+
tags=[module_name_tag],
178180
host=host,
179181
)
180182

181183
def runner_on_ok(self, host, res):
182184
# Only send an event when the task has changed on the host
183185
if res.get('changed'):
184-
event_text, module_name = self.format_result(res)
186+
event_text, module_name_tag = self.format_result(res)
185187
self.send_task_event(
186188
'Ansible task changed on "{0}"'.format(host),
187189
alert_type='success',
188190
text=event_text,
189-
tags=[module_name],
191+
tags=[module_name_tag],
190192
host=host,
191193
)
192194

0 commit comments

Comments
 (0)