host-specific provisioners for multi-vm Vagrant file

I'm trying to cook up a multi-VM Vagrantfile in which each system first runs a common provisioner, and then runs its own host-specific second provisioner script.

```
hosts = {
"server" => { :ip => "192.168.56.101", :common => "common.sh", :secondary => "server.sh" },
"client1" => { :ip => "192.168.56.102", :common => "common.sh", :secondary => "client1.sh" },
"client2" => { :ip => "192.168.56.103", :common => "common.sh", :secondary => "client2.sh" },
}

Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
hosts.each_with_index do |(hostname,info), index|

config.vm.define hostname do |cfg|
cfg.vm.provider :virtualbox do |vb, override|
override.vm.hostname = hostname
override.vm.network "private_network", ip: info[:ip]
end

config.vm.provision "shell", inline: "echo COMMON SCRIPT = #{info[:common]}"
config.vm.provision "shell", inline: "echo SECONDARY SCRIPT = #{info[:secondary]}"

end
end

```

Expected results are that each system will come up with its defined hostname and ip address (works) and in the provisioning process run two scripts - the common one defined for it, and the host-specific secondary script defined for it (doesn't work).

I'm getting different results:
* server runs common.sh and server.sh
* client1 runs the system above it runs, plus the two things defined for it
* client2 runs what two systems above it ran, plus the two things defined for it

Output snippets from 'vagrant up' look like:
```
==> server: Running provisioner: shell...
server: Running: inline script
server: COMMON SCRIPT = common.sh
==> server: Running provisioner: shell...
server: Running: inline script
server: SECONDARY SCRIPT = server.sh

==> client1: Running provisioner: shell...
client1: Running: inline script
client1: COMMON SCRIPT = common.sh
==> client1: Running provisioner: shell...
client1: Running: inline script
client1: SECONDARY SCRIPT = server.sh
==> client1: Running provisioner: shell...
client1: Running: inline script
client1: COMMON SCRIPT = common.sh
==> client1: Running provisioner: shell...
client1: Running: inline script
client1: SECONDARY SCRIPT = client1.sh

==> client2: Running provisioner: shell...
client2: Running: inline script
client2: COMMON SCRIPT = common.sh
==> client2: Running provisioner: shell...
client2: Running: inline script
client2: SECONDARY SCRIPT = server.sh
==> client2: Running provisioner: shell...
client2: Running: inline script
client2: COMMON SCRIPT = common.sh
==> client2: Running provisioner: shell...
client2: Running: inline script
client2: SECONDARY SCRIPT = client1.sh
==> client2: Running provisioner: shell...
client2: Running: inline script
client2: COMMON SCRIPT = common.sh
==> client2: Running provisioner: shell...
client2: Running: inline script
client2: SECONDARY SCRIPT = client2.sh

```

I'm not sure if I have a logic bug in the attached or if it's a vagrant bug someplace. I'm running vagrant 2.2.3 on Windows10Pro if that matters. Ideas ?

1 thought on “host-specific provisioners for multi-vm Vagrant file”

  1. I would start with printing the info hash to see whether this is each_with_index() issue or config.vm.provision() issue.

    Reply

Leave a Comment