Posts Chef Solo with virtualbox and vagrant part 3
Post
Cancel

Chef Solo with virtualbox and vagrant part 3

  • Installing/Updating chef-solo

If you downloaded the Ubuntu image that I mentioned then that image comes preinstalled with chef-solo.

In case you still want to install or update chef-solo then we can add a custom command in the VagrantFile to achieve that.

On your local pc

Go to the c drive testBox directory

Edit the VagrantFile

1
2
c:\testBox
                -- Vagrantfile

Add the below line

1
config.vm.provision "shell", inline: "curl -L https://www.opscode.com/chef/install.sh | bash", privileged: false

This line will execute the curl command which will download and execute the chef-solo installation script, thereby installing or updating the chef-solo on guest OS

  • Creating chef cookbook

Chef provides a way to create software installation/configuration steps known as cookbooks.

We write cookbook once, and then we can create multiple virtual machines just by including them in the vagrant configuration.

Depending on our need we can either create our own cookbook or download it from internet.

Once the default cookbook setup is complete we can then add custom steps to modify the software installation as required.

To create a sample cookbook that installs apache2 service

Create the below folder structure highlighted in red, the subfolder is denoted by “–“

1
2
3
4
5
c:\testBox
                -- Vagrantfile
                -- cookbooks
                                    -- apache2
                                                    -- recipes

Here inside testBox folder we will create new folder called “cookbooks”

inside “cookbooks” folder we will then create “apache2” folder

inside “apache2” folder we will then create “recipies” folder

  • To add apache2 installation steps

Now in c:\testBox\cookbooks\apache\recipes folder create a file “default.rb” (without quotes).

Inside this file add the below lines highlighted in yellow.

1
2
3
package "apache2" do
  action :install
end
  • Example of bash command to restart apache
1
2
3
4
5
6
bash "restart_apache" do
  user "root"
  code <<-EOF
    /etc/init.d/apache2 restart
  EOF
end

chef will use guest operating system inbuilt software installation commands to install the apache2 package.

Note:- Internet connectivity will be required to successfully complete this step.

To add the apache cookbook to chef-solo execution during guest VM creation

Edit the VagrantFile present in the below location

1
2
c:\testBox
    -- Vagrantfile

Add the below lines to the VagrantFile

1
2
3
4
5
config.vm.provision :chef_solo do |chef|
     chef.cookbooks_path = "cookbooks"

     chef.add_recipe "apache2"
end

The above lines inform vagrant that once the guest OS is up and running it should then execute the above recipe that is placed inside the cookbooks folder.

This post is licensed under CC BY 4.0 by the author.