Shortly after writing my first Chef cookbook and using it in a production environment we needed to update the cookbook with changes. Thankful Chef has you covered here. You can constrain cookbook versions in each environment to ensure that a new cookbook version is not automatically/immediately applied to a production server. Instead we can slowly roll the update through our environments, testing along the way.
In the rest of this post we will walk through the steps to updating a cookbook.
Use the knife environment show command to verify the constraints in each environment and the cookbook you are updating is constrained to less than or equal to the current version on the server.
> knife environment show prod
chef-client: <= 7.2.0
docker: <= 2.13.11
inf_redis_docker_cookbook: <= 1.0.0
If necessary use the knife environment edit command to add/edit the constraint in each environment.
We have four environments: DEV, QA, MOCK and PROD. We generally constrain all environments past DEV. This means any updates would automatically be applied to all nodes in the DEV environment. You may want to constrain DEV depending on your use cases.
Upload New Cookbook Version
Make the necessary changes to your cookbook and don’t forget to update the version number in the metadata.rb file. Use knife to upload the new cookbook version.
Roll Through Environments
Next verify the cookbook update applies to the first environment correctly. If so, update the constraint in the next environment and verify. Rinse and repeat through all your environments.