Chef Solo Basics : Simple Recipe using Chef Solo and Vagrant

Objective


To learn how to use a simple recipe that is run in a VM using Vagrant and Chef Solo.

Discussion


In the previous article, we wrote a simple hello world program that created a file with the contents 'hello world' on the local machine. We are now going to use the same recipe but it will be executed in a VM.

Steps


Step 1

Create a new project folder. My folder is named third.

Step 2

Create a Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
end

Step 3

$vagrant up
$vagrant ssh

To stop: vagrant halt. To destroy: vagrant destroy

Step 4

Create cookbooks directory in your project folder.

Step 5

Change Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.provision :chef_solo do |chef|
    chef.add_recipe "mfweb"
  end
end

Step 6

Create the folder structure:

cookbooks
└── mfweb
    └── recipes
        └── default.rb

Step 7

If we want the file hello.txt to appear in /home/vagrant. We need the following fragment in the recipe file (cookbooks/recipes/default.rb):

file "/home/vagrant/hello.txt" do
  content "hello world"
end

Step 8

Run

$vagrant halt

if your VM is still running. Then:

$vagrant up

Step 9

$vagrant ssh

Check the contents of the file.

vagrant@vagrant-ubuntu-trusty-64:~$ cat hello.txt
hello world

Summary


In this article, we wrote a simple recipe to create a file in the VM using Chef Solo and Vagrant.

Reference


Vagrant Chef Rbenv


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.