我有以下 my_script.ruby
require 'erb'
require 'ostruct'
namespace = OpenStruct.new(:jmx_port => 9200, :markets=> ['CH', 'FR'])
template = File.open("my.json.erb", "rb").read;
puts ERB.new(template).result(namespace.instance_eval { binding })
和 my.json.erb:
{
"servers" : [ {
"port" : "<%= jmx_port %>",
"host" : "localhost",
"queries" : [
<% @markets.each do |market| -%>
{
"outputWriters" : [ {
"@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter",
} ],
"obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
"attr" : [ "hits","hitratio"]
},
<% end -%>
],
"numQueryThreads" : 2
} ]
}
但执行脚本时irb my_script.ruby
出现此错误:
?> puts ERB.new(template).result(namespace.instance_eval { binding })
SyntaxError: compile error
(erb):10: syntax error, unexpected ';'
; @markets.each do |market| -; _erbout.concat "\n "
^
对于上下文,my.json.erb 文件是一个 puppet 文件,并且使用 my_script.ruby 我试图验证该文件是否正确,然后再将其发送给 puppet。
我究竟做错了什么?
ps:这里是erb模板,puppet使用: http: //docs.puppetlabs.com/guides/templating.html