Upload everything with Knife

Let's say you have a standard Chef repository with directories as follows:

cookbooks
data_bags
environments
roles

Is there way to upload it all in one go? Otherwise you have to do this:

knife cookbook upload -a
knife data bag from file data_bags/*.json
knife environment from file environments/*.rb
knife role from file roles/*.json

Perhaps there are third-party tools to do this kind of thing?


Make sure you are using ruby 1.9, then install knife-essentials

$ gem install knife-essentials

Then add the following to ~/.chef/knife.rb

repo_mode 'everything'
versioned_cookbooks true
chef_repo_path 'PATH/TO/chef-repo'

Then you can download/upload your entire server, or just parts of it. Downloading will put a lot of .json files in your chef-repo, good for backups I found.

$ knife download /
$ knife upload /
$ knife upload /clients

Also note that if you have roles stored as ruby files (e.g: roles/*.rb), you will need to convert them to json. Then you can re-download them from chef server as json. For example:

  1. knife role from file roles/*.rb
  2. knife download roles/

This method is also used to upgrade from chef server 10 to 11.


You could write a simple bash script to do that like:

#!/bin/sh

for file in `ls cookbooks`;do
 [ -d $file ] && knife cookbook upload cookbooks/$file
done
for file in `ls data_bags | grep \.json$`;do
 knife data bag from file data_bags/$file
done
for file in `ls environments | grep \.rb$`;do
 knife environment from file environments/$file
done
for file in `ls roles | grep \.json$`;do
 knife role from file roles/$file
done

then call it using ./script.sh, this will do everything automatically.