在chef中:未定义的节点属性或方法`<<<'在`node'

时间:2017-01-24 15:13:41

标签: chef

另一个令人沮丧的问题是测试工作而非生产。

代码是问题,将字符串附加到节点属性。

if node['tom-ssh']['allow_groups']
  if !node['tom-ssh']['allow_groups'].include?("bots")
    node.normal['tom-ssh']['allow_groups'] << "bots"
  end
else
    node.normal['tom-ssh']['allow_groups'] = ["bots"]
end

如上所述,这可以在测试厨房中使用,也可以在生产中的厨师贝壳中使用,但在生产中运行配方会引发以下情况:

NoMethodError
-------------
Undefined node attribute or method `<<' on `node'. To set an attribute, use `<<=value' instead.

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/tom-users/recipes/reboot_bot.rb:22:in `from_file'
  /var/chef/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/run_context.rb:347:in `load_recipe'
  /var/chef/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/run_context.rb:303:in `block in include_recipe'
  /var/chef/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/run_context.rb:302:in `each'
  /var/chef/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/run_context.rb:302:in `include_recipe'
  /var/chef/cache/cookbooks/tom-security-patches/recipes/default.rb:8:in `from_file'
  /var/chef/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/run_context.rb:347:in `load_recipe'

Relevant File Content:
----------------------
/var/chef/cache/cookbooks/tom-users/recipes/reboot_bot.rb:

 15:      '/sbin/reboot'
 16:    ]
 17:  end
 18:  
 19:  # allow 'bots' to ssh log in
 20:  if node['tom-ssh']['allow_groups']
 21:    if !node['tom-ssh']['allow_groups'].include?("bots")
 22>>     node.normal['tom-ssh']['allow_groups'] << "bots"
 23:    end
 24:  else
 25:      node.normal['tom-ssh']['allow_groups'] = ["bots"]
 26:  end
 27:  

我在这个主题上发现的问题(例如Chef: Undefined node attribute or method `<<' on `node' when trying to add)是指不使用node对象的优先级,但我这样做,只是与链接中显示的不同。< / p>

我可能在这里错过了一些愚蠢的东西,但它以前工作过,而且仍在厨房里工作,所以我跑出去看看。可能是某种奇怪的编译问题,不能在chef-zero中复制吗?

2 个答案:

答案 0 :(得分:1)

你可能需要做的是:

node.normal ['tom-ssh'] ['allow_groups'] = DeepMerge.merge(node.normal ['tom-ssh'] ['allow_groups']。to_hash,[“bots”]。to_hash)< / p>

答案 1 :(得分:0)

我认为你想要的逻辑更像是这个

node.normal['tom-ssh']['allow_groups'] ||= []
node.normal['tom-ssh']['allow_groups'] |= %w{bots}