Introduction to Vagrantfile

Vagrant makes it easy to create reproducible virtualised environments. Machines are provisioned on top of VirtualBox, VMWare, AWS, or any other provider. Vagrant includes support for configuration management tools like Chef or Ansible. Naturally you can use simple shell scripts to automatically install and configure software as well. All of this is defined in a Vagrantfile.

The Vagrantfile defines the "box", VM customizations (like memory and networking settings), and which provisioners to run. The "box" is a base image. This may be a VirtualBox image or an AMI (Amazon Machine Image) on AWS. Essentially, this is where you decide the operating system. Then, you use the various provisioners to set everything up. Once you have everything in the Vagrantfile, you're ready to go. Now anyone can recreate the same environment with a few short commands.

These features are useful in modern teams. Engineers can define a Vagrantfile that installs all the programming language tools and databases for their work. Then, they can be distribute it amongst them, so that everyone can work in the same environment of their OS. DevOps teams can use Vagrant to spin up multiple VMs running different Linux distributions to test configuration management in different systems. It also makes it easy to create disposable environments to experiment with different technologies. It's also possible to package up your entire product in a Vagrantfile and give that to your designers or product owners, as a way to experiment with the product.

Basic settings in Vagrantfile

1) Box name
Vagrant.configure(2) do |config|
            config.vm.box = "IMAGE_NAME"
 end
As it stands, all this Vagrantfile does is define the Vagrant configuration version as 
2(Vagrant.configure(2) do |config|) and sets the guest machine to work from the “base” box (config.vm.box = "base"). Note that the config at the start of the box configuration references directly back to the |config| value in the version line. All our configuration settings will start with config.

2) Setting hostname
We also want to define the hostname, myhost, for this box. We can do this by simply adding config.vm.hostname = "myhost" to our code:
Vagrant.configure(2) do |config|
   config.vm.box = "centos/7"
   config.vm.hostname = "myhost"
end

3) We can also configure networking settings in Vagrant. For this example, we’re going to set an IP address within a private network range:
Vagrant.configure(2) do |config|
   config.vm.box = "centos/7"
   config.vm.hostname = "myhost"
   config.vm.network "private_network", ip: "192.168.50.10"
 end

4) We can sync a folder to a folder within our guest machine.
Vagrant.configure(2) do |config|
   config.vm.box = "centos/7"
   config.vm.hostname = "myhost"
   config.vm.network "private_network", ip: "192.168.50.10"
   config.vm.synced_folder "src/", "/var/www/html"
 end

5) Provisioning
Vagrant is able to pair with a number of provisioning tools to configure your server upon creation. The most basic uses simple shell scripting. This is what we’ll be using. Specifically, we will be using Ruby syntax to feed a series of commands into a variable, and then call that variable in the line that configures our provisioning.
Vagrant.configure(2) do |config|
   config.vm.box = "centos/7"
   config.vm.hostname = "myhost"
   config.vm.network "private_network", ip: "192.168.50.10"
   config.vm.synced_folder "src/", "/var/www/html"
   config.vm.provision "shell", inline: "yum -y install httpd"
 end

6) Port mapping:
We can configure port mapping from host to guest
Vagrant.configure(2) do |config|
   config.vm.box = "centos/7"
   config.vm.hostname = "myhost"
   config.vm.network "private_network", ip: "192.168.50.10"
   config.vm.synced_folder "src/", "/var/www/html"
   config.vm.provision "shell", inline: "yum -y install httpd"
   config.vm.network "forwarded_port", guest: 80, host: 80
 end

7) Memory and CPU configuration
  config.vm.provider :virtualbox do |v|
    v.memory = 1024
    v.cpus = 2
  end

Final Vagrantfile will be looks as below
Vagrant.configure(2) do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "myhost"
  config.vm.network "private_network", ip: "192.168.50.10"
  config.vm.synced_folder "src/", "/var/www/html"
  config.vm.provision "shell", inline: "yum -y install httpd"
  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end



<< Previous                                                                                                                                 Next >>




No comments:

Post a Comment