0

I have setup a Chef server in our network that I use to manage several nodes. These nodes have a chef-client installed executing as a daemon every X minutes.

The problem is that every time the client runs, it executes the recipes for all the cookbooks, even those previously executed, so it consumes resources and sometimes it even breaks thinks (with services restarts, for example).

I know that I can avoid executing a piece of code or a recipe I create as detailed here: Prevent chef recipe from executing previously executed action? but, would this mean that I should modify any cookbook that I download from the opscode repository.

In other words, is it possible to make the chef server (or the clients) to mark the cookbooks as 'executed' as soon as they are executed the first time?

1 Answers1

2

Well designed cookbooks should be entirely idempotent - that is that they can be run multiple times with subsequent runs doing nothing (provided no configuration has changed). If you are in a situation where a cookbook is restarting a service even though none of the configuration has changed then this would suggest that there is a bug in the cookbook.

If on the other hand the configuration has changed (e.g. you have changed an attribute that the cookbook reads) then the recipe may very well (legitimately) restart the service so that the configuration change takes effect.


In the specific case of services, the way that restarts are usually triggered is via the notification mechanism. Within a recipe you will have a service resource that looks something like:

service "myservice" do
  action :nothing
end

If the recipe then generates a configuration file for the application you will have a template resource that sends a notification to the service resource:

template "/etc/myapp.conf" do
  notifies :restart, "service[myservice]"
end

The template resource has logic built-into it such that if the existing myapp.conf file is the same as the file Chef has generated then Chef will not trigger the notification to restart the service.


You will need to identify whether these restarts are happening due to you changing the configuration, or whether there is a bug in the recipe that causes it to always restart the service.

  • Thanks a lot for your explanation, although, it was not exactly what I was looking for. I understandt that the cookbooks should be idempotent, but some of the ones provided by opscode repository are not (see java cookbook), so my question still stands: is there any way to 'externally' mark cookbooks as run (and maybe change the version to force rerun) or I should change the cookbooks from opscode? Thanks! – Gonzalo Alvarez Jul 02 '13 at 07:47
  • No, that is not possible, because that would kill all the idea of Chef. Every time Chef is run, it makes sure the machine is configured exactly as it should be. If you could mark recipe as executed and Chef would not then check the resources from that cookbook, you will end up with misconfigured environment as soon as something changes the configuration on that machine. – Draco Ater Jul 16 '13 at 11:19
  • @GonzaloAlvarez - the java cookbook should be idempotent (and is, in my experience). If that's not the case, it should be reported as a bug. – zts Jul 26 '13 at 13:20