【问题标题】:ruby/hash:Chef::Node::ImmutableMash included in file when converting Chef attributes to yaml将 Chef 属性转换为 yaml 时,文件中包含 ruby​​/hash:Chef::Node::ImmutableMash
【发布时间】:2015-11-03 15:35:29
【问题描述】:

我正在尝试从属性子集编写.yml 文件。这是我正在使用的资源定义:

file '/home/user/file.yml' do
  owner 'user'
  group 'user'
  mode '0755'
  content node['default']['properties'].to_yaml
end

当我运行它时,file.yml 最终看起来像这样:

--- !ruby/hash:Chef::Node::ImmutableMash
config: !ruby/hash:Chef::Node::ImmutableMash
  example: value
  another: value

如何在没有所有 !ruby/hash:Chef::Node::ImmutableMash 输出的情况下获得干净的 yaml 输出?

【问题讨论】:

    标签: ruby chef-infra yaml


    【解决方案1】:

    事实证明,您需要做的就是在转换为 yaml 之前将属性显式转换为哈希。这是工作代码:

    file '/home/user/file.yml' do
      owner 'user'
      group 'user'
      mode '0755'
      content node['default']['properties'].to_hash.to_yaml
    end
    

    注意:您需要使用 chef-client 11.10.0 或更高版本,因为 to_hash 方法曾经有一个错误。更多信息在这里: https://stackoverflow.com/a/14782389/1688034

    【讨论】:

      猜你喜欢
      • 2015-01-23
      • 2020-05-27
      • 1970-01-01
      • 2020-11-15
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 2013-07-08
      相关资源
      最近更新 更多