迭代Chef

时间:2015-05-11 20:55:15

标签: ruby chef chef-recipe lwrp

我正在使用Chef Provisioning在AWS中创建计算机。在创建机器时,我向它添加一个属性,它是一个JSON文件数组。在我运行的配方中,我想迭代这个数组并在机器上创建一个模板文件。机器已配置,但在迭代数组属性时,我收到一条错误消息:

未定义的方法`each'代表nil:NilClass

我试着查看在我的服务器上创建的节点文件并猜测是什么? 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'

1 个答案:

答案 0 :(得分:0)

错误似乎出现在您的模板中,而不是您的食谱中。实际上,它似乎与您发布的文件完全无关。我需要查看#{env_name}_config.conf.erb文件和build_admin.rb文件才能获得更多帮助。