chef only_if属性等于true

时间:2014-07-15 16:22:39

标签: ruby chef lwrp

问题:我有一个厨师声明,只有在属性为" true"时才会运行。但它每次都会运行。

预期行为:不应安装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

2 个答案:

答案 0 :(得分:20)

运行Ruby的

Guards必须包含在块{}中,否则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),除非您需要使用字符串引号插入其他数据。

答案 1 :(得分: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 == {{ 1}}将返回"true"

相关问题