How to get a machine/node name from within a define block?

19 views
Skip to first unread message

Rui Lopes

unread,
Jul 26, 2017, 12:41:55 PM7/26/17
to Vagrant
Is there a way to get server1 and server2 from within a config.vm.define block?

For example, how can I do it in the following Vagrantfile?

config.vm.define "server1" do |config|
  puts TODO-what-variable-will-get-me-server1?
end

config.vm.define "server2" do |config|
  puts TODO-what-variable-will-get-me-server1?
end

Do I really need to use a temporary variable? e.g. something like:

node1_name = 'server1'
config.vm.define node1_name do |config|
  puts node1_name
end

node2_name = 'server2'
config.vm.define node2_name do |config|
  puts node2_name
end

Thanks!

Alvaro Miranda Aguilera

unread,
Jul 27, 2017, 12:49:27 AM7/27/17
to vagra...@googlegroups.com
Hello

This works for me:


Vagrant.configure("2") do |config|

  config.vm.box = "hashicorp/precise64"


  (1..2).each do |i|

    config.vm.define vm_name = "dc1-web0%01d" % i do |web|

      puts vm_name

      web.vm.hostname = "#{vm_name}"

    end

  end

end



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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/43ee4ed4-0c2e-407b-ad1f-be29fe37ee0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Rui Lopes

unread,
Jul 27, 2017, 7:58:43 AM7/27/17
to Vagrant
Hello, oh so we really need to use a temporary variable? I was looking for some other solution, like having the name somewhere at the config object :-/
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages