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).

Full configuration is available here: 
https://github.com/jollyroger/test-infra-vagrant-ansible

-- 
This mailing list is governed under the HashiCorp Community Guidelines - 
https://www.hashicorp.com/community-guidelines.html. Behavior in violation of 
those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
--- 
You received this message because you are subscribed to the Google Groups 
"Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vagrant-up+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vagrant-up/24d7ebd6-6b23-4474-b184-65e795e4f948%40googlegroups.com.

Reply via email to