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

如何在Grafana Helm Chart的values.yaml中传递通知模板

解决Grafana values.yaml中通知模板{{}}被解析的问题

当在Helm的values.yaml中配置Grafana通知模板时,模板里的{{}}会被Helm的模板引擎误解析,导致内容被忽略或篡改。以下两种方法可以原样传递模板内容:

方法一:用单引号包裹整个模板块

将template:后的多行内容用单引号包裹,同时注意移除模板末尾多余的单引号(你提供的代码最后有个多余的')。修改后的配置如下:

alerting:
  rules.yaml:
    apiVersion: 1
    templates:
      - orgID: 1
        name: Slack Template
        template: |
          '{{ define "alert_severity_prefix_emoji" }}
            {{- if ne .Status "firing" }}
              :white_check_mark:
            {{- else if eq .CommonLabels.severity "critical" }}
              :red_circle:
            {{- else if eq .CommonLabels.severity "warning" }}
              :warning:
            {{ end }}
          {{ end }}
          {{ define "slack.print_alert" }}
            {{ template "alert_severity_prefix_emoji" . }}
            [{{.Status}}] {{ .Labels.alertname }}
            *Labels*:
            {{ range .Labels.SortedPairs }}
              - {{ .Name }}: {{ .Value }}
            {{ end }}
            {{ if .Annotations }}
            *Annotations*:
              {{ range .Annotations.SortedPairs }}
                - {{ .Name }}: {{ .Value }}
              {{ end }}
            {{ end }}
            {{ if .SilenceURL }}
              *Silence*: {{ .SilenceURL }}
            {{ end }}
            {{ if .DashboardURL -}}
              *Go to dashboard*: {{ .DashboardURL }}
            {{ end }}
          {{ end }}
          {{ define "slack.message" -}}
            {{ if .Alerts.Firing -}}
              {{ len .Alerts.Firing }} firing alert(s):
              {{ range .Alerts.Firing }}
                {{ template "slack.print_alert" . }}
              {{ end }}
            {{ end }}
            {{ if .Alerts.Resolved }}
              {{ len .Alerts.Resolved }} resolved alert(s):
              {{ range .Alerts.Resolved }}
                {{ template "slack.print_alert" .}}
              {{ end }}
            {{ end }}
          {{ end }}'

方法二:使用Helm的转义语法

如果不想用单引号包裹,也可以将模板中的每个{{替换为{{ "{{" }},每个}}替换为{{ "}}" }},这样Helm会将其解析为原样的{{}}。示例片段:

template: |
  {{ "{{" }} define "alert_severity_prefix_emoji" {{ "}}" }}
    {{ "{{" }}- if ne .Status "firing" {{ "}}" }}
      :white_check_mark:
    {{ "{{" }}- else if eq .CommonLabels.severity "critical" {{ "}}" }}
      :red_circle:
    {{ "{{" }}- else if eq .CommonLabels.severity "warning" {{ "}}" }}
      :warning:
    {{ "{{" }} end {{ "}}" }}
  {{ "{{" }} end {{ "}}" }}

不过这种方法需要修改所有的{{}},比较繁琐,推荐优先使用方法一。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 04:28:20