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.
Create a new project folder. My folder is named third.
Create a Vagrantfile:
VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "ubuntu/trusty64" end
$vagrant up $vagrant ssh
To stop: vagrant halt. To destroy: vagrant destroy
Create cookbooks directory in your project folder.
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
Create the folder structure:
cookbooks └── mfweb └── recipes └── default.rb
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
if your VM is still running. Then:
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.
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