This post will be the first one from a series covering Vagrant.
I recently started to play with it and thought it would be great to document what I learned so that others can benefit from my notes.
Vagrant is a tool for building and managing virtual machine environments.
Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow.
Machines are provisioned on top of VirtualBox, VMware, AWS or any other provider. Then shell scripts, Chef or Puppet can automatically install and configure software on the virtual machine.
First you will need to download you backend provider(VirtualBox, VMware and so on).
I am using MacOS with VirtualBox.
Vagrant works with boxes which are base images that are used to clone virtual machines.
The first step with any Vagrant project is to create the Vagrantfile(this is actually the name of the file) which does two things:
- mark the root folder of the project
- describe what kind of VM you are using in the project
The project is started using “vagrant init”. You can get the list of commands for vagrant:
parau-mbp:aut parau$ vagrant list-commands Below is a listing of all available Vagrant commands and a brief description of what they do. box manages boxes: installation, removal, etc. cap checks and executes capability destroy stops and deletes all traces of the vagrant machine docker-exec attach to an already-running docker container docker-logs outputs the logs from the Docker container docker-run run a one-off command in the context of a container global-status outputs status Vagrant environments for this user halt stops the vagrant machine help shows the help for a subcommand init initializes a new Vagrant environment by creating a Vagrantfile list-commands outputs all available Vagrant subcommands, even non-primary ones login log in to HashiCorp's Vagrant Cloud package packages a running vagrant environment into a box plugin manages plugins: install, uninstall, update, etc. port displays information about guest port mappings powershell connects to machine via powershell remoting provider show provider for this environment provision provisions the vagrant machine push deploys code in this environment to a configured destination rdp connects to machine via RDP reload restarts vagrant machine, loads new Vagrantfile configuration resume resume a suspended vagrant machine rsync syncs rsync synced folders to remote machine rsync-auto syncs rsync synced folders automatically when files change snapshot manages snapshots: saving, restoring, etc. ssh connects to machine via SSH ssh-config outputs OpenSSH valid configuration to connect to the machine status outputs status of the vagrant machine suspend suspends the machine up starts and provisions the vagrant environment validate validates the Vagrantfile version prints current and latest Vagrant version parau-mbp:aut parau$
You can specify what box should be used by Vagrant. In this case, I used the box created by the makers of Vagrant.
You can see that the Vagrantfile was created and that is the content:
parau-mbp:aut parau$ vagrant init hashicorp/precise64 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. parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" end parau-mbp:aut parau$
Next, the box can be started like this:
parau-mbp:aut parau$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'hashicorp/precise64' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'hashicorp/precise64' default: URL: https://vagrantcloud.com/hashicorp/precise64 ==> default: Adding box 'hashicorp/precise64' (v1.1.0) for provider: virtualbox default: Downloading: https://vagrantcloud.com/hashicorp/boxes/precise64/versions/1.1.0/providers/virtualbox.box default: Download redirected to host: hashicorp-files.hashicorp.com ==> default: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'virtualbox'! ==> default: Importing base box 'hashicorp/precise64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'hashicorp/precise64' is up to date... ==> default: Setting the name of the VM: aut_default_1522660746218_67289 Vagrant is currently configured to create VirtualBox synced folders with the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant guest is not trusted, you may want to disable this option. For more information on this option, please refer to the VirtualBox manual: https://www.virtualbox.org/manual/ch04.html#sharedfolders This option can be disabled globally with an environment variable: VAGRANT_DISABLE_VBOXSYMLINKCREATE=1 or on a per folder basis within the Vagrantfile: config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.2.0 default: VirtualBox Version: 5.2 ==> default: Mounting shared folders... default: /vagrant => /Users/parau/aut parau-mbp:aut parau$
To check the status of the VM:
parau-mbp:aut parau$ vagrant status Current machine states: default running (virtualbox) The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`. parau-mbp:aut parau$
And this is how you can connect to the VM:
parau-mbp:aut parau$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ New release '14.04.5 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2 vagrant@precise64:~$
You can add another box:
parau-mbp:aut parau$ vagrant box add ubuntu/trusty64 ==> box: Loading metadata for box 'ubuntu/trusty64' box: URL: https://vagrantcloud.com/ubuntu/trusty64 ==> box: Adding box 'ubuntu/trusty64' (v20180328.0.0) for provider: virtualbox box: Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/20180328.0.0/providers/virtualbox.box box: Download redirected to host: cloud-images.ubuntu.com ==> box: Successfully added box 'ubuntu/trusty64' (v20180328.0.0) for 'virtualbox'! parau-mbp:aut parau$
And this is the box list:
parau-mbp:aut parau$ vagrant box list hashicorp/precise64 (virtualbox, 1.1.0) ubuntu/trusty64 (virtualbox, 20180328.0.0) parau-mbp:aut parau$
You can suspend the VM, which is a point in time state of the VM:
parau-mbp:aut parau$ vagrant suspend ==> default: Saving VM state and suspending execution... parau-mbp:aut parau$
You can resume the VM using:
parau-mbp:aut parau$ vagrant resume ==> default: Resuming suspended VM... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key ==> default: Machine booted and ready! ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run. parau-mbp:aut parau$
As a side note, you can shutdown the VM using “vagrant halt”.
In case you do not need the VM anymore, you can destroy it(it will free all the resources allocated when the VM was created), but it will not remove the box(you must use “vagrant box remove”):
parau-mbp:aut parau$ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives... parau-mbp:aut parau$
You can spin again the VM by doing “vagrant up”.
Let’s spin another VM by modifying the Vagrantfile and reference an Ubuntu box:
parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "ubuntu/trusty64" end parau-mbp:aut parau$
And start the VM:
parau-mbp:aut parau$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'ubuntu/trusty64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'ubuntu/trusty64' is up to date... ..... ==> default: Mounting shared folders... default: /vagrant => /Users/parau/aut parau-mbp:aut parau$
Now the VM is:
parau-mbp:aut parau$ vagrant ssh Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)
And this should cover the basics in order to get started with Vagrant.
In the next posts, I will cover more advanced topics related.
I hope you found this post informative.
Latest posts by Paris ARAU (see all)
- Channelizing interfaces on QFX5200 - 20 April 2018
- Vagrant – Part II – Synced Folders, Provisioning - 18 April 2018
- Vagrant – Basics - 3 April 2018
- Statistics for logical child links in aggregated interface on QFX10K - 7 September 2017
- DHCP Relay on EX9200 - 18 July 2017