Contributors10 minutesHere you'll upload a cookbook to the Chef server.
Most Chef users maintain their cookbooks in a version control system such as Git. However, also maintaining a copy of your cookbooks on the Chef server provides these benefits:
- A central location for your cookbooks that's accessible from every node in your network.
- A versioning mechanism that enables you to associate different cookbook versions among your nodes. This enables you to roll out new configuration policies only when you're ready.
The Chef server does not replace version control. Version control is where you and your team maintain your cookbooks as you develop them. Chef server is where you publish your cookbooks when you're ready to run them on your nodes.
1. Get the learn_chef_apache2 cookbook from GitHub
In Learn the Chef basics, you wrote a cookbook named learn_chef_apache2 that configures Apache web server. You'll run that cookbook on your new node.
Normally, you write cookbooks on your workstation. In Learn the Chef basics, you wrote the learn_chef_apache2 cookbook directly on your test server. Instead of having you type in the cookbook again, let's get a copy from GitHub.
First, you need a place to store your cookbooks locally on your workstation. Your knife configuration file looks similar to this.
Editor: ~/learn-chef/.chef/knife.rb
1
2
3
4
5
6
7
8
9
| # See http://docs.chef.io/config_rb_knife.html for more information on knife configuration options
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
node_name "chef-user-1"
client_key "#{current_dir}/chef-user-1.pem"
chef_server_url "https://api.chef.io/organizations/learn-chef-2"
cookbook_path ["#{current_dir}/../cookbooks"] |
cookbook_path tells knife where to look for cookbooks, and is defined as "#{current_dir}/../cookbooks". The path breaks down like this:
| Partial path | Meaning | Full path |
|---|
#{current_dir} | The location of knife.rb | ~/learn-chef/.chef |
.. | Traverse up one directory | ~/learn-chef |
cookbooks | Traverse to the cookbooks directory | ~/learn-chef/cookbooks |
As you can see, knife is configured to look in the ~/learn-chef/cookbooks directory for cookbooks.
| Your knife.rb file, like many files in Chef, is a Ruby file. The #{current_dir} syntax, for example, is how you insert variables into strings. The Chef documentation provides a quick overview of the common Ruby patterns you'll use when you write Chef code. |
Start by creating the ~/learn-chef/cookbooks directory.
Terminal: ~/learn-chef
$ | mkdir ~/learn-chef/cookbooks
|
Now cd there.
Terminal: ~/learn-chef
$ | cd ~/learn-chef/cookbooks
|
Next, clone the learn_chef_apache2 cookbook from GitHub.
Terminal: ~/learn-chef/cookbooks
$ | git clone https://github.com/learn-chef/learn_chef_apache2.gitCloning into 'learn_chef_apache2'...
|
The contents of ~/learn-chef/cookbooks/learn_chef_apache2 is the same as what you built in the previous module. You can examine this directory to familiarize yourself with its contents.
| Chef Supermarket is also a place for the community to share cookbooks. You'll learn more about community cookbooks in future modules. |
2. Upload your cookbook to the Chef server
In practice, you would typically run your cookbook on a temporary instance, such as a virtual machine, before you upload it to the Chef server. You might also run a series of quality tests to ensure your cookbook does what you expect. You'll learn about this process in a future module.
For now, upload the learn_chef_apache2 cookbook to your Chef server. Run this command from anywhere under your ~/learn-chef directory.
Terminal: ~/learn-chef
$ | knife cookbook upload learn_chef_apache2Uploading learn_chef_apache2 [0.1.0]Uploaded 1 cookbook.
|
The output shows that the cookbook was successfully uploaded, but you can run the knife cookbook list command to verify.
Terminal: ~/learn-chef
$ | knife cookbook listlearn_chef_apache2 0.1.0
|