Upload everything with Knife

8,319

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

From the chef repository.

If you execute knife upload . it uploads everything.

Solution 5

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

Share:
8,319
Steve Bennett
Author by

Steve Bennett

Updated on September 18, 2022

Comments

  • Steve Bennett
    Steve Bennett over 1 year

    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
    Steve Bennett over 11 years
    Um, well, obviously. :/
  • Reaces
    Reaces over 9 years
    This has less information than provided in the question (let alone the current accepted question) For a 3 year old question.
  • mmell
    mmell over 9 years
    @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.
  • Steve Bennett
    Steve Bennett over 9 years
    (I've accepted an answer now. I don't use Chef anymore, so I'm just trusting that CrackerJackMack's answer is valid.)