Category Archives: vagrant

Command cheat sheet for vagrant

Few months back, I wrote article on vagrant usage which basically covers the very brief introduction to vagrant.

https://ervikrant06.wordpress.com/2015/08/26/how-to-use-vagrant-to-create-vm-part-1/

I started learning about ansible, again I need to visit the basic concept of vagrant to make my learning quick with ansible.  In this post, I am just going to list the commands and codes which I am going to use most of the time :

Commands :

1) We already know that we can create a file using “vagrant init” if we want to re-create file in the same directory and without any comments.

# vagrant init -f -m

2) Creating file specifying the name of box.

# vagrant init -f rhel-7.2

You need to replace the name with your box name.

3) To check the global status of all vagrant boxes.

# vagrant global-status

4) Checking the port mapping between host and vagrant guest.

# vagrant port <guest name>

This command doesn’t work for libvirtd provider, I have used this command with Virtualbox provider.

5) Commands to destroy, shutdown or suspend the vagrant environment.

# vagrant suspend
# vagrant halt
# vagrant destroy

Code tips :

1) Enabling the port forwarding and running a script while bringing up the vagrant box.

Vagrant.configure(“2”) do |config|
config.vm.box = “rhel-7.2”
config.vm.provision :shell, path: “bootstrap.sh”
config.vm.network :forwarded_port, guest: 80, host: 4567
end

Above stanza can be changed if we want to avoid any sort of port conflict while spawning multiple instances.

Vagrant.configure(“2”) do |config|
config.vm.box = “rhel-7.2”
config.vm.provision :shell, path: “bootstrap.sh”
config.vm.network :forwarded_port, guest: 80, host: 4567

auto_correct: true
end

2) bootstrap.sh script will run only once when you brought up the vagrant instance. To run that script again.

# vagrant up –provision
# vagrant provision
# vagrant reload –provision

3) If we want to bring the vagrant instance up without running mentioned script.

# vagrant up –no-provision

4) Running a particular box version.

Vagrant.configure(“2”) do |config|
config.vm.box = “rhel-7.2”
config.vm.box_version = “1.1.0”
end

I will keep on adding more hacks into this article.