Skip to content

Instantly share code, notes, and snippets.

Last active September 30, 2019 09:04
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save kesor/4770945 to your computer and use it in GitHub Desktop.
Save kesor/4770945 to your computer and use it in GitHub Desktop.
Vagrantfile to create a chef-server (11.x) and a client box that uses it.
source :rubygems
gem 'json', '= 1.5.4' # knife/chef 11.0 and 11.20 is broken with json 1.5.5/1.7.7
gem 'vagrant'
gem 'vagrant-hostmaster'
# -*- mode: ruby -*-
# vi: set ft=ruby :
# default box when no VAGRANT_BOX / VAGRANT_BOX_URL environment is set
BOX_NAME = "quantal"
BOX_URL = ""
test -d /opt/chef || {
echo "Installing chef-client via omnibus"
curl -L -s | bash
test -d /opt/chef-server || {
echo "Installing chef-server via omnibus"
curl -L -s '' > chef-server.dpkg
dpkg -i chef-server.dpkg
/opt/chef-server/bin/chef-server-ctl reconfigure >/dev/null
[ -f "/vagrant/.chef/chef-validator.pem" ] && {
# compare chef-validator.pem file, don't continue when its already the same
server_md5=`md5sum /etc/chef-server/chef-validator.pem | cut -f1 -d' '`
client_md5=`md5sum /vagrant/.chef/chef-validator.pem | cut -f1 -d' '`
[ "$server_md5" = "$client_md5" ] && exit 0
echo "Creating workstation knife configuration"
mkdir -p /vagrant/.chef
cp /etc/chef-server/chef-validator.pem /vagrant/.chef/
/opt/chef-server/bin/knife configure \
--initial --yes --verbose --repository /vagrant \
--server-url https://chefserver.vagrant.local \
--validation-client-name chef-validator --validation-key /etc/chef-server/chef-validator.pem \
--admin-client-name chef-webui --admin-client-key /etc/chef-server/chef-webui.pem \
--user "workstation" --key /vagrant/.chef/workstation.pem
cat <<EOK > /vagrant/.chef/knife.rb
cwd = File.dirname(__FILE__)
log_level :info # valid values - :debug :info :warn :error :fatal
log_location STDOUT
node_name ENV.fetch('KNIFE_NODE_NAME', 'workstation')
client_key ENV.fetch('KNIFE_CLIENT_KEY', File.join(cwd,'workstation.pem'))
chef_server_url ENV.fetch('KNIFE_CHEF_SERVER_URL', 'https://chefserver.vagrant.local')
validation_client_name ENV.fetch('KNIFE_CHEF_VALIDATION_CLIENT_NAME', 'chef-validator')
validation_key ENV.fetch('KNIFE_CHEF_VALIDATION_KEY', File.join(cwd,'chef-validator.pem'))
syntax_check_cache_path File.join(cwd,'syntax_check_cache')
cookbook_path File.join(cwd,'..','cookbooks')
data_bag_path File.join(cwd,'..','data_bags')
role_path File.join(cwd,'..','roles')
EOF do |config| = ENV.fetch("VAGRANT_BOX", BOX_NAME)
config.vm.box_url = ENV.fetch("VAGRANT_BOX_URL", BOX_URL)
config.vm.define :chef_server do |v|
v.vm.customize ["modifyvm", :id, "--memory", 1024] :hostonly, ""
v.vm.host_name = "chefserver.vagrant.local"
v.vm.provision :shell, :inline => CHEF_CLIENT_INSTALL
v.vm.provision :shell, :inline => CHEF_SERVER_INSTALL
v.vm.provision :shell, :inline => CHEF_CREATE_WORKSTATION
config.vm.define :client do |v| :hostonly, ""
v.vm.host_name = "client.vagrant.local"
v.vm.provision :shell, :inline => CHEF_CLIENT_INSTALL
v.vm.provision :chef_client do |chef|
chef.chef_server_url = 'https://chefserver.vagrant.local'
chef.validation_key_path = ".chef/chef-validator.pem"
chef.validation_client_name = "chef-validator"
chef.run_list = [
# ... put something in here, or knife node edit client.vagrant.local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment