ruby/hash:Chef::Node::ImmutableMash 在将 Chef 属性转换为 yaml 时包含在文件中

ruby/hash:Chef::Node::ImmutableMash included in file when converting Chef attributes to yaml

我正在尝试从属性子集编写 .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 输出?

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

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

Note: you'll need to be using chef-client 11.10.0 or later, because there used to be a bug with the to_hash method. More info here: