【问题标题】:Chef recipe execution order厨师食谱执行顺序
【发布时间】:2014-08-22 23:10:49
【问题描述】:

以下是我用来在我的 Windows 服务器上复制和安装软件的秘诀。

batch "Get installed software list" do
  code "wmic /output:C:\\InstallList.txt product get name,version"
end

if File.read('C:/InstallList.txt', mode: 'r:BOM|UTF-16:UTF-8') =~ /.NET Framework 4/
  print "Dotnet framework 4 is already installed."
else
  remote_file 'c:/repo/dotNetFx40_Full_x86_x64.exe' do
    source 'file:////10.132.17.53/e$/CHEFREPO/dotNetFx40_Full_x86_x64.exe'
  end
  batch "Install Dotnet" do
    cwd 'c:/repo/'
    code <<-EOH
        dotNetFx40_Full_x86_x64.exe  #{node['mycloud_dotnet']['passive']} #{node['mycloud_dotnet']['CEIPconsent']} #{node['mycloud_dotnet']['chainingpackage']} #{node['mycloud_dotnet']['createlayout']} #{node['mycloud_dotnet']['lcid']} #{node['mycloud_dotnet']['log']} #{node['mycloud_dotnet']['msioptions']} #{node['mycloud_dotnet']['norestart']} #{node['mycloud_dotnet']['promptrestart']} #{node['mycloud_dotnet']['quit']} #{node['mycloud_dotnet']['repair']} #{node['mycloud_dotnet']['serialdownload']} #{node['mycloud_dotnet']['uninstall']} #{node['mycloud_dotnet']['parameterfolder']} #{node['mycloud_dotnet']['NoSetUpVersionCheck']} #{node['mycloud_dotnet']['uninstallpatch']} 
        del dotNetFx40_Full_x86_x64.exe

    EOH
    not_if {File.read('c:/InstallList.txt', mode: 'r:BOM|UTF-16:UTF-8') =~ /.NET Framework 4/}
  end
end

批处理资源创建了一个包含已安装软件列表的文本文件。然后我阅读此文件以检查该软件是否已安装。是的,我打印了一条消息。否则,配方将移至我的 else 部分,我从远程存储库下载设置,然后使用静默安装进行安装。 但是在客户端运行时,我收到如下错误:

[2014-08-22T05:26:38-07:00] FATAL: Errno::ENOENT: No such file or directory - C:
/InstallList.txt

在执行第一批 resource("Get installed Software List") 之前,我的客户端以某种方式执行 if 块中的 File.read 代码。任何可能出现问题的想法以及相同的解决方法。

【问题讨论】:

    标签: chef-infra chef-recipe silent-installer


    【解决方案1】:

    是的,您的if 代码在编译阶段执行,而您的batch 资源在收敛阶段执行,请参阅details here

    您可以通过在编译时运行batch 资源来实现您的愿望,例如:

    batch "Get installed software list" do
      code "wmic /output:C:\\InstallList.txt product get name,version"
      action :nothing
    end.run_action(:run)
    

    在您的情况下,将您的代码包含在 not_if 块中并摆脱 if/else 部分是另一种选择 即:

    remote_file 'c:/repo/dotNetFx40_Full_x86_x64.exe' do
      source 'file:////10.132.17.53/e$/CHEFREPO/dotNetFx40_Full_x86_x64.exe'
    end
    
    batch "Install Dotnet" do
      cwd 'c:/repo/'
      code <<-EOH
        dotNetFx40_Full_x86_x64.exe  #{node['mycloud_dotnet']['passive']} #                 {node['mycloud_dotnet']['CEIPconsent']} #{node['mycloud_dotnet']['chainingpackage']} #{node['mycloud_dotnet']['createlayout']} #{node['mycloud_dotnet']['lcid']} #{node['mycloud_dotnet']['log']} #{node['mycloud_dotnet']['msioptions']} #{node['mycloud_dotnet']['norestart']} #{node['mycloud_dotnet']['promptrestart']} #{node['mycloud_dotnet']['quit']} #{node['mycloud_dotnet']['repair']} #{node['mycloud_dotnet']['serialdownload']} #{node['mycloud_dotnet']['uninstall']} #{node['mycloud_dotnet']['parameterfolder']} #{node['mycloud_dotnet']['NoSetUpVersionCheck']} #{node['mycloud_dotnet']['uninstallpatch']} 
        del dotNetFx40_Full_x86_x64.exe
      EOH
      not_if { `wmic /output:C:\\InstallList.txt product get name,version` =~ /.NET Framework 4/ }
    end
    

    远程文件可以有一个保护,以避免每次下载:

    not_if { File::exists(...) }
    

    【讨论】:

    • 在编译时运行批处理资源的方法很有效。但我想 not_if 条件有错误。在上传食谱时,它给出了一个 sntax 错误:'Unexpected }'
    • 感谢 @sethvargo 完善答案并修复警戒线。
    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多