Weird provision order vagrant

Can someone PLEASE help me with this, I've been stuck for a few hours now and googling doesn't help :p

I have 3 provisioners. The first one (ansible) should be executed on all servers. The second one (shell) should be executed on only one server after all of them are up. The 3rd one (also ansible) should only be executed once after the second one is finished.

Vagrant keeps doing random things and the if statement also just doesn't work...

# Create 3 machines, 1 masters and 1 workers
N = 3..1
(N.first).downto(N.last).each do |server_id|
config.vm.define "server#{server_id}" do |server|
server.vm.host_name = "server#{server_id}" # Hostname
server.vm.network "private_network", ip: "10.0.0.1#{server_id}"
server.vm.provider :virtualbox do |vb|
vb.name = "server#{server_id}"
end

# INSTALL THE REQUIRED DEPENDENCIES FOR THE CLUSTER ON EACH MACHINE
server.vm.provision "dependencies", type: "ansible" do |ansible|
ansible.playbook = "./provisioning/playbook.yml"
ansible.groups = {
"all" => ["server1", "server2", "server3"],
}
ansible.tags = "prepare"
end

# ONLY EXECUTE THIS PART AFTER THE LAST SERVER IS CREATED
if server_id == 1
# CREATE THE KUBERNETES CLUSTER USING KUBEKEY
config.vm.provision "cluster_creation", after: "dependencies", type: "host_shell", inline: <<-SHELL yes yes | sudo ./kubekey/kk create cluster -f kubekey/config.yaml SHELL # DEPLOY EVERYTHING ON THE KUBERNETES CLUSTER (metallb, argocd, github actions...) config.vm.provision "kubernetes_provisioning", after: "cluster_creation" type: "ansible" do |ansible| ansible.playbook = "./provisioning/playbook.yml" ansible.groups = { "provisioner" => ["server1"] # the machine that is used for provisioning
}
ansible.tags = "provision"
end
end
end
end

1 thought on “Weird provision order vagrant”

Leave a Comment