问题
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