Chef infra 厨师配方编译失败

Chef infra 厨师配方编译失败,chef-infra,cookbook,Chef Infra,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["i

我正在尝试创建一个厨师食谱,它依赖于
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

在我看来,这个配方找不到
节点[:etc][:passwd][tomcat\u user]
。tomcat配方运行时将安装tomcat用户。我还在这本食谱的metadata.rb中添加了依赖于“tomcat”的
。我的目的是在运行tomcat的用户的主位置安装一个文件。我该怎么做呢?

问题的根源在于,tomcat用户是在阅读OHAI为其设置的值后创建的

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

  • 您必须在创建用户后重新加载OHAI数据,以便访问数据。通常,OHAI数据(在
    节点[“etc”]
    中)在厨师长运行的第一个阶段中只更新一次
  • 您必须调整您的配方,以便帐户数据仅在更新后读取
  • 您可以重构代码,如下所示:

    ########################################################################
    # 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_块
    确保您仅在OHAI数据更新后的转换阶段读取数据。

    可以通过延迟属性求值来解决吗?您还可以在
    execute
    ruby\u块中使用惰性求值。您仍然需要重新加载ohai,以便能够访问用户信息。