How to define a function/action/… in chef that returns a value which can be used in e.g. not_if

StackOverflow https://stackoverflow.com/questions/7497691

  •  24-01-2021
  •  | 
  •  

Question

I'm learning chef at the moment and I'm trying to write everything in a way that repeated provisioning doesn't break anything.

I have a server that is deployed on the machine and then there is some code loaded into it. The next time of provisioning I like to test first if the code has been loaded already. And I want to do it in a generic way because I use it in different recipes. My idea would be to define a function/defintion/etc.. I can call the function which tests the condition and returns a value. My hopes would be that I can use this function/... in a not_if clause for other actions.

Is there a way to do this in chef with a defintion/action/provider/... or would I need to add some rubyish stuff somewhere?

Was it helpful?

Solution

Resources in Chef all have conditional execution.

The not_if and only_if statements can take a shell command as a string or a ruby block to determine if they should perform their action or not.

user "myuser" do
  not_if "grep myuser /etc/password"
  action :create
end

You might have a node attribute and use that as your conditional or call a ruby method that returns true or false.

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

https://web.archive.org/web/20111120120013/http://wiki.opscode.com/display/chef/Resources#Resources-ConditionalExecution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top