Chef:未定义的节点属性或方法`<<'尝试添加时在`node'上

时间:2015-03-05 13:26:14

标签: ruby chef chef-recipe

在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'

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

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