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

如何通过yasha从Makefile传递字典到Jinja .j2模板

问题排查与解决

1. 修正Makefile变量定义

你当前的Dict格式既不合法,也不符合Makefile语法要求,必须调整为标准JSON数组,同时注意Makefile变量赋值的语法(=前后不能有空格):

Dict='[{"AccountId": {"Key": "x", "Value": "y"}}, {"AccountId": {"Key": "x", "Value": "b"}}]'
  • 用单引号包裹整个JSON,避免shell解析双引号导致结构破坏;
  • 原写法重复AccountId键会被直接覆盖,改成数组中每个元素为独立对象,保证数据完整性。

2. 修正yasha参数传递方式

Makefile中执行命令时,要确保变量完整传递,避免shell拆分参数。推荐两种方式:

方式1:命令行参数传递

generate:
	yasha --tag_list=$(Dict) -o test.yaml test.yaml.j2

方式2:环境变量传递(更可靠,适合复杂结构)

export Dict='[{"AccountId": {"Key": "x", "Value": "y"}}, {"AccountId": {"Key": "x", "Value": "b"}}]'
generate:
	yasha -o test.yaml test.yaml.j2

3. 修正Jinja模板遍历逻辑

模板中需要对应传递的数据结构进行遍历,原写法无法正确访问嵌套字段,调整如下:

如果用命令行参数传递(tag_list)

{% for item in tag_list %}
name: test-{{ item.AccountId.Key }}-{{ item.AccountId.Value }}
{% endfor %}

如果用环境变量传递(Dict)

{% for item in Dict %}
name: test-{{ item.AccountId.Key }}-{{ item.AccountId.Value }}
{% endfor %}

4. 调试验证

可以先添加调试目标确认变量是否正确输出:

debug:
	@echo $(Dict)

执行make debug,确认输出为完整合法的JSON字符串。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 17:42:50