community.zabbix.zabbix_action 模块 – 创建/删除/更新 Zabbix 动作

注意

此模块是 community.zabbix 集合 (版本 3.2.0) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.zabbix。您需要其他要求才能使用此模块,有关详细信息,请参阅 要求

要在剧本中使用它,请指定:community.zabbix.zabbix_action

概要

  • 此模块允许您创建、修改和删除 Zabbix 动作。

要求

执行此模块的主机需要以下要求。

  • python >= 3.9

参数

参数

注释

acknowledge_operations

别名:update_operations

列表 / 元素=字典

确认操作列表。

从 Zabbix 4.0 开始,操作确认操作被称为更新操作。

子选项 与 *operations* 的相同。

默认值: []

command

字符串

要运行的命令。

command_type

字符串

操作命令的类型。

选项

  • "custom_script"

  • "ipmi"

  • "ssh"

  • "telnet"

  • "global_script"

execute_on

字符串

将执行自定义脚本操作命令的目标。

选项

  • "agent"

  • "server"

  • "proxy"

media_type

字符串

将用于发送消息的媒体类型。

可在 *acknowledge_operations* 内与 *type=send_message* 或 *type=notify_all_involved* 一起使用。

对于所有媒体类型,设置为 all

默认值: "all"

op_message

字符串

操作消息文本。

如果未定义 *op_message* 和 *subject*,则将使用媒体类型的“默认消息”。

password

字符串

用于身份验证的密码。

当 *ssh_auth_type=password* 或 *command_type=telnet* 时必填。

可在 *type=remote_command* 时使用。

port

整数

用于身份验证的端口号。

可在 *command_type in [ssh, telnet]* 和 *type=remote_command* 时使用。

run_on_groups

列表 / 元素=字符串

要在其上运行远程命令的主机组。

当 *type=remote_command* 且未设置 *run_on_hosts* 时必填。

run_on_hosts

列表 / 元素=字符串

要在其上运行远程命令的主机。

当 *type=remote_command* 且未设置 *run_on_groups* 时必填。

如果设置为 0,则命令将在当前主机上运行。

script_name

字符串

用于全局脚本命令的脚本名称。

当 *command_type=global_script* 时必填。

可在 *type=remote_command* 时使用。

send_to_groups

列表 / 元素=字符串

要向其发送消息的用户组。

send_to_users

列表 / 元素=字符串

要向其发送消息的用户(用户名或别名)。

ssh_auth_type

字符串

用于 SSH 命令的身份验证方法。

当 *type=remote_command* 和 *command_type=ssh* 时必填。

选项

  • "password"

  • "public_key"

ssh_privatekey_file

字符串

用于使用公钥身份验证的 SSH 命令的私钥文件名。

当 *ssh_auth_type=public_key* 时必填。

可在 *type=remote_command* 时使用。

ssh_publickey_file

字符串

用于使用公钥身份验证的 SSH 命令的公钥文件名。

当 *ssh_auth_type=public_key* 时必填。

可在 *type=remote_command* 时使用。

subject

字符串

操作消息主题。

如果未定义 *op_message* 和 *subject*,则将使用媒体类型的“默认消息”。

type

字符串 / 必填

操作类型。

选项

  • "send_message"

  • "remote_command"

  • "notify_all_involved"

username

字符串

用于身份验证的用户名。

当 *ssh_auth_type in [public_key, password]* 或 *command_type=telnet* 时必填。

可在 *type=remote_command* 时使用。

conditions

列表 / 元素=字典

用于过滤结果的条件列表。

有关此选项子选项的更多信息,请查看 Zabbix API 文档 https://www.zabbix.com/documentation/5.0/manual/api/reference/action/object#action_filter_condition

默认值: []

formulaid

字符串

用于从自定义表达式引用条件的任意唯一 ID。

只能包含大写字母。

仅在自定义表达式过滤器中需要,否则忽略。

运算符

字符串 / 必填

条件运算符。

type设置为time_period时,可选值为in, not in

选项

  • "等于"

  • "="

  • "不等于"

  • "<>"

  • "包含"

  • "匹配"

  • "不包含"

  • "不匹配"

  • "属于"

  • "大于等于"

  • ">="

  • "小于等于"

  • "<="

  • "不属于"

  • "匹配"

  • "不匹配"

  • "是"

  • "否"

类型

字符串 / 必填

条件的类型(标签)。

event_source=trigger时的可能值

  • 主机组

  • 主机

  • 触发器

  • 触发器名称

  • 触发器严重性

  • 时间段

  • 主机模板

  • maintenance_status 在Zabbix 4.0及以上版本中称为“问题已抑制”

  • 事件标签

  • 事件标签值

event_source=discovery时的可能值

  • 主机IP

  • 已发现的服务类型

  • 已发现的服务端口

  • 发现状态

  • 运行时间或停机时间

  • 接收到的值

  • 发现规则

  • 发现检查

  • 代理

  • 发现对象

event_source=auto_registration时的可能值

  • 代理

  • 主机名

  • 主机元数据

event_source=internal时的可能值

  • 主机组

  • 主机

  • 主机模板

  • 事件类型

字符串

用于比较的值。

type=discovery_status时,可选值为

  • 正常

  • 异常

  • 已发现

  • 已丢失

type=discovery_object时,可选值为

  • 主机

  • 服务

type=event_type时,可选值为

  • 项处于不支持状态

  • 项处于正常状态

  • LLD规则处于不支持状态

  • LLD规则处于正常状态

  • 触发器处于未知状态

  • 触发器处于正常状态

type=trigger_severity时,可选值为(不区分大小写)

  • 未分类

  • 信息

  • 警告

  • 平均

  • 严重

  • 灾难

无论Zabbix中用户可见的名称是否更改。如果省略,则默认为not classified

除了以上选项外,这通常是对象的名称或要比较的字符串。

value2

字符串

用于比较的第二个值。

当条件type=event_tag_value时,触发器操作需要此参数。

esc_period

字符串

默认操作步骤持续时间。必须大于60秒。

接受秒、带后缀的时间单位和用户宏(自Zabbix 3.4起)。

state=present时需要。

eval_type

字符串

筛选条件评估方法。

如果条件少于2个或未指定formula,则默认为andor

当指定formula时,默认为custom_expression

选项

  • "andor"

  • "and"

  • "or"

  • "custom_expression"

event_source

字符串

操作将处理的事件类型。

state=present时需要。

选项

  • "触发器"

  • "发现"

  • "自动注册"

  • "内部"

formula

字符串

用于使用自定义表达式评估条件的用户定义表达式。

表达式必须包含引用每个条件的formulaid的ID。

表达式中使用的ID必须与conditions中定义的ID完全匹配。任何条件都不能未使用或省略。

eval_type=custom_expression时需要。

使用从“A”开始的连续ID。如果使用非连续ID,Zabbix将重新索引它们。这使得每个模块运行都会注意到ID的差异并更新操作。

http_login_password

字符串

基本认证密码

http_login_user

字符串

基本认证用户名

名称

字符串 / 必填

操作的名称

notify_if_canceled

布尔值

是否在取消升级时发出通知。

可在event_source=trigger时使用。

选项

  • ← (默认)

operations

列表 / 元素=字典

操作列表

默认值: []

命令

字符串

要运行的命令。

type=remote_commandcommand_type!=global_script时需要。

command_type

字符串

操作命令的类型。

type=remote_command时需要。

选项

  • "custom_script"

  • "ipmi"

  • "ssh"

  • "telnet"

  • "global_script"

esc_period

字符串

升级步骤持续时间(秒)。

必须大于60秒。

接受秒、带后缀的时间单位和用户宏。

如果设置为0或0s,将使用默认操作升级周期。

默认: "0s"

esc_step_from

整数

开始升级的步骤。

默认: 1

esc_step_to

整数

结束升级的步骤。

指定0表示无限。

默认: 1

execute_on

字符串

将执行自定义脚本操作命令的目标。

type=remote_commandcommand_type=custom_script时需要。

选项

  • "agent"

  • "server"

  • "proxy"

host_groups

列表 / 元素=字符串

主机应添加到的主机组列表。

type=add_to_host_grouptype=remove_from_host_group时需要。

inventory

字符串

主机清单模式。

type=set_host_inventory_mode时需要。

选项

  • "手动"

  • "自动"

media_type

字符串

将用于发送消息的媒体类型。

可在 *acknowledge_operations* 内与 *type=send_message* 或 *type=notify_all_involved* 一起使用。

对于所有媒体类型,设置为 all

默认值: "all"

op_message

字符串

操作消息文本。

如果未定义 *op_message* 和 *subject*,则将使用媒体类型的“默认消息”。

operation_condition

字符串

操作条件对象定义必须满足才能执行当前操作的条件。

选项

  • "已确认"

  • "未确认"

密码

字符串

用于身份验证的密码。

当 *ssh_auth_type=password* 或 *command_type=telnet* 时必填。

可在 *type=remote_command* 时使用。

端口

整数

用于身份验证的端口号。

可在 *command_type in [ssh, telnet]* 和 *type=remote_command* 时使用。

run_on_groups

列表 / 元素=字符串

要在其上运行远程命令的主机组。

当 *type=remote_command* 且未设置 *run_on_hosts* 时必填。

run_on_hosts

列表 / 元素=字符串

要在其上运行远程命令的主机。

当 *type=remote_command* 且未设置 *run_on_groups* 时必填。

如果设置为 0,则命令将在当前主机上运行。

script_name

字符串

用于全局脚本命令的脚本名称。

当 *command_type=global_script* 时必填。

可在 *type=remote_command* 时使用。

send_to_groups

列表 / 元素=字符串

要向其发送消息的用户组。

send_to_users

列表 / 元素=字符串

要向其发送消息的用户(用户名或别名)。

ssh_auth_type

字符串

用于 SSH 命令的身份验证方法。

当 *type=remote_command* 和 *command_type=ssh* 时必填。

选项

  • "password"

  • "public_key"

ssh_privatekey_file

字符串

用于使用公钥身份验证的 SSH 命令的私钥文件名。

当 *ssh_auth_type=public_key* 时必填。

可在 *type=remote_command* 时使用。

ssh_publickey_file

字符串

用于使用公钥身份验证的 SSH 命令的公钥文件名。

当 *ssh_auth_type=public_key* 时必填。

可在 *type=remote_command* 时使用。

主题

字符串

操作消息主题。

如果未定义 *op_message* 和 *subject*,则将使用媒体类型的“默认消息”。

templates

列表 / 元素=字符串

主机应链接到的模板列表。

type=link_to_templatetype=unlink_from_template时需要。

类型

字符串 / 必填

操作类型。

设置recovery_operationsacknowledge_operations类型的有效选择

  • 发送消息

  • 远程命令

  • notify_all_involved

选择notify_all_involved仅在recovery_operationsacknowledge_operations中受支持。

选项

  • "send_message"

  • "remote_command"

  • "添加主机"

  • "移除主机"

  • "添加到主机组"

  • "从主机组移除"

  • "链接到模板"

  • "从模板取消链接"

  • "启用主机"

  • "禁用主机"

  • "设置主机清单模式"

  • "notify_all_involved"

用户名

字符串

用于身份验证的用户名。

当 *ssh_auth_type in [public_key, password]* 或 *command_type=telnet* 时必填。

可在 *type=remote_command* 时使用。

pause_in_maintenance

布尔值

是否在维护期间暂停升级。

可在event_source=trigger时使用。

选项

  • ← (默认)

pause_symptoms

布尔值

如果事件是症状事件,是否暂停升级。

如果event_source设置为trigger受支持

仅适用于Zabbix 6.4及以上版本

选项

  • ← (默认)

recovery_operations

列表 / 元素=字典

恢复操作列表。

子选项 与 *operations* 的相同。

默认值: []

命令

字符串

要运行的命令。

command_type

字符串

操作命令的类型。

选项

  • "custom_script"

  • "ipmi"

  • "ssh"

  • "telnet"

  • "global_script"

execute_on

字符串

将执行自定义脚本操作命令的目标。

选项

  • "agent"

  • "server"

  • "proxy"

media_type

字符串

将用于发送消息的媒体类型。

可在 *acknowledge_operations* 内与 *type=send_message* 或 *type=notify_all_involved* 一起使用。

对于所有媒体类型,设置为 all

默认值: "all"

op_message

字符串

操作消息文本。

如果未定义 *op_message* 和 *subject*,则将使用媒体类型的“默认消息”。

密码

字符串

用于身份验证的密码。

当 *ssh_auth_type=password* 或 *command_type=telnet* 时必填。

可在 *type=remote_command* 时使用。

端口

整数

用于身份验证的端口号。

可在 *command_type in [ssh, telnet]* 和 *type=remote_command* 时使用。

run_on_groups

列表 / 元素=字符串

要在其上运行远程命令的主机组。

当 *type=remote_command* 且未设置 *run_on_hosts* 时必填。

run_on_hosts

列表 / 元素=字符串

要在其上运行远程命令的主机。

当 *type=remote_command* 且未设置 *run_on_groups* 时必填。

如果设置为 0,则命令将在当前主机上运行。

script_name

字符串

用于全局脚本命令的脚本名称。

当 *command_type=global_script* 时必填。

可在 *type=remote_command* 时使用。

send_to_groups

列表 / 元素=字符串

要向其发送消息的用户组。

send_to_users

列表 / 元素=字符串

要向其发送消息的用户(用户名或别名)。

ssh_auth_type

字符串

用于 SSH 命令的身份验证方法。

当 *type=remote_command* 和 *command_type=ssh* 时必填。

选项

  • "password"

  • "public_key"

ssh_privatekey_file

字符串

用于使用公钥身份验证的 SSH 命令的私钥文件名。

当 *ssh_auth_type=public_key* 时必填。

可在 *type=remote_command* 时使用。

ssh_publickey_file

字符串

用于使用公钥身份验证的 SSH 命令的公钥文件名。

当 *ssh_auth_type=public_key* 时必填。

可在 *type=remote_command* 时使用。

主题

字符串

操作消息主题。

如果未定义 *op_message* 和 *subject*,则将使用媒体类型的“默认消息”。

类型

字符串 / 必填

操作类型。

选项

  • "send_message"

  • "remote_command"

  • "notify_all_involved"

用户名

字符串

用于身份验证的用户名。

当 *ssh_auth_type in [public_key, password]* 或 *command_type=telnet* 时必填。

可在 *type=remote_command* 时使用。

状态

字符串

操作的状态。

对于present,如果操作不存在,则会创建一个操作;如果关联数据不同,则会更新操作。

对于absent,如果操作存在,则会移除操作。

选项

  • "present" ← (默认)

  • "absent"

状态

字符串

操作的状态。

选项

  • "已启用" ← (默认)

  • "已禁用"

示例

# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
  ansible.builtin.set_fact:
    ansible_user: Admin
    ansible_httpapi_pass: zabbix

# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
  ansible.builtin.set_fact:
    ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895

# Trigger action with only one condition
- name: Deploy trigger action
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_action:
    name: "Send alerts to Admin"
    event_source: "trigger"
    state: present
    status: enabled
    esc_period: 60
    conditions:
      - type: "trigger_severity"
        operator: ">="
        value: "Information"
    operations:
      - type: send_message
        subject: "Something bad is happening"
        op_message: "Come on, guys do something"
        media_type: "Email"
        send_to_users:
          - "Admin"

# Trigger action with multiple conditions and operations
- name: Deploy trigger action
  # set task level  variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_action:
    name: "Send alerts to Admin"
    event_source: "trigger"
    state: present
    status: enabled
    esc_period: 1m
    conditions:
      - type: "trigger_name"
        operator: "like"
        value: "Zabbix agent is unreachable"
        formulaid: A
      - type: "trigger_severity"
        operator: ">="
        value: "disaster"
        formulaid: B
    formula: A or B
    operations:
      - type: send_message
        media_type: "Email"
        send_to_users:
          - "Admin"
      - type: remote_command
        command: "systemctl restart zabbix-agent"
        command_type: custom_script
        execute_on: server
        run_on_hosts:
          - 0

# Trigger action with recovery and acknowledge operations
- name: Deploy trigger action
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_action:
    name: "Send alerts to Admin"
    event_source: "trigger"
    state: present
    status: enabled
    esc_period: 1h
    conditions:
      - type: "trigger_severity"
        operator: ">="
        value: "Information"
    operations:
      - type: send_message
        subject: "Something bad is happening"
        op_message: "Come on, guys do something"
        media_type: "Email"
        send_to_users:
          - "Admin"
    recovery_operations:
      - type: send_message
        subject: "Host is down"
        op_message: "Come on, guys do something"
        media_type: "Email"
        send_to_users:
          - "Admin"
    acknowledge_operations:
      - type: send_message
        media_type: "Email"
        send_to_users:
          - "Admin"

返回值

常见的返回值已在此处记录,以下是此模块特有的字段

描述

msg

字符串

操作的结果

返回:成功

示例: "操作已删除:注册web服务器,ID:0001"

作者

  • Ruben Tsirunyan (@rubentsirunyan)

  • Ruben Harutyunov (@K-DOT)