» Multi-Machine

    These machines are generally able to work together or are somehow associatedwith each other. Here are some use-cases people are using multi-machineenvironments for today:

    • Modeling a distributed system and how they interact with each other.
    • Testing an interface, such as an API to a service component.

    Using the multi-machine feature of Vagrant, these environments can be modeledin the context of a single Vagrant environment without losing any of thebenefits of Vagrant.

    Multiple machines are defined within the same project using the method call. This configuration directiveis a little funny, because it creates a Vagrant configuration within aconfiguration. An example shows this best:

    As you can see, config.vm.define takes a block with another variable. Thisvariable, such as web above, is the exact same as the config variable,except any configuration of the inner variable applies only to the machinebeing defined. Therefore, any configuration on web will only affect theweb machine.

    And importantly, you can continue to use the config object as well. Theconfiguration object is loaded and merged before the machine-specific configuration,just like other Vagrantfiles within theVagrantfile load order.

    When using these scopes, order of execution for things such asprovisioners becomes important. Vagrant enforces ordering outside-in, inthe order listed in the Vagrantfile. For example, with the Vagrantfilebelow:

    The provisioners in this case will output "A", then "C", then "B". Noticethat "B" is last. That is because the ordering is outside-in, inthe order of the file.

    If you want to apply a slightly different configuration to multiple machines,see .

    The moment more than one machine is defined within a Vagrantfile, theusage of the various vagrant commands changes slightly. The change shouldbe mostly intuitive.

    Commands that only make sense to target a single machine, such as, now require the name of the machine to control. Usingthe example above, you would say vagrant ssh web or vagrant ssh db.

    Additionally, you can specify a regular expression for matching onlycertain machines. This is useful in some cases where you specify many similarmachines, for example if you are testing a distributed service you may havea leader machine as well as a , follower1, follower2, etc. If youwant to bring up all the followers but not the leader, you can just dovagrant up /follower[0-9]/. If Vagrant sees a machine name within forwardslashes, it assumes you are using a regular expression.

    In order to facilitate communication within machines in a multi-machine setup,the various networking options should be used.In particular, the canbe used to make a private network between multiple machines and the host.

    You can also specify a primary machine. The primary machine will be thedefault machine used when a specific machine in a multi-machine environmentis not specified.

    To specify a default machine, just mark it primary when defining it. Onlyone primary machine may be specified.

    By default in a multi-machine environment, vagrant up will startall of the defined machines. The autostart setting allows you to tellVagrant to not start specific machines. Example: