1

在 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 来自哪里,以及如何正确转换参数以使其正常工作……</p>

4

1 回答 1

0

It turns out, the source of problem is ZAML, YAML replacement which Puppet uses for performance purposes.

It can be proved by testing this erb-template without Puppet.

I'm pretty sure it will work.

I'm investigating to allow to work them together.

UPDATE:

ZAML.dump(MyHash[myScope]) # instead of MyHash[myScope].to_yaml

This may heal problem, at least it does my one that really similar.

于 2013-06-14T17:30:40.630 回答