-
Get Started
-
Overview
-
Install Vagrant
- $ gem install vagrant
$ gem list vagrant -d
- $ git clone git://github.com/mitchellh/vagrant.git
$ bundle install
$ bundle show vagrant
-
Your First Vagrant Virtual Environment
- $ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
$ vagrant init lucid32
$ vagrant up
-
Why Vagrant?
- Web developers use virtual environments every day with their web applications.
- By providing easy to configure, lightweight, reproducible, and portable virtual machines targeted at development environments, Vagrant helps maximize your productivity and flexibility.
- Vagrant is a development tool which stands on the shoulders of giants, using tried and proven technologies to achieve its magic.
- Vagrant uses Oracle’s VirtualBox to create its virtual machines and then uses Chef or Puppet to provision them.
-
For Solo Developers
- Each project depends on its own libraries, message queue systems, databases, framework, and more, each with their own versions.
-
For Teams
- Vagrant gives teams the ability to enforce a consistent and portable virtual development environment that is easy to create and quick to setup.
- For Companies
-
Introduction
-
binaries
- help
- version
- status
-
box
- list
- add/remove
- repackage
-
init/destroy
- vagrant init [box_name] [box_url]
- up/halt
- reload
- suspend/resume
- ssh/ssh_config
- package
- provision
-
Vagrantfile
-
Vagrant::Config.run do |config|
...
end
- config.vm.box = "my_box"
-
Project Setup
- $ mkdir vagrant_guide
$ cd vagrant_guide
$ vagrant init # using box "base"
-
Boxes
- ~/.vagrant.d/boxes
-
Getting a Base Box
- $ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
-
Removing Boxes
- $ vagrant box remove my_box
-
Configuring the Project to use the Box
-
Subcommand init
- $ vagrant init [box_name] [URL]
-
Vagrantfile
- config.vm.box = "lucid32"
-
SSH
- Nothing beats the power of the command line.
- $ vagrant ssh
-
Accessing the Project Files
- Default directory "/vagrant" on guest
- config.vm.share_folder "v-data", "/vagrant_data", "../data"
-
Provisioning
-
chef
- config.vm.provision :chef_solo do |chef|
chef.recipe_url = "http://files.vagrantup.com/getting_started/cookbooks.tar.gz"
chef.add_recipe("vagrant_main")
end
-
Port Forwarding
- config.vm.forward_port("web", 80, 4567)
- $ vagrant reload
-
Packaging
-
Vagrantfile.pkg
- Vagrant::Config.run do |config|
# Forward apache
config.vm.forward_port("web", 80, 8080)
end
- $ vagrant package --vagrantfile Vagrantfile.pkg
- What's with the MAC address?
-
Teardown
- $ vagrant suspend
- $ vagrant halt
- $ vagrant destroy
-
LE
- pros and cons
-
Rebuild Instantly
- $ cd project_dir
$ vagrant up
# BINGO
-
Documentation
-
Commands
-
$ vagrant
-
help
- COMMAND
-
version
- $ be bin/vagrant version
Vagrant version 0.8.7.dev
-
box
- add
- NAME URI
- help
- $ be bin/vagrant box help
Tasks:
vagrant box add NAME URI # Add a box to the system
vagrant box help [COMMAND] # Describe subcommands or one specific subcommand
vagrant box list # Lists all installed boxes
vagrant box remove NAME # Remove a box from the system
vagrant box repackage NAME # Repackage an installed box into a `.box` file.
- list
- remove
- NAME
- repackage
- NAME
-
package
- --vagrantfile
- --vagrantfile Vagrantfile.pkg
- --include
- --include README.txt
- init
- status
-
up
- Requires Vagrantfile
- Build the VM based on the box
- Setup shared folders
- Setup forwarded ports
- Provision with chef or Puppet (if configured)
- Boot in the background
- reload
- resume
- suspend
- halt
- destroy
- provision
- ssh
-
ssh-config
- $ vagrant ssh-config
Host vagrant
HostName localhost
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
IdentityFile /opt/local/lib/ruby/gems/1.8/gems/vagrant-0.3.0/keys/vagrant
- $ scp vagrant:/vagrant/my_file.txt ~/Desktop/my_file.txt
-
Vagrantfile
-
Load Order
-
Vagrantfile from the gem directory
- ~/.rvm/gems/`rvm current`/gems/vagrant-0.8.6/config/default.rb
-
Vagrantfile from the box directory
- ~/.vagrant.d/boxes/base/Varantfile
- ~/.vagrant.d/boxes/base/include/_Vagrantfile
-
Vagrantfile from the home directory
- ~/.vagrant.d/Vagrantfile
-
Vagrantfile from the project directory
- ./Vagrantfile
-
Structure
- Vagrant::Config.run do |config|
config.xxx.yyy.zzz = "..."
end
-
Options
-
config
- vm
- box
- "base"
- "my_box"
- box_url
- "http://some.url.for/some_remote_box.box"
- "/some/local/path/to/a.box"
- customize
- structure
- config.vm.customize do |vm|
vm.xxx = XXX
vm.yyy = "YYY"
end
- memory_size
- name
- forward_port
- "web", 80, 8080
- "ftp", 21, 4567
- "ssh", 22, 2222, :auto => true
- network
- web_config.vm.network("192.168.1.10")
- db_config.vm.network("192.168.1.11")
- share_folder
- "v-root", "/vagrant", ".", :nfs => true
- "my-folder", "/folder", "/path/to/real/folder"
- "another-folder", "/other", "../other"
- "third-folder", "/third", ".", :owner => "my-user", :group => "my-group"
- define
- Vagrant::Config.run do |config|
config.vm.define :web do |web_config|
web_config.vm.box = "web"
web_config.vm.forward_port("http", 80, 8080)
end
config.vm.define :db do |db_config|
db_config.vm.box = "db"
db_config.vm.forward_port("db", 3306, 3306)
end
end
- provision
- provision
- config.vm.provision :chef_solo
- config.vm.provision :chef_solo, :cookbooks_path => "cookbooks", :run_list => "recipe[foo]"
- config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = "cookbooks"
chef.add_recipe "foo"
end
- box_ovf
- "box.ovf"
- base_mac
- "0800274153F3"
- useful for those creating boxes for distribution
- chef
- recipe_url
- "http://files.vagrantup.com/getting_started/cookbooks.tar.gz"
- cookbooks_path
- chef.cookbooks_path = "cookbooks"
- chef.cookbooks_path = ["cookbooks", "~/company/cookbooks"]
- chef.cookbooks_path = [:vm, "cookbooks"]
- chef.cookbooks_path = ["local-cookbooks", [:vm, "cookbooks"]]
- add_recipe
- chef.add_recipe "foo"
- roles_path
- chef.roles_path = "roles"
- add_role
- chef.add_role("web")
- json
- config.chef.json = {
:instance_role => "vagrant",
:recipes => ["vagrant_main"]
}
- config.chef.json.merge!({
:mysql => {
:server_root_password => "my_root_password"
}
})
- can be used to set attributes for the cookbooks used in provisioning
- provisioning_path
- chef.provisioning_path = "/tmp/vagrant-chef"
- run_list
- # Accessing the run list directly
config.chef.run_list = ["recipe[foo]", "recipe[bar]"]
- # Using the helpers
config.chef.add_recipe("foo")
config.chef.add_role("bar")
- provisioning_path
- chef.provisioning_path = "/tmp/vagrant-chef"
- package
- name
- extension
-
Provisioners
- chef
- puppet
-
shell
- config.vm.provision :shell, :inline => "echo foo > /vagrant/test"
- $ cat test.sh
#!/bin/bash
echo Hello, World!
$ cat Vagrantfile
...
config.vm.provision :shell, :path => "test.sh"
...
- config.vm.provision :shell do |shell|
shell.inline = "echo $1 > /vagrant/test"
shell.args = "'write this to a file'"
end