厨师食谱编译失败

时间:2013-12-16 10:04:28

标签: chef cookbook

我正在尝试创建一个取决于tomcat食谱的厨师食谱,例如

tomcat_user = node[:tomcat][:user]
tomcat_user_home_folder = node[:etc][:passwd][tomcat_user][:dir]
execute "Install jasper license" do
    command "cp jasperserver.license #{tomcat_user_home_folder}/"
    cwd "#{node["install-jasper-license"]["license-location"]}"
end

当我在节点上运行sudo chef-client时,出现以下错误:

================================================================================
Recipe Compile Error in /var/chef/cache/cookbooks/install-jasper-license/recipes/default.rb
================================================================================

NoMethodError
-------------
undefined method `[]' for nil:NilClass

在我看来,这个食谱无法找到node[:etc][:passwd][tomcat_user]。 tomcat配方运行时将安装tomcat用户。我还在本手册的metadata.rb中添加了depends 'tomcat'。我的目的是在运行tomcat的用户的home位置安装一个文件。我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

您的问题的根源是您在读取由OHAI设置的值后创建的tomcat用户。

要解决此问题,您必须执行以下两个步骤:

  1. 您必须在创建用户后重新加载OHAI数据,以便您可以访问数据。通常情况下,OHAI数据(node["etc"])仅在Chef运行的第一阶段中更新一次。
  2. 您必须调整您的食谱,以便仅在更新后才能阅读帐户数据
  3. 你可以像这样重构你的代码:

    ########################################################################
    # 1. Reload OHAI data if required
    ohai "reload_passwd" do
      action :nothing
      plugin "passwd"
    end
    
    # Make the installation of the tomcat package notify the reload of the OHAI data
    # This works for all the Linux's but not SmartOS
    tomcat_package = resources(:package => "tomcat#{node["tomcat"]["base_version"]}")
    tomcat_package.notifies :reload, "ohai[reload_passwd]", :immediately
    
    ########################################################################
    # 2. Install license file
    
    ruby_block "Install jasper license" do
      block do
        tomcat_user = node["tomcat"]["user"]
        tomcat_user_home_folder = node["etc"]["passwd"][tomcat_user]["dir"]
    
        File.copy File.join(node["install-jasper-license"]["license-location"], "jasperserver.license"), File.join(tomcat_user_home_folder, "jasperserver.license")
      end
      not_if{ File.exist? File.join(tomcat_user_home_folder, "jasperserver.license") }
    end
    

    ruby_block确保您仅在OHAI数据更新后的转换阶段读取数据。

相关问题