chef only_if attribute equals true

tbenz9 picture tbenz9 · Jul 15, 2014 · Viewed 31.2k times · Source

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

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

Actual Behavior: No matter what the attribute is set to, it installs 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

Answer

Matt picture Matt · Jul 15, 2014

Guards that run Ruby must be enclosed in a block {} otherwise Chef will try to run the string in the default interpreter (usually 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 true instead of "true"

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