Reputation: 445
If I try vagrant up secondmachine
against this Vagrantfile below, Vagrant will only create one Ubuntu VM named "secondmachine". However, Vagrant will still try to run the "firstmachine" script on it.
How can I convince Vagrant to only provision machines it's actually brought up, and/or that have been specified on the command-line? Is there a right way and a wrong way to loop through multiple machine definitions?
# coding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
nodes = [
{
:name => "firstmachine",
:autostart => false,
:box => "centos/7",
:provisioners => [
{
:script => "cat /etc/redhat-release",
},
],
},
{
:name => "secondmachine",
:autostart => false,
:box => "ubuntu/trusty64",
:provisioners => [
{
:script => "apt-get update",
},
],
},
]
Vagrant.require_version ">= 2.0.2"
Vagrant.configure("2") do |config|
nodes.each do |node|
# Will ignore any node without a name!
next if node[:name].empty?
config.vm.define node[:name], autostart: node[:autostart] do |machine|
machine.vm.box = node[:box]
node[:provisioners].each do |provisioner|
config.vm.provision
type: "shell",
privileged: privileged,
inline: provisioner[:script]
end
end
end
end
Upvotes: 0
Views: 908
Reputation: 445
The mistake is that the provisioner is assigned to the config object, not the subsequent machine instances. In short, changing config.vm.provision
to machine.vm.provision
clears up the trouble.
Upvotes: 1