【问题标题】:Sorting a YAML output in an ERB template在 ERB 模板中对 YAML 输出进行排序
【发布时间】: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_emitPsych 中似乎是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


【解决方案1】:

事实证明,问题的根源是ZAMLPuppet 用于性能目的的 YAML 替换。

可以通过在没有Puppet 的情况下测试这个erb-模板来证明。

我很确定它会起作用。

我正在调查是否允许他们一起工作。

更新:

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

这可能会治愈问题,至少它确实与我的问题非常相似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2017-07-17
    • 2012-08-27
    • 2020-03-23
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多