https://docs.vagrantup.com/v2/
My front end development environments are becoming very dependent on the software installed on my machine (Node/NPM, Grunt, Yeoman, Bower, Ruby Gems etc). When another developer picks up one of my projects they often have problems with software versions so that the build system cannot be run and they have to spend hours troubleshooting their system, often requiring me to hold their hand which kind of defeats the purpose of handing the work off to them in the first place.
Similarly our back end development environments are becoming increasingly complex, with software like Rails, Node/NPM, Composer, different versions of PHP, etc.
Vagrant & Puppet solve this problem by allowing developers to author configuration files in which they describe which software needs to be installed on which OS, in order to have a usable environment.
- https://docs.vagrantup.com/v2/getting-started/index.html
- http://stackoverflow.com/questions/10155708/vagrant-box-add-where-does-box-file-get-downloaded-to
Vagrant requires a Virtual Machine to run boxes. By default Vagrant supports the VirtualBox virtualisation software, which is free and is what most of us use at work to test our websites on MS Windows (for different versions of MSIE).
- Download and Install Virtualbox for your OS (~110mb). The same Vagrant API also works with other providers including VMWare and AWS.
Vagrant needs a box to install software on.
Linux base boxes can be downloaded for free, but Apple forbids upload of OS X boxes. So we'd need to create our own OS X base box(es). See Boxes, below.
- Download and start the default Vagrant box (VM):
vagrant up
- The will also download the default Linux box to
/Users/USERNAME/.vagrant.d/boxes/BOXNAME
. The.vmdk
file for thehashicorp/precise32
box was295.2mb
- VirtualBox will show that a new VM is running, but there is no UI
- Connect to the box via SSH:
vagrant ssh
- Do some stuff
- Exit SSH:
exit
- Destroy the box:
vagrant destroy
- Windows users can also do
vagrant rdp
- https://docs.vagrantup.com/v2/getting-started/provisioning.html
- https://docs.vagrantup.com/v2/cli/provision.html
The Provisioning feature allows software to be automatically installed on vagrant up
:
- Compose a shell script to install the software
- Save this to the host directory as eg
bootstrap.sh
- Add this script to the
Vagrantfile
:config.vm.provision :shell, path: "bootstrap.sh"
(Ruby) - Reload an existing machine after updating provisioning:
vagrant reload --provision
- Make simple modifications to the provisioning scripts on your machine and check for the desired results:
vagrant provision
, optionally with--provision-with x,y,z
This forwards network traffic from port 4567
on the host machine to port 80
(for use by Apache) on the guest. This allows a website to be edited on the host machine but viewed using the webserver / LAMP stack installed on the guest machine.
- Install Apache (https://docs.vagrantup.com/v2/getting-started/provisioning.html)
- Add this to the
Vagrantfile
:config.vm.network :forwarded_port, host: 4567, guest: 80
(Ruby) - Reload an existing machine after updating port forwarding:
vagrant reload
- You can now view files on the webserver @
http://127.0.0.1:4567
- The host directory is the one where you've run
vagrant up
, ie the one containingVagrantfile
(Vagrant automatically climbs up the directory tree looking for the firstVagrantfile
it can find, starting first in the current directory) - Files within the host directory can be edited on the host and will automatically sync to the guest
This allows you to eg share a local website with a user outside your network.
- Sign up for an Atlas account
- Check if you're already logged in:
vagrant login --check
vagrant login
vagrant share
- In other use cases you can also
vagrant share --ssh
,vagrant connect --ssh
andvagrant connect
- An
obscure-url-NNNN.vagrantshare.com
will be generated and output to the Terminal. The URL is accessible by anyone so be careful what you share. - End the sharing session:
Ctrl+C
. The URL is no longer accessible. - Logout:
vagrant login --logout
-
vagrant share
... will automatically share as many ports as possible for remote connections. If the Vagrant environment has a static IP or DNS address, then every port will be available. Otherwise, Vagrant will only expose forwarded ports on the machine. -
Note the share name at the end of calling
vagrant share
, and give this to the person who wants to connect to your machine. They simply have to callvagrant connect NAME
. This will give them a static IP they can use to access your Vagrant environment. -
Share sessions expire after a short time (currently 1 hour)
-
Shared web applications must use relative paths for loading any local assets such as images, stylesheets, javascript.
-
HTTPS (SSL):
vagrant share
by default looks for any SSL traffic on port443
in your development environment. If it can't find any, then SSL is disabled by default. - Sharing vs Security: https://docs.vagrantup.com/v2/share/security.html
vagrant up
- set up / resume the guest VM. The optional--provider
flag allows you to change from VirtualBox to eg VMWare or AWSvagrant reload
-vagrant halt
thenvagrant up
the guest VMvagrant reload --provision
-vagrant halt
thenvagrant up
the guest VM, forcing any provisioners to re-runvagrant resume
- resumes a Vagrant managed machine that was previously suspended
vagrant suspend
- saves the current running state of the VMvagrant halt
- if possible, gracefully shut down the VM's OS, otherwise just power it off; preserve the contents of the VM's HDDvagrant destroy
- removes all traces of the VM from the host machine
vagrant status
- See all primary commands:
vagrant
- See all commands:
vagrant list-commands
- Get help on a particular command:
vagrant COMMAND -h
vagrant version
- checks site for latest versionvagrant --version
- just shows the locally installed version
This is stil under development:
Vagrant is architected with plugins.
- https://docs.vagrantup.com/v2/cli/plugin.html
- https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Plugins
https://docs.vagrantup.com/v2/boxes/base.html
- https://atlas.hashicorp.com/boxes/search
- http://www.vagrantbox.es/
- https://github.com/mattbanks/vagrant-dev-box
- There are 3 OS X boxes listed on Atlas, but all are self-hosted which means they cannot be installed by the public using
vagrant up
- Andrew Dryga has created a box (~10.8GB) which he can't upload to VagrantCloud as Apple forbid it.
-
Base boxes are extremely useful for having a clean slate starting point from which to build future development environments.
-
Creating a base box can be a time consuming and tedious process, and is not recommended for new Vagrant users. If you're just getting started with Vagrant, we recommend trying to find existing base boxes to use first.
- Experimental: Using Veewee to Build OS X VMs
- [OS X templates for Packer and VeeWee(https://github.com/timsutton/osx-vm-templates) (supports both VMWare Fusion and VirtualBox)
vagrant package
packages a currently running VirtualBox (only) environment into a re-usable box - https://docs.vagrantup.com/v2/cli/package.html- Note that Virtualbox support for OS X is experimental
- OS X on OS X: Running OS X on VirtualBox
We develop in OS X and VirtualBox support for OS X is only 'experimental'. From what I've read, VMWare Fusion appears to be a more robust option when it comes to emulating OS X. It still requires the host machine to be running Intel Mac hardware (as per Apple's system requirements) but as we're already running Macs that shouldn't be an issue.
- Creating an OS X Base Box for Vagrant With Packer - uses VMWare Fusion (VMware Fusion 7 = 76.25AUD)
- The Vagrant VMWare Provider (USD $79) is also required - this does not include a license to the VMware software, which must be purchased separately
- Installing OS X 10.9 Mavericks as a guest operating system in VMware Fusion 6.0 (Video)
- Creating an OS X Server Virtual Machine in VMware Fusion
Puppet or Chef? Ansible or Salt?
Whereas Puppet and Chef will appeal to developers and development-oriented shops, Salt and Ansible are much more attuned to the needs of system administrators. Ansible's simple interface and usability fit right into the sys admin mindset, and in a shop with lots of Linux and Unix systems, Ansible is quick and easy to run right out of the gate.
Salt is the sleekest and most robust of the four, and like Ansible it will resonate with sys admins. Highly scalable and quite capable, Salt is hamstrung only by the Web UI.
Puppet is the most mature and probably the most approachable of the four from a usability standpoint, though a solid knowledge of Ruby is highly recommended. Puppet is not as streamlined as Ansible or Salt, and its configuration can get Byzantine at times. Puppet is the safest bet for heterogeneous environments, but you may find Ansible or Salt to be a better fit in a larger or more homogenous infrastructure.
Chef has a stable and well-designed layout, and while it's not quite up to the level of Puppet in terms of raw features, it's a very capable solution. Chef may pose the most difficult learning curve to administrators who lack significant programming experience, but it could be the most natural fit for development-minded admins and development shops.