【问题标题】:Chef recipe compilation failure厨师食谱编译失败
【发布时间】:2014-01-03 16:11:54
【问题描述】:

我正在尝试创建一个取决于 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 recipe 运行时,将安装一个 tomcat 用户。我还在这本食谱的 metadata.rb 中添加了depends 'tomcat'。我的意图是在运行 tomcat 的用户的主位置安装一个文件。我该怎么办?

【问题讨论】:

    标签: chef-infra cookbook


    【解决方案1】:

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

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

    1. 您必须在创建用户后重新加载 OHAI 数据,以便您可以访问数据。通常,OHAI 数据(在 node["etc"] 中)仅在 Chef 运行的第一阶段中更新一次。
    2. 您必须调整您的配方,以便仅在更新后才能读取帐户数据

    你可以像这样重构你的代码:

    ########################################################################
    # 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 数据更新后的转换阶段读取数据。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多