如何将and
和eq/ne
函数组合在一起?
我写了这段代码
{{ define "opsgenie.default.tmpl" }}
<font size="+0"><b>{{.CommonLabels.alertname }}</b></font>
{{- range $i, $alert := .Alerts }}
<font size="+0">{{ .Annotations.description }}</font>
{{- end -}}
{{- "\n" -}}
{{- "\n" -}}
{{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
Grafana: https://{{ .CommonLabels.url }}
{{- "\n" -}}{{- end -}}
{{- if and ne .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
Database:
• https://{{ .CommonLabels.url }}/
• https://{{ .CommonLabels.url }}/
{{- "\n" -}}{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
目标是:
- 如果我的alert 同时包含标签
infoalert: true
和topic: database
,则仅显示Grafana链接 - 如果我的alert 只包含标签
topic: database
而不包含infoalert: true
,则仅显示数据缺失链接
条件{{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
的语法看起来不正确,因为在触发alert 时,我在警示管理器.log中收到以下错误:
notify retry canceled due to unrecoverable error after 1 attempts: templating error: template: email.tmpl:24:17: executing \"opsgenie.default.tmpl\" at <eq>: wrong number of args for eq: want at least 1 got 0