Vagrant Quick reads

Rakesh Jain
3 min readJun 15, 2020

--

Vagrant is a software configuration management (SCM) tool used for building and managing virtual machine environments.

Box A prepackaged Vagrant environment that is the same
on any computer in which it is provisioned.

Provider The underlying system that manages the
virtual machines, such as VirtualBox and Docker.

Provisioner Systems that allow you to install software
and otherwise configure guest machines, such as Chef and
Puppet.

Vagrantfile A single file used to define the desired
Vagrant environment; written in Ruby

Commands -

vagrant init
>> Initialize the directory as a Vagrant environment; creates Vagrantfile.

vagrant box add <boxname>
>> Add a Vagrant box to to environment.

vagrant up
>> Create and configure the guest machine(s) defined in the Vagrantfile.

vagrant ssh
>> SSH into a guest machine; include hostname in multi-machine
environments.

vagrant ssh-config
$ vagrant ssh-config
Host kmaster-rj
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile C:/Users/Jinendra/kubernetes_new/.vagrant/machines/kmaster-rj/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL

vagrant halt
>> Attempt a graceful shutdown of the guest machine(s)

vagrant suspend
>> Suspend the machine(s) in its current state; does not shut down machine(s).

vagrant resume
>> Start stopped guest machine(s), whether halted or suspended.

vagrant reload
>> Reboot guest machine; the same as running vagrant halt then vagrant resume.

vagrant status
>> View state of machines managed by Vagrant
$ vagrant status
Current machine states:
kmaster-rj running (virtualbox)
kworker-rj1 poweroff (virtualbox)
kworker-rj2 poweroff (virtualbox)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

vagrant destroy
>> Remove guest machine(s)

vagrant global-status
>> outputs status Vagrant environments for this user
$ vagrant.exe global-status — prune
id name provider state directory
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
45fa38b ansible-controller virtualbox poweroff C:/Users/Jinendra/ansible_controller
1efe64c kmaster virtualbox poweroff C:/Users/Jinendra/vagrant_test/k8s_ubuntu
8385055 kworker1 virtualbox poweroff C:/Users/Jinendra/vagrant_test/k8s_ubuntu
631db17 kworker2 virtualbox poweroff C:/Users/Jinendra/vagrant_test/k8s_ubuntu

vagrant port
>> displays information about guest port mappings
$ vagrant.exe port kmaster-rj
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.
22 (guest) => 2222 (host)

Example Vagrantfile — CentOS 7 with Apache

Commands
# The $samplescript variable uses Ruby to feed in a script that runs upon
# provisioning:

$samplescript = <<SCRIPT
yum install -y httpd
systemctl enable httpd
systemctl start httpd
SCRIPT

# Define Vagrant configuration version (2):
Vagrant.configure(2) do |config|

# Define what box to use (CentOS 7):
config.vm.box = “centos/7”

# Define hostname (myhost); most useful for environments with multiple guests:
config.vm.hostname = “myhost”

# Configure private network IP address:
config.vm.network “private_network”, ip: “192.168.50.10”

# Sync files between local src/ folder and guest /var/www/html folder:
config.vm.synced_folder “src/”, “/var/www/html”

# Provision guest with $samplescript variable; uses shell scripting:
config.vm.provision “shell”, inline: $samplescript

# Set guest options for VirtualBox, including memory (1024), and CPU (2):
config.vm.provider “virtualbox” do |vb|
vb.memory = “1024”
vb.cpus = “2”
e

Hope you like the tutorial. Please let me know your feedback in the response section.

Happy learning!

--

--

Rakesh Jain
Rakesh Jain

Written by Rakesh Jain

DevOps Professional | Technical writer

No responses yet