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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With