Skip to content

2015

How to use Berkshelf, Chef Zero, Vagrant and VirtualBox

In this post we show how you can use Berkshelf, Chef Zero, Vagrant, and VirtualBox to provision Ubuntu 14.04 guest on Windows 10 host.

Before You Begin

You need VirtualBox and Vagrant installed. To do that, you can follow the steps described in Ubuntu with Vagrant and VirtualBox on Windows.

Install Vagrant-Omnibus Plugin

vagrant plugin install vagrant-omnibus

Install Vagrant-Berkshelf Plugin

vagrant plugin install vagrant-berkshelf

Install Chef Development Kit (ChefDK)

In PowerShell as Administrator:

choco install chefdk

Install Knife Solo

At this time we only need knife solo to generate an empty Chef repository that is compatible with Chef Zero. For more information see Knife Solo.

chef gem install knife-solo

Create Chef Repository

Create a chef-repo directory

mkdir ubuntu-chef-repo
cd ubuntu-chef-repo

Setup the the chef-repo directory

touch Berksfile
chef exec knife solo init . --no-git

Paste this into the Berksfile:

source 'https://supermarket.chef.io'

cookbook 'main', path: './site-cookbooks/main'

Create New Cookbook

cd site-cookbooks
chef exec berks cookbook main --skip-git --skip-test-kitchen --no-foodcritic --no-chef-minitest --no-bundler --skip-vagrant

Check the structure of the main cookbook:

tree /F main

It should have the following structure:

│   Berksfile
│   CHANGELOG.md
│   chefignore
│   LICENSE
│   metadata.rb
│   README.md
│   Thorfile
├───attributes
├───files
│   └───default
├───libraries
├───providers
├───recipes
│       default.rb
├───resources
└───templates
    └───default

Add the Apt Cookbook as a Dependency

The apt cookbook runs the apt-get update command on the guest system. See apt Cookbook - Chef Supermarket for details.

Open the site-cookbooks/main/metadata.rb file and add the following code at the end (the version may be different than 2.9.2):

depends 'apt', '~> 2.9.2'

This is how the final file should look like:

name             'main'
maintainer       'YOUR_NAME'
maintainer_email 'YOUR_EMAIL'
license          'All rights reserved'
description      'Installs/Configures main'
long_description 'Installs/Configures main'

version          '0.1.0'

depends 'apt', '~> 2.9.2'

Include the Apt Cookbook in the Default Recipe

Open site-cookbooks/main/recipes/default.rb and add the following code at the end:

include_recipe 'apt::default'

The final file looks like this:

#
# Cookbook Name:: main
# Recipe:: default
#
# Copyright (C) 2016 YOUR_NAME
#
# All rights reserved - Do Not Redistribute
#

include_recipe 'apt::default'

Test

This will generate Berksfile.lock

berks install

Create Vagrantfile

Go to the Chef Repo root (ubuntu-chef-repo).

touch Vagrantfile

Open Vagrantfile and replace its content with the following one:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  #
  config.vm.box = "ubuntu/trusty64"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  #
  config.vm.provider "virtualbox" do |vb|
    # For a complete reference, please see the online documentation at
    # https://docs.vagrantup.com/v2/virtualbox/configuration.html

    # Name used in Oracle VM VirtualBox Manager GUI
    vb.name = "berkshelf-ubuntu-kitchen"

    # Customize the amount of memory on the VM (in MB):
    vb.memory = "2048"

    # Customize the amount of video memory on the VM (in MB):
    vb.customize ["modifyvm", :id, "--vram", "128"]
  end

  # Install the latest version of Chef.
  # For more information see https://github.com/chef/vagrant-omnibus
  #
  config.omnibus.chef_version = :latest

  # Enabling the Berkshelf plugin.
  config.berkshelf.enabled = true

  # Provision with Chef Zero
  #
  config.vm.provision :chef_zero do |chef|
    # Specify the local paths where Chef data is stored
    chef.cookbooks_path = [ 'cookbooks', 'site-cookbooks' ]
    chef.data_bags_path = "data_bags"
    chef.nodes_path = "nodes"
    chef.roles_path = "roles"

    # Add a recipe
    chef.add_recipe "main::default"
  end
end

Test

vagrant up
vagrant provision

Provision Chef with Vagrant-Omnibus Plugin

In this post we show how you can provision Chef from Vagrant using the vagrant-omnibus plugin. This post is for Ubuntu 14.04 guest running in VirtualBox on Windows 10 host.

Prerequisites

You need VirtualBox and Vagrant installed. To do that, you can follow the steps described in Ubuntu with Vagrant and VirtualBox on Windows.

Install Chef Development Kit (ChefDK)

In PowerShell as Administrator:

choco install chefdk

Provision Chef

Before you can use Chef Solo, Chef Zero, or Chef Client for provisioning, you need to install the latest Chef client on the guest. This can be done with a shell script or with a Vagrant plugin called vagrant-omnibus.

In PowerShell:

vagrant plugin install vagrant-omnibus

Open Vagrantfile and add the following code at the end of the configuration:

# Install the latest version of Chef.
# For more information see https://github.com/chef/vagrant-omnibus
#
config.omnibus.chef_version = :latest

The complete Vagrantfile file should look like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
#
Vagrant.configure(2) do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  #
  config.vm.box = "ubuntu/trusty64"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  #
  config.vm.provider "virtualbox" do |vb|
    # For a complete reference, please see the online documentation at
    # https://docs.vagrantup.com/v2/virtualbox/configuration.html

    # Name used in Oracle VM VirtualBox Manager GUI
    vb.name = "Ubuntu-x64-Vagrant"

    # Customize the amount of memory on the VM (in MB):
    vb.memory = "2048"

    # Customize the amount of video memory on the VM (in MB):
    vb.customize ["modifyvm", :id, "--vram", "128"]
  end

  # Install the latest version of Chef.
  # For more information see https://github.com/chef/vagrant-omnibus
  #
  config.omnibus.chef_version = :latest
end

Test

Finally launch and provision the Vagrant box. It should install the latest Chef Client:

vagrant reload
vagrant provision

Ubuntu and Chef with Vagrant and VirtualBox on Windows

In this post we show how you can use VirtualBox and Vagrant to launch an Ubuntu 14.04 guest and install the latest Chef client on it using Windows 10 as a host.

Prerequisites

You need VirtualBox and Vagrant installed. To do that, you can follow the steps described in Ubuntu with Vagrant and VirtualBox on Windows.

Chef

Install Chef Development Kit (ChefDK)

In PowerShell as Administrator:

choco install chefdk

Install Chef on the guest

Before you can use Chef Solo, Chef Zero, or Chef Client for provisioning, you need to install the latest Chef client on the guest.

Open Vagrantfile and add the following code at the end of the configuration:

# Provision Chef Client with a shell script that runs the Chef Omnibus Installer
# For more information see https://docs.chef.io/install_omnibus.html
# 
config.vm.provision "shell", inline: <<-SHELL
  sudo apt-get update -y
  sudo apt-get install curl -y
  curl -L https://www.opscode.com/chef/install.sh | sudo bash
SHELL

The complete Vagrantfile file should look like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
#
Vagrant.configure(2) do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  #
  config.vm.box = "ubuntu/trusty64"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  #
  config.vm.provider "virtualbox" do |vb|
    # For a complete reference, please see the online documentation at
    # https://docs.vagrantup.com/v2/virtualbox/configuration.html

    # Name used in Oracle VM VirtualBox Manager GUI
    vb.name = "Ubuntu-x64-Vagrant"

    # Customize the amount of memory on the VM (in MB):
    vb.memory = "2048"

    # Customize the amount of video memory on the VM (in MB):
    vb.customize ["modifyvm", :id, "--vram", "128"]
  end

  # Provision Chef Client with a shell script that runs the Chef Omnibus Installer
  # For more information see https://docs.chef.io/install_omnibus.html
  # 
  config.vm.provision "shell", inline: <<-SHELL
    sudo apt-get update -y
    sudo apt-get install curl -y
    curl -L https://www.opscode.com/chef/install.sh | sudo bash
  SHELL
end

Test

Reload and provision the guest virtual machine. This should install the latest Chef Client:

vagrant reload
vagrant provision

How to Install Jekyll on Windows

This post shows how to install Ruby and Jekyll via Chocolatey on Windows 8.1 or Windows Server 2012 R2 Update 1.

Chocolatey

If you do not have Chocolatey, you can install it by following the instructions on chocolatey.org. You may also see this post for instructions on how to set the Chocolatey cache location.

Ruby

Install

Open PowerShell as Administrator and run the following command:

choco install ruby

This will install Ruby 2.1.6 in C:\tools\ruby21\bin.

Configure

Close PowerShell and open it again, so it can pick the changes to the PATH environment.

Test

ruby --version
ruby 2.1.6p336 (2015-04-13 revision 50298) [x64-mingw32]

Ruby DevKit (2.0+)

Install

Open PowerShell as Administrator and run the following command:

choco install ruby2.devkit

This will install Ruby DevKit 2.0+ and its dependencies- in C:\tools\DevKit2.

Configure

Open C:\tools\DevKit2\config.ymland add C:\tools\ruby21 path at the end. The complete config.yml should look like this:

# This configuration file contains the absolute path locations of all
# installed Rubies to be enhanced to work with the DevKit. This config
# file is generated by the 'ruby dk.rb init' step and may be modified
# before running the 'ruby dk.rb install' step. To include any installed
# Rubies that were not automagically discovered, simply add a line below
# the triple hyphens with the absolute path to the Ruby root directory.
#
# Example:
#
# ---
# - C:/ruby19trunk
# - C:/ruby192dev
#
---
 - C:/tools/ruby21

Run the DevKit installer:

cd C:/tools/DevKit2
ruby dk.rb install

Jekyll

Install Bundler

gem install bundler

Create Jekyll Site

Create a jekyll directory under your home:

cd ~
mkdir jekyll

Create a New Bundle

cd ~/jekyll
bundle init
Jekyll Gem

For Jekyll we need the jekyll gem.

Syntax Highlighter Gem

We will use rouge as a syntax highlighter. This comes in the rouge gem.

Auto-generation Gem

Jekyll has a built-in auto-regeneration feature that watches your source folder for changes and then re-builds your site. On Windows, you need to install the wdm gem to enable this functionality.

Gemfile

Open the Gemfile file and add jekyll, rouge, and wdm packages. Here is the final file:

# A sample Gemfile
source "https://rubygems.org"

gem "jekyll"
gem "rouge"
gem "wdm"
Install Gems

Install gems locally:

bundle install --binstubs --path vendor

Create Site

cd ~/jekyll
bundle exec jekyll new site

Configure

Syntax Highlighter

Open ~/jekyll/site/_config.yml. Add this line at the end to set rouge as a syntax highlighter:

highlighter: rouge

Test

Build
cd ~/jekyll/site
chcp 65001
bundle exec jekyll build --watch
Serve

Open a second PowerShell window

cd ~/jekyll/site
chcp 65001
bundel exec jekyll serve

Open a browser and navigate to http://localhost:4000. You should see a basic Jekyll site running.

Auto-generation

Go to ~/jekyll/site and open About.md with a text editor capable of using UTF-8 encoding, e.g. Sublime Text. Change the title from About to About Me. Save the file. Refresh your browser and navigate to http://localhost:4000/about/. You should see the page has the new About Me title.

Ubuntu with Vagrant and VirtualBox on Windows

In this post I show how you can use VirtualBox and Vagrant to create and launch an Ubuntu 14.04 virtual machine on Windows 8.1 host.

Preparation

Chocolatey

We will use Chocolatey to install all the required software. If you do not have it, you can install it by following the instructions on chocolatey.org. You may also see this post for instructions on how to set the Chocolatey cache location.

VirtualBox

In order to use VirtualBox on Windows 10 you first need to disable Hyper-V.

Install VirtualBox

In PowerShell as Administrator:

choco install virtualbox

IMPORTANT: After the installation, run Oracle VM VirtualBox once and when prompted, follow the instructions to install the Oracle VM VirtualBox Extension Pack. The version of the installed extension pack should match the version of Oracle VM VirtualBox.

Add VirtualBox To System Path

Vagrant will need access to VBoxManage.exe. For that you need to add C:\Program Files\Oracle\VirtualBox to your path. You can do that easily in PowerShell, by using the Environment.SetEnvironmentVariable .NET method.

[Environment]::SetEnvironmentVariable("Path", "C:/Program Files/Oracle/VirtualBox;" + $env:Path, "Machine")

Close the PowerShell terminal and open it again, so it can pick up the PATH to VirtualBox.

Git

We want Git for ssh.exe and other Linux tools, which are needed later by Vagrant. If you have installed ssh.exe using some other way, you may skip this step.

Install Git

In PowerShell as Administrator:

choco install git --params "/GitAndUnixToolsOnPath /NoAutoCrlf"

Now, besides git, you should have ssh, rsync, touch, clear and a number of other useful Linux tools.

Verify Git and SSH

Close and open PowerShell, so it can pick the updated PATH environment.

Verify you have git:

git --version
git version 2.6.4.windows.1

Verify you have ssh:

ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

Vagrant

Install Vagrant

In PowerShell as Administrator:

choco install vagrant
Set VAGRANT_HOME (Optional)

The Vagrant home directory is where things such as boxes are stored, so it can actually become quite large on disk. By default, this is set to C:\Users\yourusername\.vagrant.d.

In PowerShell as Administrator:

mkdir C:/VagrantHome
[Environment]::SetEnvironmentVariable("VAGRANT_HOME", "C:/VagrantHome", "Machine")

You are now ready to launch virtual machines.

Initialize an Ubuntu 14.04 Box and Launch it

Create a directory for your project, e.g. C:\Ubuntu. Open PowerShell in the new directory and run the following commands:

vagrant init ubuntu/trusty64
vagrant up

That creates a Vagrantfile file from the ubuntu/trusty64 box. Every Vagrant development environment requires a box. You can search for boxes at https://atlas.hashicorp.com/search.

VirtualBox Guest Additions

During virtual machine boot you may get a warning about the VirtualBox Guest Additions version, something similar to this:

==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.10
    default: VirtualBox Version: 5.0

There is a Vagrant plugin for that. The vagrant-vbguest plugin will check for and install the correct guest additions automatically on startup. To get the vagrant-vbguest plugin:

vagrant plugin install vagrant-vbguest

Now restart the VM:

vagrant reload

Login with SSH

vagrant ssh