ruby 厨师 only_if 属性等于 true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24763345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:32:27  来源:igfitidea点击:

chef only_if attribute equals true

rubycheflwrp

提问by tbenz9

Problem:I have a chef statement that should only run if the attribute is "true". But it runs every time.

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

Expected Behavior:When default[:QuickBase_Legacy_Stack][:dotNetFx4_Install] = "false"dotnet4 should not be installed.

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

Actual Behavior:No matter what the attribute is set to, it installs dotnet4.

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

My code:

我的代码:

attribute file:

属性文件:

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

recipe file:

配方文件:

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

回答by Matt

Guardsthat run Ruby must be enclosed in a block {}otherwise Chef will try to run the string in the default interpreter (usually bash).

运行 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

Check if you need boolean trueinstead of "true"

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

Also, use the plain variable name (for source) unless you need to interpolate other data with the string quoting.

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

回答by sethvargo

That is a Ruby conditional, so you need to use a block for your not_if:

这是一个 Ruby 条件,因此您需要为您的 使用一个块not_if

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

(Please take note of the added {}). You can also use the do..endsyntax for multiline conditions:

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

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

Finally, please make sure your value is the String "true"and not the value true(see the difference). In Ruby, trueis a boolean (just like false), but "true"is a string (just like "foo") Checking if true== "true"will return false.

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