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

风流意气都作罢 提交于 2019-12-02 10:26:55

问题


In my attributes file for postgresql recipe I have:

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

I want to my recipe automatically add my servers to pg_hga config file like this:

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'

But I'm receiving an error:

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:


Your problem is here:

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

This way you're accessing the attribute for reading.

Assuming you want to stay at default level you have to use default method like this:

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

This will call default method (like in attribute file) to add the entry.

For this to work the first attribute declaration should be an array (or a hash of hash) like this:

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


来源:https://stackoverflow.com/questions/28878933/chef-undefined-node-attribute-or-method-on-node-when-trying-to-add

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!