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

如何在Helm中用命名模板的多行输出设置变量?

模板变量赋值问题

现有用于测试的简化多行命名模板:

{{- define "sample.callee" }}
- |
  echo "test 1"; \
  echo "test 2";
{{- end }}

需要在另一个命名模板中,参照如下示例(已移除其他逻辑)将上述模板的输出赋值给变量:

{{- define "sample.caller" }}
{{- $commands := include "sample.callee" . }}
{{- end }}

当前实现方式

目前只能通过将模板输出转为映射再取值的方式得到列表:

{{ $commands := ( get ( ( printf "list:
 %s" ( include "sample.callee" . ) ) | fromYaml ) "list" ) }}

更简洁的实现方法

不需要额外包装成键值对,因为sample.callee输出的内容本身就是合法的YAML列表格式,直接用fromYaml过滤器解析即可:

{{- define "sample.caller" }}
{{- $commands := include "sample.callee" . | fromYaml }}
{{- end }}

此时$commands就是一个列表类型变量,列表中的第一个元素就是包含两行echo命令的多行字符串,可直接在模板中使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 14:45:04