5

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?

Steve Bennett
  • 5,539
  • 12
  • 45
  • 57

5 Answers5

5

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.

Logic Wreck
  • 1,428
  • 9
  • 8
4

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.

TrinitronX
  • 1,087
  • 9
  • 13
2

I wrote a small knife plugin called sync-all described below that accomplishes this as well:
https://github.com/cdoughty77/knife-sync-all

cdoughty
  • 121
  • 1
1

Currently the docs say you should simply knife upload /. There are other possibilities in the docs.

mmell
  • 121
  • 4
  • This has less information than provided in the question (let alone the current accepted question) For a 3 year old question. – Reaces Jan 23 '15 at 19:07
  • @Reaces There is no accepted answer. I pointed to the docs, which no one else did, and gave the correct answer. The correct answer has changed since the question was posed. The existing answers here are unnecessarily complicated. – mmell Jan 23 '15 at 20:41
  • (I've accepted an answer now. I don't use Chef anymore, so I'm just trusting that CrackerJackMack's answer is valid.) – Steve Bennett Jan 27 '15 at 04:27
1

From the chef repository.

If you execute knife upload . it uploads everything.

Starx
  • 259
  • 1
  • 3
  • 12