Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chef only_if attribute equals true

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
like image 996
tbenz9 Avatar asked Jul 15 '14 16:07

tbenz9


People also ask

What is attribute in chef?

An attribute is a specific detail about a node. Attributes are used by the chef-client to understand: The current state of the node. What the state of the node was at the end of the previous chef-client run.

What are chef resources?

Chef resource represents a piece of the operating system at its desired state. It is a statement of configuration policy that describes the desired state of a node to which one wants to take the current configuration to using resource providers.


2 Answers

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.

like image 85
Matt Avatar answered Oct 05 '22 10:10

Matt


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

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

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

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, true is a boolean (just like false), but "true" is a string (just like "foo") Checking if true == "true" will return false.

like image 45
sethvargo Avatar answered Oct 05 '22 08:10

sethvargo