From f9e315671a61d3fae93ef816c9d5793c9fc2267c Mon Sep 17 00:00:00 2001 From: oceyral Date: Tue, 7 Jan 2020 12:27:11 +0100 Subject: [PATCH] pagerduty_alert: fix IndexError exception (#46770) * pagerduty_alert: fix IndexError exception * clean a bit --- lib/ansible/modules/monitoring/pagerduty_alert.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/monitoring/pagerduty_alert.py b/lib/ansible/modules/monitoring/pagerduty_alert.py index b2551620ebf..ea5645e81e5 100644 --- a/lib/ansible/modules/monitoring/pagerduty_alert.py +++ b/lib/ansible/modules/monitoring/pagerduty_alert.py @@ -153,11 +153,18 @@ def check(module, name, state, service_id, integration_key, api_key, incident_ke if info['status'] != 200: module.fail_json(msg="failed to check current incident status." "Reason: %s" % info['msg']) - json_out = json.loads(response.read())["incidents"][0] - if state != json_out["status"]: - return json_out, True - return json_out, False + incidents = json.loads(response.read())["incidents"] + msg = "No corresponding incident" + + if len(incidents) == 0: + if state in ('acknowledged', 'resolved'): + return msg, False + return msg, True + elif state != incidents[0]["status"]: + return incidents[0], True + + return incidents[0], False def send_event(module, service_key, event_type, desc,