From 0f9b01ef8b333ed96cef7eab3410251d198d2202 Mon Sep 17 00:00:00 2001 From: Andrew Lathrop Date: Fri, 24 May 2024 13:48:14 +0000 Subject: [PATCH] handle case when no matching master item is found --- plugins/modules/zabbix_item.py | 5 ++++- plugins/modules/zabbix_itemprototype.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/modules/zabbix_item.py b/plugins/modules/zabbix_item.py index b7407341f..871384cbc 100644 --- a/plugins/modules/zabbix_item.py +++ b/plugins/modules/zabbix_item.py @@ -429,7 +429,10 @@ def sanitize_params(self, name, params): params['master_item']['host_name'] = None if 'template_name' not in params['master_item']: params['master_item']['template_name'] = None - params['master_itemid'] = self.get_items(params['master_item']['item_name'], params['master_item']['host_name'], params['master_item']['template_name'])[0]['itemid'] + master_items = self.get_items(params['master_item']['item_name'], params['master_item']['host_name'], params['master_item']['template_name']) + if len(master_items) == 0: + self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name']) + params['master_itemid'] = master_items[0]['itemid'] params.pop('master_item') if 'preprocessing' in params: for param in params['preprocessing']: diff --git a/plugins/modules/zabbix_itemprototype.py b/plugins/modules/zabbix_itemprototype.py index afca62e05..5320f5db7 100644 --- a/plugins/modules/zabbix_itemprototype.py +++ b/plugins/modules/zabbix_itemprototype.py @@ -456,7 +456,10 @@ def sanitize_params(self, name, discoveryrule_name, params, host_name=None, temp params['master_item']['host_name'] = None if 'template_name' not in params['master_item']: params['master_item']['template_name'] = None - params['master_itemid'] = self.get_itemprototypes(params['master_item']['item_name'], params['master_item']['discoveryrule_name'], params['master_item']['host_name'], params['master_item']['template_name'])[0]['itemid'] + master_items = self.get_itemprototypes(params['master_item']['item_name'], params['master_item']['discoveryrule_name'], params['master_item']['host_name'], params['master_item']['template_name']) + if len(master_items) == 0: + self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name']) + params['master_itemid'] = master_items[0]['itemid'] params.pop('master_item') if 'preprocessing' in params: for param in params['preprocessing']: