
Chef Cookbook
By :

If running your own Chef server seems like overkill and you're not comfortable with using the hosted Chef, you can use local mode to execute cookbooks.
my_cookbook
by running the following command:mma@laptop:~/chef-repo $ chef generate cookbook cookbooks/my_cookbook Compiling Cookbooks... Recipe: code_generator::cookbook ...TRUNCATED OUTPUT...
my_cookbook
so that it creates a temporary file:mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb file "/tmp/local_mode.txt" do content "created by chef client local mode" action :create end
Let's run my_cookbook
on your local workstation using the Chef client's local mode:
my_cookbook
in the run list:mma@laptop:~/chef-repo $ chef-client --local-mode -o my_cookbook [2016-10-03T20:37:02+02:00] INFO: Started chef-zero at chefzero://localhost:8889 with repository at /Users/matthias.marschall/chef-repo One version per cookbook [2016-10-03T20:37:02+02:00] INFO: Forking chef instance to converge... Starting Chef Client, version 12.14.89 [2016-10-03T20:37:02+02:00] INFO: *** Chef 12.14.89 *** [2016-10-03T20:37:02+02:00] INFO: Platform: x86_64-darwin13 ...TRUNCATED OUTPUT... Chef Client finished, 1/1 resources updated in 04 seconds
mma@laptop:~/chef-repo $ cat /tmp/local_mode.txt created by chef client local mode
The --local-mode
(short form: -z
) parameter switches the Chef client into local mode. Local mode uses chef-zero
—a simple, in-memory version of the Chef server provided by Chef DK—when converging the local workstation.
By providing the -o
parameter, you override the run list of your local node so that the Chef client executes the default recipe from my_cookbook
.
Chef-zero saves all modifications made by your recipes to the local filesystem. It creates a JSON
file containing all node attributes for your local workstation in the nodes
directory. This way, the next time you run the Chef client in local mode, it will be aware of any changes your recipes made to the node.
You can use knife in local mode, too. To set the run list of a node named laptop (instead of having to override it with -o
), you can run the following command:
mma@laptop:~/chef-repo $ knife node run_list add -z laptop 'recipe[my_cookbook]'
When you're done editing and testing your cookbooks on your local workstation with chef-zero, you can seamlessly upload them to hosted Chef or your own Chef server:
Make sure you bump the version number of modified cookbooks in their metadata.rb
file and commit them to your version control system before uploading to the Chef Server.
mma@laptop:~/chef-repo $ berks upload Uploaded ...
Change the font size
Change margin width
Change background colour