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

Ansible Zabbix模块问题:如何通过变量批量绑定主机模板

Ansible Zabbix模块多模板分配问题解决

问题场景

使用Ansible Zabbix模块给单台主机分配多个模板时,通过Inventory变量传递模板列表失败,但直接在任务中写死模板名称可正常执行。

错误的任务代码

- name: Create/update Zabbix host
[...]
  link_templates:
    - '{{ zabbix_templates }}'
[...]

可正常运行的任务代码

- name: Create/update Zabbix host
[...]
  link_templates:
    - "Zabbix agent active"
    - "Linux by Zabbix agent"
[...]

Inventory配置

all:
  hosts:
[...]
  children:
 [...]-cluster:
      hosts:
      [...]
      vars:
        kubernetes_cluster: yes
        cluster_name: [...]-cluster
        OS: ubuntu_22
        zabbix_templates:
          - Zabbix agent active
          - Linux by Zabbix agent
        zabbix_hostgroup: Linux servers

收到的错误信息

fatal: [[...]]: FAILED! => {"changed": false, "msg": "Template not found: ['Zabbix agent active', 'Linux by Zabbix agent']"}

解决方案

错误写法的核心问题是:将整个zabbix_templates列表作为单个列表项传递给了link_templates,导致模块把整个列表字符串当作一个模板名称去查找,自然找不到。

正确的写法是直接将变量赋值给link_templates,因为zabbix_templates本身就是符合要求的一维列表:

- name: Create/update Zabbix host
[...]
  link_templates: '{{ zabbix_templates }}'
[...]

或者使用双引号的YAML语法:

- name: Create/update Zabbix host
[...]
  link_templates: "{{ zabbix_templates }}"
[...]

这样模块会正确识别变量中的每一个模板名称,完成多模板的分配。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 10:13:17