Chef Solo Basics : Simple Recipe using Chef Solo and Vagrant


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


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.


Step 1

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

Step 2

Create a Vagrantfile:


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

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:


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

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

Step 6

Create the folder structure:

└── 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"

Step 8


$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


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


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.