TO DO
Cheat Sheet
https://gist.github.com/wpscholar/a49594e2e2b918f4d0c4
Official getting start tutorial
Project setup
The first step in configuring any Vagrant project is to create aVagrantfile. The purpose of the Vagrantfile is twofold:
Mark the root directory of your project. Many of the configuration options in Vagrant are relative to this root directory.
Describe the kind of machine and resources you need to run your project, as well as what software to install and how you want to access it.
vagrant init
vagrant init hashicorp/precise64
Box
Boxes are added to Vagrant with vagrant box add. This stores the box under a specific name so that multiple Vagrant environments can re-use it. If you have not added a box yet, you can do so now:
$ vagrant box add hashicorp/precise64
Inside vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.box_version = "1.1.0"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end
Up and ssh
vagrant up
vagrant ssh
Synced folder
By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the /vagrant
directory in your guest machine.
Provisioning
If you've never used a configuration management system before, it is recommended you start with basic shell scripts for provisioning.
You can find the full list of built-in provisioners and usage of these provisioners in the navigational area to the left.
Provision time
Provisioning happens at certain points during the lifetime of your Vagrant environment:
- On the first vagrant up that creates the environment, provisioning is run. If the environment was already created and the up is just resuming a machine or booting it up, they will not run unless the --provision flag is explicitly provided.
- When vagrant provision is used on a running environment.
- When vagrant reload --provision is called. The --provision flag must be present to force provisioning.
You can also bring up your environment and explicitly not run provisioners by specifying --no-provision.
Use shell
- Inline script
Vagrant.configure("2") do |config|
config.vm.provision "shell",
inline: "echo Hello, World"
end
- External script
Setup vagrantfile as
Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "script.sh"
end
Use Ansible
The Vagrant Ansible provisioner allows you to provision the guest usingAnsible playbooks by executing ansible-playbook from the Vagrant host.
Setup vagrantfile as
Vagrant.configure("2") do |config|
#
# Run Ansible from the Vagrant Host
#
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
Vagrantfile
The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines.
Configuration version
Vagrant.configure("1") do |config|
# v1 configs...
end
Vagrant.configure("2") do |config|
# v2 configs...
end
Networking
Port forwarding
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
end
Run a vagrant reload or vagrant up (depending on if the machine is already running) so that these changes can take effect.