website/docs: tips & tricks for vfile [GH-2788]

This commit is contained in:
Mitchell Hashimoto 2014-01-08 10:42:37 -08:00
parent 86bb6105d5
commit dbaa48ff5b
3 changed files with 57 additions and 3 deletions

View File

@ -128,9 +128,10 @@
<ul class="sub unstyled"> <ul class="sub unstyled">
<li<%= sidebar_current("vagrantfile-version") %>><a href="/v2/vagrantfile/version.html">Configuration Version</a></li> <li<%= sidebar_current("vagrantfile-version") %>><a href="/v2/vagrantfile/version.html">Configuration Version</a></li>
<li<%= sidebar_current("vagrantfile-vagrantversion") %>><a href="/v2/vagrantfile/vagrant_version.html">Minimum Vagrant Version</a></li> <li<%= sidebar_current("vagrantfile-vagrantversion") %>><a href="/v2/vagrantfile/vagrant_version.html">Minimum Vagrant Version</a></li>
<li<%= sidebar_current("vagrantfile-machine") %>><a href="/v2/vagrantfile/machine_settings.html">Machine Settings</a></li> <li<%= sidebar_current("vagrantfile-tips") %>><a href="/v2/vagrantfile/tips.html">Tips & Tricks</a></li>
<li<%= sidebar_current("vagrantfile-ssh") %>><a href="/v2/vagrantfile/ssh_settings.html">SSH Settings</a></li> <li<%= sidebar_current("vagrantfile-machine") %>><a href="/v2/vagrantfile/machine_settings.html" class="nocap">config.vm</a></li>
<li<%= sidebar_current("vagrantfile-vagrant") %>><a href="/v2/vagrantfile/vagrant_settings.html">Vagrant Settings</a></li> <li<%= sidebar_current("vagrantfile-ssh") %>><a href="/v2/vagrantfile/ssh_settings.html" class="nocap">config.ssh</a></li>
<li<%= sidebar_current("vagrantfile-vagrant") %>><a href="/v2/vagrantfile/vagrant_settings.html" class="nocap">config.vagrant</a></li>
</ul> </ul>
<% end %> <% end %>

View File

@ -119,6 +119,10 @@
padding: 12px 0; padding: 12px 0;
border-top: 1px solid fade(@white, 20%); border-top: 1px solid fade(@white, 20%);
a.nocap {
text-transform: none;
}
&:first-child { &:first-child {
border-top: none; border-top: none;
} }

View File

@ -0,0 +1,49 @@
---
page_title: "Tips & Tricks - Vagrantfile"
sidebar_current: "vagrantfile-tips"
---
# Tips & Tricks
The Vagrantfile is a very flexible configuration format. Since it is just
Ruby, there is a lot you can do with it. However, in that same vein, since
it is Ruby, there are a lot of ways you can shoot yourself in the foot. When
using some of the tips and tricks on this page, please take care to use them
correctly.
## Loop Over VM Definitions
If you want to apply a slightly different configuration to many
multi-machine machines, you can use a loop to do this. For example, if
you wanted to create three machines:
<pre class="prettyprint">
(1..3).each do |i|
config.vm.define "slave-#{i}" do |slave|
slave.vm.provision "shell",
inline: "echo hello from slave #{i}"
end
end
</pre>
<strong>Warning:</strong> The inner portion of multi-machine definitions
and provider overrides are lazy-loaded. This can cause issues if you change
the value of a variable used within the configs. For example, the loop below
<em>does not work</em>:
<pre class="prettyprint">
# THIS DOES NOT WORK!
for i in 1..3 do
config.vm.define "slave-#{i}" do |slave|
slave.vm.provision "shell",
inline: "echo hello from slave #{i}"
end
end
</pre>
The "for i in ..." construct in Ruby actually modifies the value of `i`
for each iteration, rather than making a copy. Therefore, when you run this,
every slave will actually provision with the same text.
This is an easy mistake to make, and Vagrant can't really protect against it,
so the best we can do is mention it here.