diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/vagrant/vagrantfile%2Ftips.html | |
new repository
Diffstat (limited to 'devdocs/vagrant/vagrantfile%2Ftips.html')
| -rw-r--r-- | devdocs/vagrant/vagrantfile%2Ftips.html | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/devdocs/vagrant/vagrantfile%2Ftips.html b/devdocs/vagrant/vagrantfile%2Ftips.html new file mode 100644 index 00000000..c16e3e16 --- /dev/null +++ b/devdocs/vagrant/vagrantfile%2Ftips.html @@ -0,0 +1,27 @@ +<h1 id="tips-amp-tricks"> Tips & Tricks </h1> <p>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.</p> <h2 id="loop-over-vm-definitions"> Loop Over VM Definitions </h2> <p>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:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">(1..3).each do |i| + config.vm.define "node-#{i}" do |node| + node.vm.provision "shell", + inline: "echo hello from node #{i}" + end +end +</pre></div> +<blockquote class="alert alert-warning" role="alert"> <p><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>:</p> </blockquote> <div class="highlight"><pre class="highlight ruby" data-language="ruby"># THIS DOES NOT WORK! +for i in 1..3 do + config.vm.define "node-#{i}" do |node| + node.vm.provision "shell", + inline: "echo hello from node #{i}" + end +end +</pre></div> +<p>The <code>for i in ...</code> construct in Ruby actually modifies the value of <code>i</code> for each iteration, rather than making a copy. Therefore, when you run this, every node will actually provision with the same text.</p> <p>This is an easy mistake to make, and Vagrant cannot really protect against it, so the best we can do is mention it here.</p> <h2 id="overwrite-host-locale-in-ssh-session"> Overwrite host locale in ssh session </h2> <p>Usually, host locale environment variables are passed to guest. It may cause failures if the guest software do not support host locale. One possible solution is override locale in the <code>Vagrantfile</code>:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">ENV["LC_ALL"] = "en_US.UTF-8" + +Vagrant.configure("2") do |config| + # ... +end +</pre></div> +<p>The change is only visible within the <code>Vagrantfile</code>.</p><div class="_attribution"> + <p class="_attribution-p"> + © 2010–2018 Mitchell Hashimoto<br>Licensed under the MPL 2.0 License.<br> + <a href="https://www.vagrantup.com/docs/vagrantfile/tips.html" class="_attribution-link">https://www.vagrantup.com/docs/vagrantfile/tips.html</a> + </p> +</div> |
