Vagrant is a command line tool that provides you with a configurable,reproducible and portable development environment using VMs .It lets you define and use preconfigured disk images to create new VMs from.
Also you can configure vagrant to use provisioners such as shell scripts,puppets or chef to bring your VM to desired state.
Note:
Chef comes with Test kitchen ,Which enables you to test your cookbooks on Vagrant with out you needing to setup anything manually.
You only need to follow this section, if you want to learn how to use Vagrant and chef for advanced cases.
we will see how to use vagrant to manage VMs using virtual box and chef client as a provisioner.
Steps:
1) Download and install VirtualBox at https://www.virtualbox.org/wiki/Downloads
2) Download and install Vagrant at https://www.vagrantup.com/downloads.html
3) Install the omnibus vagrant plugin to enable Vagrant to install the chef client on your VM by running the following command:
local@workstation:~/chef-repo $ vagrant plugin install vagrant-plugin
Installed the plugin 'vagrant-omnibus' (1.5.0)'!
How to do it?
Create and boot a virtual node by using vagrant:
1) Visit https://github.com/chef/bento and choose a vagrant box to base your VMs on,We will use the amd64 image of ubuntu -16.04 in this example.
2) The URL of that box is http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-16.04_chef-provisionerless.box
3) Create a new Vagrantfile . Make sure that you replace <YOUR-ORG> with the name of your organization on the chef server.Use the name and URL of the box file you noted down in the first step as config.vm.box and config.vm.box_url :
local@workstation:~/chef-repo $ vi Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box= "opscode-ubuntu-16.04"
config.vm.box_url = "https://github.com/chef/bento and choose a vagrant box to base your VMs on,We will use the amd64 image of ubuntu -16.04"
config.omnibus.chef_version = :latest
config.vm.provision :chef_client do |chef|
chef.provisioning_path = "/etc/chef"
chef.chef_server_url = "https://api.chef.io/orgnsations/<YOUR_ORG>
chef.validation_key_path = ".chef/<YOUR_USER>.pem"
chef.validation_client_name = "<YOUR_USER>"
chef.node_name = "server"
end
end
Create your virtual node using Vagrant:
local@workstation:~/chef-repo $ vagrant up
Bringing machine 'default' up with 'virtualbox' provider
Login to your virtual node using SSH:
local@workstation:~/chef-repo $ vagrant ssh
vagrant@ssh:~ $
Validate that the chef server knows your virtual machine as a client called server.
local@workstation:~/chef-repo $ knife client list
osr-validator
Go to https://manage.chef.io/organsations/<YOUR ORGANSATION>/nodes and validate that your new virtual machine shows up a registered node.
How it works?
The Vagrantfile is written in a Ruby Domain specific Language (DSL) to configure the vagrant virtual machines . We want to boot a simple Ubuntu VM . Lets go through the Vagrantfile step by step
First,we create a config object . Vagrant will use this config object to configure the VM:
Vagrant.configure("2") do |config|
.....
end
Inside the config block , we tell Vagrant which VM image to use, in order to boot the node:
config.vm.box = "opscode-ubuntu-16.04"
config.vm.box_url = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-16.04_chef-provisionless.box"
We want to boot our VM using a so called Bento Box, we provided by chef . We ubuntu Version 16.04 here.
As we want our VM to have the chef client installed, we tell the omnibus vagrant plugin to use the latest version of chef client:
config.omnibus.chef_version= :latest
After selecting the VM image to boot, we configure how to provision the box by using chef.
The chef configuration happens in a nested ruby block:
config.vm.provision :chef_client do |chef|
.....
end
Inside the chef block ,we need to instruct Vagrant on how to hook up our virtual node to the Chef server.First,we need to tell vagrant where to store all the Chef stuff on your node.
chef.provisioning_path = "/etc/chef"
Vagrants need to understand the API endpoint of your chef server .If you use hosted chef, it is https://api.chef.io/organizations/<YOUR_ORG> .You need to replace <YOUR_ORG> with the name of the organization that you created in your account on hosted chef.
If your using your own chef server, change the URL accordingly,
chef.chef_server_url = "https://api.chef.io/organizations/<YOUR_ORG>"
While creating your user on hosted chef, you must have downloaded your private key,Tell vagrant where to find the file:
chef.validation_key_path = ".chef/<YOUR_USER>.pem
Also,you need to tell Vagrant which client it should use to validate itself against in the Chef Server:
chef.validation_client_name = "<YOUR__USER>"
Finally,you should tell vagrant how to name your node:
chef.node_name = "server"
After configuring your Vagrantfile,all you need to do is run the basic vagrant commands such as vagrantup,vagrant provision and vagrant ssh.To stop your VM,just run the halt command.
If you want to start from scratch again, you will have to destroy your VM and delete the node and client from chef server by running the command:
local@workstation:~/chef-repo $ vagrant destroy
local@workstation:~/chef-repo $ knife node delete server -y && knife client delete server -y
No comments:
Post a Comment