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。简单来说就是配置这个虚拟主机网络连接方式,端口转发,同步文件夹,以及怎么和puppet,chef结合的一个配置文件。执行完$ vagrant init后,在工作目录中,你会发现此文件。
NOTE:配置版本说明:
1 2 3
Vagrant.configure("2") do |config| # ... end
当前支持的两个版本:“1"和"2”. “1”:描述是Vagrant 1.0.x的配置(如看到Vagrant::Config.run do |config| 此也为Vagrant 1.0.x 的配置);“2”:描述的是1.1+ leading up to 2.0.x的配置。vagrant 1.1+ 的Vagrantfiles能够与vagrant 1.0.x的Vagrantfiles保持向后兼容,也大幅引入新的功能和配置选项。
Vagrant.configure("2") do |config| config.vm.provision :shell, :inline => "echo Hello, World" end
#复杂点的调用如下: $script = <<SCRIPT echo I am provisioning... date > /etc/vagrant_provisioned_at SCRIPT Vagrant.configure("2") do |config| config.vm.provision :shell, :inline => $script end
外部脚本:
1 2 3 4 5 6 7 8 9 10 11
Vagrant.configure("2") do |config| config.vm.provision :shell, :path => "script.sh"#脚本的路径相对于项目根,也可使用绝对路径 end
#脚本可传递参数: Vagrant.configure("2") do |config| config.vm.provision :shell do |s| s.inline = "echo $1" s.args = "'hello, world!'" end end
版本"1":
内部脚本:
1 2 3
Vagrant::Config.run do |config| config.vm.provision :shell, :inline => "echo abc > /tmp/test" end
外部脚本:
1 2 3
Vagrant::Config.run do |config| config.vm.provision :shell, :path => "test.sh" end
脚本可传递参数:
1 2 3 4 5 6
Vagrant::Config.run do |config| config.vm.provision :shell do |shell| shell.inline = "echo $1 > /tmp/test" shell.args = "'this is test'" end end
Bringing machine 'default' up with 'virtualbox' provider... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] You are trying to forward to privileged ports (ports < = 1024). Most operating systems restrict this to only privileged process (typicallyprocesses running as an administrative user). This is a warning incase the port forwarding doesn't work. If any problems occur, please try a port higher than 1024. [default] Forwarding ports... [default] -- 22 => <strong>2222</strong> (adapter 1) [default] -- 80 => 8080 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] The guest additions on this VM do not match the installed version of VirtualBox! In most cases this is fine, but in rare cases it can cause things such as shared folders to not work properly. If you see shared folder errors, please update the guest additions within the virtual machine and reload your VM. Guest Additions Version: 4.1.18 VirtualBox Version: 4.2 [default] Mounting shared folders... [default] -- /var/www [default] -- /vagrant [default] Running provisioner: shell...
$vagrant up [ app ] Importing base box 'ubuntu-12-10' . . . [ app ] Matching MAC address for NAT networking . . . [ app ] Clearing any previously set forwarded ports . . . [ app ] Forwarding ports . . . [ app ] -- 22 => 2222 ( adapter 1 ) [ app ] -- 80 => 8080 ( adapter 1 ) [ app ] Creating shared folders metadata . . . [ app ] Clearing any previously set network interfaces . . . [ app ] Preparing network interfaces based on configuration . . . [ app ] Running any VM customizations . . . [ app ] Booting VM . . . [ app ] Waiting for VM to boot . This can take a few minutes . [ app ] VM booted and ready for use! [ app ] Configuring and enabling network interfaces . . . [ app ] Setting host name . . . [ app ] Mounting shared folders . . . [ app ] -- v - root : /vagrant [db] Importing base box 'ubuntu-12-10'... [db] Matching MAC address for NAT networking... [db] Clearing any previously set forwarded ports... [db] Fixed port collision for 22 => 2222. Now on port 2200. [db] Fixed port collision for 22 => 2222. Now on port 2201. [db] Forwarding ports... [db] -- 22 => 2201 (adapter 1) [db] Creating shared folders metadata... [db] Clearing any previously set network interfaces... [db] Preparing network interfaces based on configuration... [db] Running any VM customizations... [db] Booting VM... [db] Waiting for VM to boot . This can take a few minutes. [db] VM booted and ready for use! [db] Configuring and enabling network interfaces... [db] Setting host name... [db] Mounting shared folders... [db] -- v-root: / vagrant
$vagrant ssh db vagrant @db :~ $ ssh 33 . 33 . 13 . 10 The authenticity of host '33.33.13.10 (33.33.13.10)' can 't be established. ECDSA key fingerprint is a7:71:36:4c: 01:4a:38:a2:fc:fa:ea:d7:67:63:3c:40. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ' 33 . 33 . 13 . 10 ' (ECDSA) to the list of known hosts. vagrant@33.33.13.10' s password : vagrant @app :~ $