Explore Advanced Vagrant Features

In First Steps with Vagrant, you learned how to install Vagrant, set up and run a basic virtual machine. Now, we will cover a few more complex features, such as synced folders, provisioning scripts and running multiple virtual machines at the same time.

Synced Folders

Synced folders allow us to share folders and files between the virtual machine and the host machine. The changes are immediate and are applied in both directions???changes made on the host machine will be visible on the virtual machine and vice versa. Synced folders are mapped in Vagrantfile:

Vagrant.configure("2") do |config|  # other config here  config.vm.synced_folder "home/myuser/sites", "/var/www"end 

The first path is the directory location on the host machine, while the second path is the location of the directory on guest machine (virtual machine). The default owner of the folder on the virtual machine is ?vagrant? (the root user), so we often need to manually specify the owner:

config.vm.synced_folder "home/myuser/sites", "/var/www", owner: "www-data", group: "www-data" 

If multiple synced folders are needed:

config.vm.synced_folder "home/myuser/sites", "/var/www", owner: "www-data", group: "www-data"config.vm.synced_folder "home/myuser/documents", "/home/vagrant/documents" 

We can also change the file and directory permissions:

config.vm.synced_folder "home/myuser/public", "/home/vagrant/public", :mount_options => ["dmode=777", "fmode=666"] 

Sometimes the default shared folder implementation (Virtualbox shared folders) will have high performance penalties. In those cases, it is possible to change syncing mechanism to NFS, RSync or SMB. Read more about it here.

Provisioning

Vagrant offers a wide variety of boxes. A box is a downloadable virtual box, which contains an operating system and some pre-installed software. In most cases, one of the many available boxes will fit your needs. But sometimes, you might want to install additional software or change the configuration. That?s the situation in which you will use a provisioning script, i.e. a script that will automatically execute predefined commands upon starting the virtual machine.

A provisioning script is usually a shell script, which can be written inline in Vagrantfile, saved to an external file or called from an external link:

Vagrant.configure("2") do |config|  config.vm.provision "shell", path: "script.sh"endVagrant.configure("2") do |config|  config.vm.provision "shell", path: "https://example.com/provisioner.sh"end 

Ansible, Chef, Docker and Puppet provisioners are also available.

Creating Multiple Virtual Machines

Using this feature, we can accurately represent and test systems with multiple servers. As with all other features, they are configured in Vagrantfile:

Vagrant.configure("2") do |config|  # This configuration applies to all virtual machines  # Set the box to Ubuntu 14.04 64-bit OS  config.vm.box = "ubuntu/trusty64"  # Map port 8080 on the host to port 80 on the virtual machine  # http://localhost:8080 should open the default Apache page on the virtual machine  config.vm.network "forwarded_port", guest: 80, host: 8080  # This configuration is applied only to the virtual machine named ?web?  config.vm.define "web" do |web|    # LAN IP address for the virtual machine    web.vm.network :private_network, ip: "192.168.33.10"  end  # This part is applied only to the second virtual machine named ?db?  config.vm.define "db" do |db|    # LAN IP address for the virtual machine    db.vm.network :private_network, ip: "192.168.33.11"  endend 

To run a virtual machine, you would enter vagrant up web or vagrant up db. To SSH into a virtual machine, you would use vagrant ssh web?or vagrant ssh dbNext Steps Explore available Vagrant boxes?or learn how to make a custom one.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may