Chef cookbooks marked as executed

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?

Answer

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.

Attribution
Source : Link , Question Author : Gonzalo Alvarez , Answer Author : Jared Russell

Leave a Comment