厨师模板和在循环中使用它

时间:2013-06-05 20:45:41

标签: ruby chef

如何在循环中编写它。 我对ruby非常陌生,因此很难接受这种方法。

我正在尝试创建响应文件,然后更新IHS(IBM HTTP Server)的修订包

    #Install the fix pack for IHS

template "/tmp/ihs-fixpack-response1.txt" do
  source "ihs-fixpack-response.erb"
  mode 0755
  owner "root"
  group "root"
  variables({
    :fixpack => "7.0.0-WS-IHS-LinuxX32-FP0000019.pak",
    :product_path => node[:websphere][:ihs][:ihs_path]
  })
end

# code for installing Fixpack
bash "ihs/was-updateinstaller" do
  user "root"
  code %(#{node[:websphere][:ihs][:ihs_updi_path]}/update.sh  -options "/tmp/ihs-fixpack-response1.txt" -silent)

end

#Install the fix pack for the plugin.
template "/tmp/ihs-fixpack-response2.txt" do
  source "ihs-fixpack-response.erb"
  mode 0755
  owner "root"
  group "root"
  variables({
    :fixpack => "7.0.0-WS-PLG-LinuxX32-FP0000019.pak",
    :product_path => node[:websphere][:ihs][:ihs_wasPluginPath]
  })
end

# code for installing Fixpack
bash "ihs/was-updateinstaller" do
  user "root"
  code %(#{node[:websphere][:ihs][:ihs_updi_path]}/update.sh  -options "/tmp/ihs-fixpack-response2.txt" -silent)
end

1 个答案:

答案 0 :(得分:3)

我相信这会做你想要的:

[ [ "7.0.0-WS-IHS-LinuxX32-FP0000019.pak", node[:websphere][:ihs][:ihs_path] ],
  [ "7.0.0-WS-PLG-LinuxX32-FP0000019.pak", node[:websphere][:ihs][:ihs_wasPluginPath] ]
].zip(1..2).each do |vars, i|
  template "/tmp/ihs-fixpack-response#{i}.txt" do
    source "ihs-fixpack-response.erb"
    mode 0755
    owner "root"
    group "root"
    variables({
      :fixpack => vars.first,
      :product_path => vars.last
    })
  end

  bash "ihs/was-updateinstaller" do
    user "root"
    code %(#{node[:websphere][:ihs][:ihs_updi_path]}/update.sh  -options "/tmp/ihs-fixpack-response#{i}.txt" -silent)
  end
end