how to use a 'not_if' condition on a template resource

3.9k Views Asked by At

I'm trying to use a 'not_if' condition in a template resource as showcased by docs.chef.io

template "/tmp/somefile" do
  mode '0644'
  source "somefile.erb"
  not_if { node[:some_value] }
end

I'm new to chef and I'm unsure how to proceed to create something that effectively does the following:

if 'instance' (in node['project']['instance']) == nil 
do NOT do anything for this template

I would like to use the not_if condition but I don't know how to check for 'instance' == nil.

I would appreciate any help. Thank you,

1

There are 1 best solutions below

4
On

how about

not_if { node['project']['instance'].nil? }

This will check that the value of node['project']['instance'] is not nil. It will also fail if node['project'] happens to be nil, so if you aren't sure, you'll need:

not_if { node['project'].nil? || node['project']['instance'].nil? }