【发布时间】:2013-02-28 08:32:52
【问题描述】:
在 Puppet 使用的 ERB 模板中,我尝试对哈希的 YAML 输出进行排序,以确保它始终是相同的输出。
这是我目前所拥有的(mytest/templates/hash.erb):
<%=
class MyHash < Hash
def to_yaml( opts = {} )
YAML::quick_emit( self, opts ) do |out|
out.map( taguri, to_yaml_style ) do |map|
keys.sort.each do |k|
v = self[k]
map.add( k, v )
end
end
end
end
end
myScope = scope.to_hash.reject{|k,v| k.to_s =~ /(uptime|timestamp|free)/}
MyHash[myScope].to_yaml
-%>
产生:
$ puppet apply -e 'include mytest' --modulepath .
Failed to parse template mytest/hash.erb:
Filepath: /usr/lib/ruby/1.8/yaml.rb
Line: 391
Detail: wrong argument type String (expected Data)
at /home/rpinson/bas/puppet/mytest/manifests/init.pp:3 on node foo.example.com
这是mytest/manifests/init.pp的内容:
class mytest {
notify { 'toto':
message => template('mytest/hash.erb'),
}
}
我似乎无法理解这种类型的 Data 来自何处,以及如何正确转换参数以使其正常工作……
【问题讨论】:
-
您能至少给我们看一下您的
/home/rpinson/bas/puppet/mytest/manifests/init.pp文件吗?顺便说一句,quick_emit在Psych中似乎是to be deprecated。你使用Psych作为YAML实现吗? -
我在问题中添加了
init.pp。我不知道用什么 YAML 实现来说实话。 -
好的,问题肯定出在
myScope。你会在模板的最后一行使用puts myScope而不是MyHash[myScope].to_yaml并显示输出吗? -
如果我在代码中执行
p myScope,它将返回与节点的事实相对应的键/值对的标准散列。此外,返回myScope.to_yaml也可以正常工作。 -
工作正常并且打印什么?而且,事实上,你为什么决定使用
quick_emit?为什么不直接转至key.to_yaml?
标签: ruby types yaml erb puppet