我不是一位厨师专家,我很可能会忽略一些愚蠢的事情。我试图在厨师食谱(厨师客户端12.5.1)中迭代哈希并且正在接收未定义的方法`每个'为零: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|
漂亮的打印在红宝石过程中显示变量的内容。
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 最初是实例,但我改变了它,看它是否偶然与某事发生冲突;但是,内容可以在irb中迭代。谁能告诉我我做错了什么?
编辑:我也在厨师客户端12.14.89上运行它并收到相同的结果。
答案 0 :(得分:1)
可能是default['haproxy']['shared']
行,没有frontends
键。你的意思是将那个改为haproxy-shared
吗?打印instance_name
以确定。请记住nil.to_s == ''
,这样您可能无法看到预期的日志记录。