简体   繁体   English

在Chef中:未定义节点属性或“节点”上的方法“ <<”

[英]in chef: Undefined node attribute or method `<<' on `node'

Another frustrating matter of works in testing not in prod. 测试不在产品中的另一个令人沮丧的问题。

The code is question appends a string to a node attribute. 代码是问题,将字符串附加到节点属性。

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

This, as mentioned above works in test kitchen, in chef-shell in production, but running the recipe in prod throws the following: 如上所述,这可以在测试厨房中使用,也可以在生产中使用厨师用壳,但是在生产中运行配方会引发以下情况:

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:  

The questions I found on the subject (for example Chef: Undefined node attribute or method `<<' on `node' when trying to add ) refer to not using a priority on the node object, but I do, just a different one from that presented in the link. 我在该主题上发现的问题(例如Chef:未定义的节点属性或试图添加时在“ node”上使用方法“ <<” )指的是不在node对象上使用优先级,但我确实与之不同。链接中显示的内容。

I might be missing something stupid here, but it worked before, and still works in kitchen, so I ran out of places to look. 我可能在这里错过了一些愚蠢的东西,但是它曾经可以工作,现在仍然可以在厨房工作,所以我没有地方去看。 Could it be some kind of strange compilation issue that does not replicate in chef-zero ? 难道是某种奇怪的编译问题不会在chef-zero复制吗?

what you probably need to do is : 您可能需要做的是:

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

I think the logic you want is more like this 我认为您想要的逻辑更像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM