5

When I look at the Vagrant docs https://www.vagrantup.com/docs/networking/forwarded_ports.html it gives an example like this:

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 2003, host: 12003, protocol: "tcp"
  config.vm.network "forwarded_port", guest: 2003, host: 12003, protocol: "udp"
end

However, the following also seem to be valid:

Single quotes

Vagrant.configure("2") do |config|
  config.vm.network 'forwarded_port', guest: 2003, host: 12003, protocol: "tcp"
  config.vm.network 'forwarded_port', guest: 2003, host: 12003, protocol: "udp"
end

Colon Prefix

Vagrant.configure("2") do |config|
  config.vm.network :forwarded_port, guest: 2003, host: 12003, protocol: "tcp"
  config.vm.network :forwarded_port, guest: 2003, host: 12003, protocol: "udp"
end

What is the difference in these syntax's? Are double quotes only supposed to be used when you have variables like in puppet?

What is the colon prefix syntax? It is confusing because the rest of the keywords on the line have colons suffixed.

Is there a style guide for Vagranfiles?

opticyclic
  • 7,412
  • 12
  • 81
  • 155

2 Answers2

6

Disclaimer: the following is opinionated (just my take on this). But so are all the style guides. Don't get caught into thinking too deeply about reasoning behind this, just make your choice and try to be consistent.

Is There A Style Guide Vagrantfiles?

Shortly: Use this style guide

Long answer: There does not seem to be any official Vagrant style guide, but since it is Ruby programming language, we can look at an official Ruby style guide... Oh, wait, there is actually none for Ruby either! This one is probably the most popular, so stick with it:

rubocop-hq/ruby-style-guide

What should I use: single quotes ('word'), double quotes ("word") or symbols (:word)?

Shortly: Use double quotes.

Long answer: Style guide is ambivalent on a single versus double quote matter: it offers you choice A and B, so stick with Vagrant documentation:

  • Use double quotes by default:

    node.vm.provision "shell", inline: "echo hello from node #{i}"

  • Use single quotes in case double quotes are parts of a string. This snippet from Vagrant documentation is consistent with style guide:

    config.winssh.export_command_template = '$env:%ENV_KEY%="%ENV_VALUE%"'

What is the colon prefix syntax?

Shortly: Colon marks a symbol (see Ruby documentation).

This answer expand a bit more: What is the colon operator in Ruby?

When should I use symbols in Vagrantfile?

Shortly: (Virtually) never.

Long answer: Just stick with double quotes for consistency, unless you do hit mutability or performance problems while Vagrant parses your Vagrantfile. More on performance and mutability of Symbols and Strings in Ruby: The Difference Between Ruby Symbols and Strings.

Andrei Sinitson
  • 739
  • 6
  • 10
0

Vagrantfile is a ruby script so its obey to ruby rules.

one of the main advantage to using double quotes is when you need to have string interpolation

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139