1

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

4

1 回答 1

3

您的问题的根源是在您读取将由 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 数据更新后的转换阶段读取数据。

于 2013-12-16T12:49:10.907 回答