Vagrant – Part II – Synced Folders, Provisioning
In this second part of the series covering, I will cover synced folders and provisioning and other small features like VirtualBox Guest plugin installation and box update.
In the first part, we saw how to initialize Vagrant, how to spin a VM, how to connect to it and how to shut it down/reload/destroy.
In case there is a newer version of the box, during bring up or reload, you will see a message:
parau-mbp:aut parau$ vagrant reload ==> default: Attempting graceful shutdown of VM... ==> default: Checking if box 'ubuntu/trusty64' is up to date... ==> default: A newer version of the box 'ubuntu/trusty64' for provider 'virtualbox' is ==> default: available! You currently have version '20180328.0.0'. The latest is version ==> default: '20180410.0.0'. Run `vagrant box update` to update. ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces...
This is how the box can be updated:
parau-mbp:aut parau$ vagrant box update ==> default: Checking for updates to 'ubuntu/trusty64' default: Latest installed version: 20180328.0.0 default: Version constraints: default: Provider: virtualbox ==> default: Updating 'ubuntu/trusty64' with provider 'virtualbox' from version ==> default: '20180328.0.0' to '20180410.0.0'... ==> default: Loading metadata for box 'https://vagrantcloud.com/ubuntu/trusty64' ==> default: Adding box 'ubuntu/trusty64' (v20180410.0.0) for provider: virtualbox ==> default: Successfully added box 'ubuntu/trusty64' (v20180410.0.0) for 'virtualbox'! parau-mbp:aut parau$
And this is how you can install VirtualBox Guest:
parau-mbp:aut parau$ vagrant plugin install vagrant-vbguest Installing the 'vagrant-vbguest' plugin. This can take a few minutes... Fetching: micromachine-2.0.0.gem (100%) Fetching: vagrant-vbguest-0.15.1.gem (100%) Installed the plugin 'vagrant-vbguest (0.15.1)'! parau-mbp:aut parau$
Next we will see how you can use a shell script as provisioner to perform several tasks during VM creation along with synced folder feature usage.
This is the Vagrantfile:
parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "ubuntu/trusty64" config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.synced_folder "vagrant/", "/var/www/html" config.vm.provision "shell", path: "lamp.sh" config.vm.network "forwarded_port", guest: 80, host: 8080 end parau-mbp:aut parau$
Vagrant allows by default sharing of folders between the host and the guest and that is the location where the Vagrantfile resides(“.”) can be found on /vagrant on the guest(in case this is Linux, I did not check what happens when the guest is Windows machine for instance). The location on the guest has to use absolute path format.
If you want to disable this default sharing, you need to use the disabled option set to true.
But I want to create my own synced folder and I want that subfolder “vagrant” from the project directory(where Vagrantfile is) to match “/var/www/html” on the guest.
Going further, when the VM is created, I want to use a provisioner, in this case “shell” that will run a script, “lamp.sh”.
This script will install Apache, Mysql and PHP(LAMP) on the guest:
parau-mbp:aut parau$ cat lamp.sh #!/bin/bash sudo apt-get -y update sudo apt-get -y install apache2 sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password lab123' sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password lab123' sudo apt-get -y install mysql-server php5-mysql php5 libapache2-mod-php5 libapache2-mod-auth-mysql php5-mcrypt echo '<html><body><b>Hello from Guest VM</b></body></html>' > /var/www/html/index.html parau-mbp:aut parau$
When Apache starts, the root directory is /var/www/html. The script will also insert some HTML tags and text in index.html file which is the default file looked up when you try to access http server.
Finally, I also did some port forwarding so that port 8080 on the host will redirect to port 80 on the guest.
There is another default port forwarding used to get access on the guest using “vagrant ssh”. Port 2222 on the host is forwarded to port 22 on the guest.
So this means that when I will access http://localhost:8080, I should be redirected to port 80 on the guest and this should work because during VM creation I installed LAMP. More than this, the page returned should be the one created during VM creation using echo command:
Going back to synced folders(vagrant <—>/var/www/html), on the host, I put a php file that displays some information about the PHP version.
parau-mbp:aut parau$ ls Vagrantfile lamp.sh python_basics vagrant parau-mbp:aut parau$ ls -l vagrant/ total 16 -rw-r--r-- 1 parau staff 53 Apr 17 20:37 index.html -rw-r--r-- 1 parau staff 19 Apr 17 18:33 info.php parau-mbp:aut parau$ cat vagrant/info.php <?php phpinfo(); ?> parau-mbp:aut parau$
Also as you can see, the synced folders allows bidirectional access, hence you see the index.html file from guest on the host.
In order to display the PHP information, I need to access “http://localhost:8080/info.php” because Apache will look in /var/www/html(which is synced with vagrant/ from the host) and it will try to find info.php(which actually comes from the host):
One last thing is about box packaging. Box packaging allows you to create a base box that has all the tools you need to avoid installing them manually every time you need a VM. In this case, we will package the current VM with LAMP as a new box so that every time it will have Apache/Mysql/PHP installed:
parau-mbp:aut parau$ vagrant package --output MY_LAMP_UBUNTU16.04.box ==> default: Attempting graceful shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Exporting VM... ==> default: Compressing package to: /Users/parau/aut/MY_LAMP_UBUNTU16.04.box parau-mbp:aut parau$
I deleted the old Vagrantfile and initialized the current directory to be a Vagrant environment:
parau-mbp:aut parau$ vagrant init MY_LAMP_UBUNTU16.04 file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box 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$ vagrant status Current machine states: default not created (virtualbox) The environment has not yet been created. Run `vagrant up` to create the environment. If a machine is not created, only the default provider will be shown. So if a provider is not listed, then the machine is not created for that environment. parau-mbp:aut parau$
And my box can be used to spin a new VM:
parau-mbp:aut parau$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'MY_LAMP_UBUNTU16.04' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Box file was not detected as metadata. Adding it directly... ==> default: Adding box 'MY_LAMP_UBUNTU16.04' (v0) for provider: virtualbox default: Downloading: file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box ==> default: Successfully added box 'MY_LAMP_UBUNTU16.04' (v0) for 'virtualbox'! ==> default: Importing base box 'MY_LAMP_UBUNTU16.04'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: aut_default_1524036048468_30756 ==> 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: Warning: Remote connection disconnect. Retrying... default: Warning: Connection reset. Retrying... default: Warning: Remote connection disconnect. Retrying... default: Warning: Connection reset. Retrying... ==> default: Machine booted and ready! [default] GuestAdditions 5.2.8 running --- OK. ==> default: Checking for guest additions in VM... ==> default: Mounting shared folders... default: /vagrant => /Users/parau/aut parau-mbp:aut parau$
Just a simple check on the guest to confirm that Apache is running:
vagrant@vagrant-ubuntu-trusty-64:~$ service apache2 status * apache2 is running vagrant@vagrant-ubuntu-trusty-64:~$
And that would be all for the second part of the series. In the next part I will cover the networking and multi-machine features of Vagrant.
I hope you found this information useful.
Paris ARAU
Latest posts by Paris ARAU (see all)
- Junos Fusion – Part IV – Satellite policies and uplink failure detection - 30 July 2018
- Junos Fusion – Part III – Satellite commands and traffic forwarding - 16 July 2018
- Junos Fusion – Part II – Configuration, Administration and Operation - 16 July 2018
- Junos Fusion – Part I – Overview, Components, Ports and Software - 11 July 2018
- Vagrant – Part IV – Network topology using Juniper and Cumulus - 26 April 2018
Hi Paris,
Thank you for your time and effort !!
Very nice elaboration of what can be done with Vagrant.
Very helpful.
wocean