Vagrant – Vagrantfile

Vagrant is a tool for building and managing virtual machine environments in a single workflow. Vagrant provides a simple and easy to use command-line client for managing these environments, and an interpreter for the text-based definitions of what each environment looks like, called Vagrantfiles

Machines can be provisioned on top of VirtualBox, VMware, AWS, or any other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can automatically install and configure software on the virtual machine.

Vagrant is open source, which means that anyone can download it, modify it, and share it freely. Vagrant is available for download as a binary package for Linux, Mac, and Windows from the official download page.

Vagrantfile

The Vagrantfile is a Ruby file used to configure Vagrant on a per-project basis.

The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines. Vagrantfiles are called Vagrantfiles because the actual literal filename for the file is Vagrantfile.

The first step in configuring any Vagrant project is to create a Vagrantfile.

Vagrant has a built-in command for initialising a directory for usage with Vagrant: vagrant init. When you run that command Vagrantfile is being created automatically and you will be able to do the required changes necessary for your design.

C:\………\Vagrant-Lab\Master-1>vagrant init
A Vagrantfile has been placed in this directory. You are now
ready to vagrant up your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on vagrantup.com for more information on using Vagrant.

Vagrantfile is being created in your folder

Available configurations can be done with updating the lines in Vagrantfile with the commands as below.

  • config.vm; you can configure the Machine Settings that Vagrant manages
  • config.ssh relate to configuring how Vagrant will access your machine over SSH.
  • config.winrm relate to configuring how Vagrant will access your Windows guest over WinRM.
  • config.winssh communicator is built specifically for the Windows native port of OpenSSH.
  • config.vagrant modify the behaviour of Vagrant itself

One vagrantfile can include configuration to create multiple machines. A sample vagrantfile configuration for a Kubernetes node.

Vagrant.configure(“2”) do |config|
config.vm.box = “ubuntu/xenial64”
config.vm.box_version = “20200407.0.0”
config.vm.hostname = “k8-master1”
config.vm.network “private_network”, ip: “193.169.1.10”
config.vm.network “forwarded_port”, guest: 22, host: 2250, host_ip: “127.0.0.1”, id: “ssh”
config.vm.define “k8-master1”
config.vm.provider “virtualbox” do |vb|
vb.name = “vagrant_k8-master1”
vb.memory = “2048”
vb.cpus = 2
end
end

Vagrant Commands

When the vagrantfile is updated and ready, vagrant commands will be used to activate the workflow. Some basic commands are;

vagrant –help
vagrant global-status // shows all machines on vagrant
vagrant up <vm-name> //starts the machine
vagrant up <vm-name> –provision // Starts, provisions preinstalled machine
vagrant ssh-config <vm-name> // shows the ssh configuration of the machine
vagrant snapshot <vm-name> <snapshot-name> // takes full snapshot of the machine
vagrant halt <vm-name> // stops the machine
vagrant reload <vm-name> // reloads the machine
vagrant destroy <vm-name> // destroy the machine permanently