Surely I’m not a Ruby expert, but it seems triggers are driving me crazy. Apparently they seem to be a simple concept, still I can’t make them work.
I have a Vagrantfile for configuring a multi-machine setup, like this:
Vagrant.configure(2) do |config|
# Config parameters for the environments
number_of_nodes = 6
cpus_per_node = 4
mem_per_node = 4096
master_node_name = "portainer"
worker_node_name = "pworker"
# Provision the nodes.
(1..number_of_nodes).each do |i|
# First node is the master
if i == 1 then
config.vm.define "#{master_node_name}", primary: true do |master|
master.vm.box = "almalinux/9"
master.vm.hostname = 'master'
master.vm.provider "hyperv" do |hv|
hv.enable_virtualization_extensions = true
hv.linked_clone = true
hv.vmname = "Portainer"
hv.cpus = cpus_per_node
hv.memory = mem_per_node
end
# Define a reload trigger.
master.trigger.before :reload do |trigger|
trigger.info = "Setting Hyper-V switch to 'LabSwitch' to allow for static IP..."
trigger.run = {
privileged: "true", powershell_elevated_interactive: "true", path: "./scripts/powershell/set-hyperv-switch.ps1", args: "Portainer"
}
end
# Provision some stuff.
# Reload the machine.
master.vm.provision :reload
end
else
config.vm.define "#{worker_node_name}#{i - 1}" do |worker|
worker.vm.box = "almalinux/9"
worker.vm.hostname = "worker#{i - 1}"
worker.vm.provider "hyperv" do |hv|
hv.enable_virtualization_extensions = true
hv.linked_clone = true
hv.vmname = "Portainer Worker #{i - 1}"
hv.cpus = cpus_per_node
hv.memory = mem_per_node
end
# Define a reload trigger.
worker.trigger.before :reload do |trigger|
trigger.info = "Setting Hyper-V switch to 'LabSwitch' to allow for static IP..."
trigger.run = {
privileged: "true", powershell_elevated_interactive: "true", path: "./scripts/powershell/set-hyperv-switch.ps1", args: "Portainer Worker #{i - 1}"
}
end
# Reload the machine.
worker.vm.provision :reload
end
end
end
end
As you can see, there are two “guest scoped” triggers: thery’re the same, the only thing that changes is the parameter that the trigger takes. In the first machine, the trigger works fine.
In the second machine (the else) when the directive worker.vm.provision :reload is fired, the executed trigger seems to be the first one, and not the second.
Any help / idea on why this is happening?