Dear community,
I'm looking for a way to run a single trigger for a single command i(i.e. "vagrant up") inside a multi-machine environment to play with Ansible. I've defined a trigger to download a pubkey that will be used in Dockerfile, i.e. when "vagrant up" command "issues a "docker build" command under the hood, but instead trigger runs for every defined machine. My shortened Vagrantfile looks like this:
Vagrant.configure("2") do |config|
# Check and download vagrant pubkey to include in the Docker images
config.trigger.before :up do |trigger|
trigger.name = "Prepare insecure Vagrant SSH public key"
trigger.ruby do |env, machine|
vagrant_pubkey_url = "
https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub"
vagrant_pubkey_name = ".vagrant/vagrant.pub"
if (not File.file?(vagrant_pubkey_name))
trigger.info = "Vagrant's insecure SSH public key is not present. Downloading..."
open(vagrant_pubkey_url) do |pubkey|
File.open(vagrant_pubkey_name, "wb") do |pubkey_file|
pubkey_file.write(pubkey.read)
trigger.info = "Download complete. Saved key to #{vagrant_pubkey_name}"
end
end
end
end
end
kinds = ["app", "db", "www"]
images = [{distribution: "debian", dockerfile: "debian10-systemd.Dockerfile"}]
kinds.each do |server_type|
(1..images.length).zip(images).each do |index, image|
node_name = "#{server_type}#{index}-#{image[:distribution]}"
config.vm.define "#{node_name}" do |node|
node.vm.network :private_network, type: "dhcp"
node.vm.provider "docker" do |docker|
docker.name = node_name
docker.build_dir = "."
docker.has_ssh = true
docker.dockerfile = "dockerfiles/#{image[:dockerfile]}"
docker.create_args = ["--rm", "--privileged", "-v", "/sys/fs/cgroup:/sys/fs/cgroup:ro"]
end
end
end
end
end
In this configuration we should get 3 containers named "app1-debian", "db1-debian", "www1-debian".
I expected my trigger would run only once since it is defined outside of node creation loop. But when I run "vagrant up", trigger is fired 3 times. Moreover even though there is some code that checks file existence, it is downloaded 3 times as well. Next time I run "vagrant up", the file won't be downloaded, as if the code is run once on initial Vagrantfile read.
I'd like to use similar global trigger to run Ansible playbook to check network connectivity between all hosts but currently I get it run after each container is up and not after all containers are up that leads to run checks multiple times with different number of hosts (1, 2 and finally, 3 hosts).