【问题标题】:Chef: Undefined node attribute or method `<<' on `node' when trying to add厨师:尝试添加时“节点”上未定义的节点属性或方法“<<”
【发布时间】:2015-05-06 21:16:06
【问题描述】:

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

【问题讨论】:

    标签: ruby chef-infra chef-recipe


    【解决方案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
    

    【讨论】:

    • 以及在节点定义中。请使用default['postgresql']['pg_hba'] = [{ :comment =&gt; '# IPv4 local connections', :type =&gt; 'host', :db =&gt; 'all', :user =&gt; 'all', :addr =&gt; '127.0.0.1/32', :method =&gt; 'md5' }] 改进答案
    • @Wbar 问题是关于厨师食谱的属性访问,我回答这个问题,没有兴趣重复你原来的问题,这是一个属性文件,而不是节点定义
    • 我错过了数组声明。在属性会话中查看我的问题并再次查看我的评论
    • 哦,确实,添加到哈希中会合并而不是添加条目,我会添加这个明天回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多