nil:NilClass 的未定义方法“each”在厨师食谱中迭代哈希时

undefined method `each' for nil:NilClass when iterating over hash in chef recipe

我不完全是厨师专家,我很可能会忽略一些愚蠢的事情。我正在尝试遍历厨师食谱(厨师客户端 12.5.1)中的哈希,并且正在接收 undefined method `each' for nil:NilClass

我也不知道为什么,也许我看得太久了。感谢任何帮助。

属性

default['haproxy']['default']['frontends']['main'] =
  { socket: '*:5000', default_backend: 'default-backend' }

default['haproxy']['default']['frontends']['http'] =
  { socket: '*:80', default_backend: 'default-backend' }

default['haproxy']['test']['frontends']['main'] =
  { socket: '*:6000', default_backend: 'test-backend' }

default['haproxy']['test']['frontends']['https'] =
  { socket: '*:443', default_backend: 'test-backend' }

default['haproxy-shared']['default-backend']['servers'] = [
  { name: 'app1', socket: '127.0.0.1:5001', options: ['check'] },
  { name: 'app2', socket: '127.0.0.1:5002', options: ['check'] },
  { name: 'app3', socket: '127.0.0.1:5003', options: ['check'] },
  { name: 'app4', socket: '127.0.0.1:5004', options: ['check'] }
]

default['haproxy']['shared']['default-backend']['options'] = [
  'balance roundrobin',
  'option httpchk HEAD / HTTP/1.1\r\nHost:localhost'
]

default['haproxy-shared']['test-backend']['servers'] = [
  { name: 'app1', socket: '127.0.0.1:5001', options: ['check'] }
]

配方错误

注意:我在 pp 中添加是为了解决问题。

   undefined method `each' for nil:NilClass

   Cookbook Trace:
   ---------------
     /tmp/kitchen/cache/cookbooks/ga_haproxy_default/recipes/default.rb:12:in `block in from_file'
     /tmp/kitchen/cache/cookbooks/ga_haproxy_default/recipes/default.rb:8:in `each'
     /tmp/kitchen/cache/cookbooks/ga_haproxy_default/recipes/default.rb:8:in `from_file'

   Relevant File Content:
   ----------------------
   /tmp/kitchen/cache/cookbooks/ga_haproxy_default/recipes/default.rb:

     5:  # Copyright (c) 2016 Sean McGowan, All Rights Reserved.
     6:  require 'pp'
     7:
     8:  node['haproxy'].each do |instance_name, inst|
     9:    ga_haproxy instance_name
    10:
    11:  pp inst['frontends']
    12>>   inst['frontends'].each do |fe_name, frontend|
    13:      ga_haproxy_frontend fe_name do
    14:        instance_name instance_name
    15:        socket frontend['socket']
    16:        default_backend frontend['default_backend']
    17:        action :enable
    18:      end
    19:    end
    20:
    21:    node['haproxy-shared'].each do |be_name, backend|

漂亮的打印显示 ruby 遍期间变量的内容。

   Synchronizing Cookbooks:
     - ga_haproxy (0.1.1)
     - ga_haproxy_default (0.1.0)
   Compiling Cookbooks...
   {"main"=>{"socket"=>"*:5000", "default_backend"=>"default-backend"},
    "http"=>{"socket"=>"*:80", "default_backend"=>"default-backend"}}
   {"main"=>{"socket"=>"*:6000", "default_backend"=>"test-backend"},
    "https"=>{"socket"=>"*:443", "default_backend"=>"test-backend"}}

inst 原本是 instance 但我改了一下,看看它是否偶然与某些东西发生冲突;然而,内容在 irb 中是可迭代的。谁能告诉我我做错了什么?

编辑:我也 运行 它针对 chef 客户端 12.14.89 并收到相同的结果。

可能是 default['haproxy']['shared'] 行,没有 frontends 键。您是否也想将那个更改为 haproxy-shared?打印出 instance_name 以确保。请记住 nil.to_s == '',这样您可能看不到预期的日志记录。