Chef:尝试添加时“节点”上未定义的节点属性或方法“<<”

Chef: Undefined node attribute or method `<<' on `node' when trying to add

在我的 postgresql 配方的属性文件中,我有:

default['postgresql']['pg_hba'] = {
    :comment => '# IPv4 local connections',
    :type => 'host',
    :db => 'all',
    :user => 'all',
    :addr => '127.0.0.1/32',
    :method => 'md5'
}

我希望我的食谱自动将我的服务器添加到 pg_hga 配置文件,如下所示:

lambda {
  if Chef::Config[:solo]
    return (Array.new.push node)
  end
  search(:node, "recipes:my_server AND chef_environment:#{node.chef_environment} ")
}.call.each do |server_node|
  node['postgresql']['pg_hba'] << {
      :comment => "# Enabling for #{server_node['ipaddress']}",
      :type => 'host',
      :db => 'all',
      :user => 'all',
      :addr => "#{server_node['ipaddress']}/32",
      :method => 'trust'
  }
end

include_recipe 'postgresql'

但是我收到一个错误:

NoMethodError
-------------
Undefined node attribute or method `<<' on `node'

35:    node['postgresql']['pg_hba'] << {
36:        :comment => "# Enabling for #{server_node['ipaddress']}",
37:        :type => 'host',
38:        :db => 'all',
39:        :user => 'all',
40:        :addr => "#{server_node['ipaddress']}/32",
41:        :method => 'trust'
42>>   }
43:  end
44:  
45:  include_recipe 'postgresql'

您的问题在这里:

node['postgresql']['pg_hba'] << {

这样您就可以访问阅读属性。

假设你想保持默认级别,你必须使用这样的默认方法:

node.default['postgresql']['pg_hba'] << { ... }

这将调用默认方法(如在属性文件中)来添加条目。

要使其正常工作,第一个属性声明应该是一个数组(或散列的散列),如下所示:

default['postgresql']['pg_hba'] = [{ # notice the [ opening an array
    :comment => '# IPv4 local connections',
    :type => 'host',
    :db => 'all',
    :user => 'all',
    :addr => '127.0.0.1/32',
    :method => 'md5'
}] # Same here to close the array