18

问题:我有一个主厨语句,只有在属性为“真”时才应该运行。但它每次都运行。

预期行为:default[:QuickBase_Legacy_Stack][:dotNetFx4_Install] = "false"不应安装 dotnet4 。

实际行为:无论属性设置为什么,它都会安装 dotnet4。

我的代码:

属性文件:

default[:QuickBase_Legacy_Stack][:dotNetFx4_Install] = "false"

配方文件:

windows_package "dotnet4" do
    only_if node[:QuickBase_Legacy_Stack][:dotNetFx4_Install]=='true'
    source "#{node[:QuickBase_Legacy_Stack][:dotNetFx4_URL]}"
    installer_type :custom
    action :install
    options "/quiet /log C:\\chef\\installLog4.txt /norestart /skipmsuinstall"
end
4

2 回答 2

20

运行 Ruby 的守卫必须包含在一个块中,{}否则 Chef 将尝试在默认解释器(通常是 bash)中运行该字符串。

windows_package "dotnet4" do
    only_if        { node[:QuickBase_Legacy_Stack][:dotNetFx4_Install] == 'true' }
    source         node[:QuickBase_Legacy_Stack][:dotNetFx4_URL]
    installer_type :custom
    action         :install
    options        "/quiet /log C:\\chef\\installLog4.txt /norestart /skipmsuinstall"
end

检查您是否需要布尔值true而不是"true"

source此外,除非您需要使用字符串引用插入其他数据,否则请使用普通变量名称(for )。

于 2014-07-15T16:34:45.847 回答
8

这是一个 Ruby 条件,所以你需要为你的not_if:

only_if { node[:QuickBase_Legacy_Stack][:dotNetFx4_Install]=='true' }

(请注意添加的{})。您还可以使用do..end多行条件的语法:

only_if do
  node[:QuickBase_Legacy_Stack][:dotNetFx4_Install]=='true'
end

最后,请确保您的值是字符串"true"而不是值true(查看差异)。在 Ruby 中,true是一个布尔值(就像false),但是"true"是一个字符串(就像"foo") 检查true=="true"是否会返回false

于 2014-07-15T16:34:09.023 回答