【问题标题】:in chef: Undefined node attribute or method `<<' on `node'在厨师中:“节点”上未定义的节点属性或方法“<<”
【发布时间】:2018-01-22 21:11:44
【问题描述】:

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

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

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

如上所述,这在测试厨房中有效,在生产中的 chef-shell 中有效,但在 prod 中运行配方会引发以下问题:

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 对象上使用优先级,但我这样做了,只是与链接中显示的不同。

我可能在这里遗漏了一些愚蠢的东西,但它以前工作过,现在还在厨房里工作,所以我没地方看。会不会是某种在chef-zero 中无法复制的奇怪编译问题?

【问题讨论】:

  • 您使用的是什么版本的 Chef?
  • 最新 (12.17.44)。但我试过降级到其他版本,结果是一样的

标签: chef-infra


【解决方案1】:

你可能需要做的是:

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

【讨论】:

    【解决方案2】:

    我觉得你想要的逻辑更像这样

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

    【讨论】:

    • 似乎不起作用。 chef (12.17.44)&gt; node.normal['blah']['boo'] |= %w{bots} NoMethodError: Undefined node attribute or method |'改为node'. To set an attribute, use |=value'。`
    最近更新 更多