You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ansible创建包含所有discovery rule的单个Zabbix Action?

单Zabbix Action整合所有发现规则的实现方案

可行性确认

完全可行。Zabbix支持在单个Action中配置多个discovery_rule条件,通过逻辑或关联即可匹配任意指定的发现规则,同时能完整保留原有其他条件与操作逻辑。

具体实现方法

1. 动态生成发现规则条件块

基于已有的discovery_rule_list,利用Ansible过滤器批量生成符合Zabbix API要求的条件结构,再与原有固定条件合并。

方法一:使用官方集合过滤器

依赖community.general集合(需提前安装:ansible-galaxy collection install community.general):

- name: 构建完整的Action条件列表
  set_fact:
    action_conditions: >-
      {{
        # 原有固定条件示例:事件类型、主机组过滤
        [
          {"conditiontype": "event_type", "operator": "0", "value": "6"},
          {"conditiontype": "host_group", "operator": "0", "value": "Linux Servers"}
        ]
        # 动态生成所有发现规则的条件
        + discovery_rule_list | map('community.general.zabbix_action_condition', conditiontype='discovery_rule', operator='0') | list
      }}

方法二:手动构造条件结构

如果不想依赖第三方过滤器,可直接通过字符串转换生成:

- name: 构建完整的Action条件列表
  set_fact:
    action_conditions: >-
      {{
        [
          {"conditiontype": "event_type", "operator": "0", "value": "6"},
          {"conditiontype": "host_group", "operator": "0", "value": "Linux Servers"}
        ]
        + discovery_rule_list | map('regex_replace', '^(.*)$', '{"conditiontype": "discovery_rule", "operator": "0", "value": "\\1"}') | map('from_json') | list
      }}

2. 创建/更新整合后的Zabbix Action

将生成的条件列表传入zabbix_action模块,保留原有操作逻辑:

- name: 创建统一发现规则处理Action
  community.general.zabbix_action:
    server_url: "{{ zabbix_server }}"
    login_user: "{{ zabbix_user }}"
    login_password: "{{ zabbix_pass }}"
    name: "统一发现事件处理"
    eventsource: "discovery"
    status: "enabled"
    # 设置条件逻辑:1=逻辑或(所有发现规则满足其一即可触发)
    evaltype: "1"
    conditions: "{{ action_conditions }}"
    operations:
      # 原有操作逻辑,示例:发送通知、添加主机到指定组
      - operationtype: "send_message"
        media_type: ["Email"]
        send_to_user: ["Admin"]
        subject: "发现事件触发"
        message: "发现规则 {DISCOVERY.RULE.NAME} 触发事件,主机: {HOST.NAME}"
      - operationtype: "add_host_to_group"
        host_groups: ["Discovered Hosts"]

3. 关键注意事项

  • 权限验证:确保Ansible使用的Zabbix账号拥有Action的创建/编辑权限
  • 逻辑关系配置:evaltype: "1"表示所有发现规则条件为逻辑或,若需混合逻辑可设置evaltype: "2"并自定义表达式
  • 名称准确性:discovery_rule_list中的名称必须与Zabbix控制台中发现规则的名称完全一致,否则条件无法匹配

内容的提问来源于stack exchange,提问作者w3ich3rt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 17:33:13