How set machine as primary in dynamic Multi-Machine scenario?

80 views
Skip to first unread message

Joaquin Menchaca

unread,
Apr 24, 2016, 3:53:51 AM4/24/16
to Vagrant
The docs say, for making one machine a primary, you can do:

config.vm.define "web", primary: true do |web|
  # ...
end

How do you do it, if you bring these up dynamically, such as a hosts file? 

nodes = Hash.new
File.readlines('config/global.hosts').map(&:chomp).each do |line|
  ipaddr, nodename = line.split(" ")
  nodes[nodename] = ipaddr
end

Vagrant.configure("2") do |config|
  nodes.each do |nodename, ipaddr|
    config.vm.define nodename do |node|
      node.vm.box = "ubuntu/trusty64"
      node.vm.hostname = "#{nodename}"
      node.vm.network "private_network", ip: ipaddr
      node.vm.provider("virtualbox") { |vbox| vbox.name = "#{nodename}" }
      node.vm.provision "shell", path: "scripts/#{nodename}.sh"
    end
  end
end 



Alvaro Miranda Aguilera

unread,
Apr 24, 2016, 6:42:56 AM4/24/16
to vagra...@googlegroups.com
Hello,

Vagrant bxes are created top to bottom,

So the first VM created can be used to copy/create files to the other boxes.

You could create files with an script, or have a file in the current directory and that will be available in /vagrant/ directory

so you could use that to distribute a file.

Thanks
Alvaro.

--
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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/cc5db890-e913-42e8-bbaf-fa5ff32f3de6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joaquin Menchaca

unread,
Apr 24, 2016, 11:56:20 AM4/24/16
to Vagrant
I am not sure I understand.  How would this make it so I type vagrant ssh, it does to a designated primary box?  

I have to specify at initialization the primary symbol to true, so I am asking if there's a way to set it to primary after declaration, or another way to do it dynamically, conditionally set the symbol to true.

Joaquin Menchaca

unread,
Apr 25, 2016, 2:54:27 PM4/25/16
to Vagrant
I found a solution.  I will essentially have a Ruby hash (created from json, yaml, xml, or other format), that I feed into Vagrant.  I toggle a default value that I set for primary.


Vagrant.configure("2") do |config|¬
··settings['hosts'].each do |hostname, ipaddr|¬
····default = settings['defaults'][hostname] || false¬
····config.vm.define hostname, primary: default do |node|¬
······node.vm.box = "ubuntu/trusty64"¬
······node.vm.hostname = "#{hostname}"¬
······node.vm.network "private_network", ip: ipaddr¬
······node.vm.provider("virtualbox") { |vbox| vbox.name = "#{hostname}_#{time}" }¬
······if settings['ports'][hostname]¬
········settings['ports'][hostname].each do |forward|¬
··········node.vm.network "forwarded_port", guest: forward['guest'], host: forward['host']¬
········end¬
······end¬
······# Provision¬
······node.vm.provision "shell", path: "scripts/#{hostname}.sh"¬
····end¬
··end¬
end




Message has been deleted

Joaquin Menchaca

unread,
Apr 26, 2016, 3:31:27 PM4/26/16
to Vagrant
Also, I did this as well

Vagrant.configure("2") do |config|
  ...
  nodes.each do |nodename, ipaddr|
    default = if nodename == "mysystem" then true else false end
    config.vm.define nodename, primary: default do |node|
    ...
    end
end
Reply all
Reply to author
Forward
0 new messages