我正在使用 Chef 配置在 AWS 中创建机器。创建机器时,我为其添加了一个属性,即 JSON 文件数组。在我运行的食谱中,我想遍历这个数组并在机器上创建一个模板文件。机器已配置,但在遍历数组属性时,我收到一条错误消息:
nil:NilClass 的未定义方法“每个”
我尝试查看在我的服务器上创建的节点文件,你猜怎么着?JSON 文件数组已添加到节点文件中!我不确定为什么它会不断抛出该错误。有任何想法吗?
代码示例如下:
机器.rb
def get_cluster_json(domain_number)
clusters = []
@@environment_template['domains'][domain_number]['clusters'].each do |cls|
clusters << JSON.parse(::File::read(new_resource.template_path + cls))
end
return clusters
end
provisioning_xyz_machine "test-admin" do
tag "usage:keep"
attribute "clusters_json", get_cluster_json(domain_counter)
recipe admin_role
machine_options get_machine_options()
ohai_hints ohai_hints
action $provisioningAction
end
管理员角色.rb
managed_details = []
node["clusters_json"].each do |cls|
managed_details << "#{cls['cluster']['name']}"
end
日志
* template[/tools/appsw/appsautm/config/INTFIN_config] action create[2015-05-12T10:24:13-07:00] INFO: Processing template[/tools/appsw/appsautm/config/INTFIN_config] action create (xyz-environment-cookbook::build_admin line 32)
================================================================================
Error executing action `create` on resource 'template[/tools/appsw/appsautm/config/INTFIN_config]'
================================================================================
Chef::Mixin::Template::TemplateError
------------------------------------
undefined method 'each' for `nil:NilClass`
Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/xyz-environment-cookbook/recipes/build_admin.rb
32: template "/tools/appsw/appsautm/config/#{env_name}_config" do
33: source "#{env_name}_config.conf.erb"
34: cookbook "appsautm-template"
35: owner node['xyz-dir-library']['user']
36: mode "0755"
37:
38: variables({
39: :admin_servers=> admin_details,
40: :managed_servers=> managed_details
41: })
42: end
43:
Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/xyz-environment-cookbook/recipes/build_admin.rb:32:in `from_file'