diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_action.py b/lib/ansible/modules/monitoring/zabbix/zabbix_action.py index 8c3211f7d46..79764ca8fcf 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_action.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_action.py @@ -131,6 +131,18 @@ options: description: - Arbitrary unique ID that is used to reference the condition from a custom expression. - Can only contain upper-case letters. + - Required for custom expression filters. + eval_type: + description: + - Filter condition evaluation method. + - Defaults to C(andor) if conditions are less then 2 or if + I(formula) is not specified. + - Defaults to C(custom_expression) when formula is specified. + choices: + - 'andor' + - 'and' + - 'or' + - 'custom_expression' formula: description: - User-defined expression to be used for evaluating conditions of filters with a custom expression. @@ -1215,11 +1227,11 @@ class Filter(object): self._zapi = zbx self._zapi_wrapper = zapi_wrapper - def _construct_evaltype(self, _eval, _conditions): + def _construct_evaltype(self, _eval_type, _formula, _conditions): """Construct the eval type Args: - _eval: zabbix condition evaluation formula + _formula: zabbix condition evaluation formula _conditions: list of conditions to check Returns: @@ -1230,9 +1242,37 @@ class Filter(object): 'evaltype': '0', 'formula': None } + if _eval_type == 'andor': + return { + 'evaltype': '0', + 'formula': None + } + if _eval_type == 'and': + return { + 'evaltype': '1', + 'formula': None + } + if _eval_type == 'or': + return { + 'evaltype': '2', + 'formula': None + } + if _eval_type == 'custom_expression': + if _formula is not None: + return { + 'evaltype': '3', + 'formula': _formula + } + else: + self._module.fail_json(msg="'formula' is required when 'eval_type' is set to 'custom_expression'") + if _formula is not None: + return { + 'evaltype': '3', + 'formula': _formula + } return { - 'evaltype': '3', - 'formula': _eval + 'evaltype': '0', + 'formula': None } def _construct_conditiontype(self, _condition): @@ -1401,7 +1441,7 @@ class Filter(object): https://www.zabbix.com/documentation/3.4/manual/api/reference/action/object#action_filter_condition""" % (value, conditiontype) ) - def construct_the_data(self, _formula, _conditions): + def construct_the_data(self, _eval_type, _formula, _conditions): """Construct the user defined filter conditions to fit the Zabbix API requirements operations data using helper methods. @@ -1426,6 +1466,7 @@ class Filter(object): "operator": self._construct_operator(cond) }) _constructed_evaltype = self._construct_evaltype( + _eval_type, _formula, constructed_data['conditions'] ) @@ -1573,6 +1614,7 @@ def main(): acknowledge_default_subject=dict(type='str', required=False, default=None), conditions=dict(type='list', required=False, default=None), formula=dict(type='str', required=False, default=None), + eval_type=dict(type='str', required=False, default=None, choices=['andor', 'and', 'or', 'custom_expression']), operations=dict(type='list', required=False, default=None), recovery_operations=dict(type='list', required=False, default=[]), acknowledge_operations=dict(type='list', required=False, default=[]) @@ -1603,6 +1645,7 @@ def main(): acknowledge_default_subject = module.params['acknowledge_default_subject'] conditions = module.params['conditions'] formula = module.params['formula'] + eval_type = module.params['eval_type'] operations = module.params['operations'] recovery_operations = module.params['recovery_operations'] acknowledge_operations = module.params['acknowledge_operations'] @@ -1645,7 +1688,7 @@ def main(): operations=ops.construct_the_data(operations), recovery_operations=recovery_ops.construct_the_data(recovery_operations), acknowledge_operations=acknowledge_ops.construct_the_data(acknowledge_operations), - conditions=fltr.construct_the_data(formula, conditions) + conditions=fltr.construct_the_data(eval_type, formula, conditions) ) if difference == {}: @@ -1674,7 +1717,7 @@ def main(): operations=ops.construct_the_data(operations), recovery_operations=recovery_ops.construct_the_data(recovery_operations), acknowledge_operations=acknowledge_ops.construct_the_data(acknowledge_operations), - conditions=fltr.construct_the_data(formula, conditions) + conditions=fltr.construct_the_data(eval_type, formula, conditions) ) module.exit_json(changed=True, msg="Action created: %s, ID: %s" % (name, action_id))