How to avoid running a resource from a community cookbook?

Let’s say I want to use the “windows” community cookbook exactly as-is except for one small change: that cookbook’s default recipe runs the chef_gem “windows-pr” resource, and I don’t want to run that. But I want to run all the other resources in this recipe. What’s the best way to do avoid running just one resource from a community cookbook?


I recommend using chef-rewind https://github.com/thommay/chef-rewind

Using chef-rewind you can “unwind” resources which takes them out of the resource queue.

In your example include the chef-rewind gem and do the following

unwind “chef_gem[windows-pr]”

The default behavior of chef_gem is that it will runs when the recipe is read into memory and is interpreted by Ruby (compile phase), the Chef docs for chef_gem state

“Runs its actions immediately, before convergence, allowing a gem to be used in a recipe immediately after it is installed”

Although there is a compile_time attribute

"Controls the phase during which a gem is installed on a node. Set to true to install a gem while the resource collection is being built (the “compile phase”). Set to false to install a gem while the chef-client is configuring the node (the “converge phase”). Possible values: nil (for verbose warnings), true (to warn once per chef-client run), or false (to remove all warnings). Recommended value: false.

To suppress warnings for cookbooks authored prior to chef-client 12.1, use a respond_to? check to ensure backward compatibility."

Fortunately the latest version of the Windows cookbook has compile_time attribute set to false.

If this wasn’t the case some community cookbooks place behaviors in attributes to allow the control to be modified by wrapper cookbooks, you could always open an issue in the Windows cookbook github and ask them to create attributes to control the gems to install but in this case I think chef-rewinds unwind method will work for you.

There are a few other things you can do to stop resources from being run but I wouldn’t really recommend them.