What's the difference between adding cookbooks to Berksfile using 'cookbook' and adding cookbooks to metadata.rb using 'depends'? For example, if I add to metadata.rb
depends 'nginx'
do I need to add it to Berksfile using
cookbook 'nginx'
?
A file named metadata. rb is located at the top of every cookbook directory structure. The contents of the metadata. rb file provides hints to the Chef server to help ensure that cookbooks are deployed to each node correctly.
Berksfile is the most crucial component of Berkshelf! It's just like metadata for Chef. However, the usage is somewhat different. Berksfile makes it simple to download a dependency cookbook from chef supermarket (or other places) and upload to the cookbook repository on the server.
Chef Server The workstation sends the cookbook to the server using a knife. The nodes communicate with the server using the Chef client. If there are any changes made to the infrastructure code, they must be passed through the Chef Server to apply them to the nodes.
Cookbooks are fundamental working units of Chef, which consists of all the details related to working units, having the capability to modify configuration and the state of any system configured as a node on Chef infrastructure. Cookbooks can perform multiple tasks.
The Berksfile is Berkshelf specific, while the metadata file is built into Chef.
Adding your dependencies to the metadata file allows other applications, like librarian-chef or the supermarket, to read your dependencies as well.
Note that Berkshelf reads the dependencies from metadata as well, as long as you add the metadata
line to the Berksfile.
I strongly recommend specifying all dependencies in your metadata file, and using your Berksfile to point to where specific cookbooks are stored if they're not available in the supermarket (like Github, or a local path).
Berksfile and metadata.rb have different purposes to solve and comes into picture at different stages of cookbook life-cycle.
Berksfile is for dependency management for cookbooks. Consider a case where my cookbook is using a community cookbook from chef supermarket. In this case, first I need to download that community cookbook from supermarket and upload it along with my own cookbook to chef server. Berksfile simplifies this workflow for you. With single command (berks install
), it downloads all dependent cookbooks (and their dependent cookbooks -- transitive dependencies) from their respective sources (may be from git repository or from supermarket). With another single command berks upload
it uploads all these cookbooks to chef server. You do not have to upload them individually with knife cookbook upload
. Role of Berksfile in particular cookbooks life-cycle finishes here.
metadata.rb is referred by chef-client while actually converging the node. It uses this file to download all the required cookbooks from chef server (assuming that these cookbooks are now available on chef server by using berkshelf
or knife
) to the node to successfully complete the chef-client run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With