迭代 Chef 中的数组属性
Iterating over an array attribute in Chef
我正在使用 Chef 配置在 AWS 中创建机器。创建机器时,我向其添加了一个属性,该属性是一个 JSON 文件数组。在我 运行 的食谱中,我想遍历这个数组并在机器上创建一个模板文件。机器已配置,但在遍历数组属性时,我收到一条错误消息:
nil:NilClass
的未定义方法“each”
我试着查看在我的服务器上创建的节点文件,你猜怎么着? JSON 文件数组已添加到节点文件中!我不确定为什么它会不断抛出该错误。有什么想法吗?
代码示例如下:
machines.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
admin_role.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'
错误似乎出在您的模板中,而不是您的食谱中。事实上,它似乎与您发布的文件完全无关。我需要查看 #{env_name}_config.conf.erb
文件和 build_admin.rb
文件才能获得更多帮助。
我正在使用 Chef 配置在 AWS 中创建机器。创建机器时,我向其添加了一个属性,该属性是一个 JSON 文件数组。在我 运行 的食谱中,我想遍历这个数组并在机器上创建一个模板文件。机器已配置,但在遍历数组属性时,我收到一条错误消息:
nil:NilClass
的未定义方法“each”我试着查看在我的服务器上创建的节点文件,你猜怎么着? JSON 文件数组已添加到节点文件中!我不确定为什么它会不断抛出该错误。有什么想法吗?
代码示例如下:
machines.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
admin_role.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'
错误似乎出在您的模板中,而不是您的食谱中。事实上,它似乎与您发布的文件完全无关。我需要查看 #{env_name}_config.conf.erb
文件和 build_admin.rb
文件才能获得更多帮助。