Creating Dynamic hostname

359 views
Skip to first unread message

Abhishek Verma

unread,
May 2, 2017, 11:34:43 AM5/2/17
to Vagrant
Hi All,

I am trying to setup a Vagrant machine with dynamic hostname.

I can setup hostname using "config.vm.hostname = "DummyReefServer"" in VagrantFile and it works fine.
But I wish to make this hostname dynamic , if possibile based on macaddress so everytime its unique..

Is it possible to setup hostname dunamic possibly something like this

config.vm.hostname = "DummyReefServer"+ethernet0.address

Thanks in Advance

Regards,
Abhishek

Alvaro Miranda Aguilera

unread,
May 2, 2017, 3:32:55 PM5/2/17
to vagra...@googlegroups.com
Hello

Not sure about mac, but you can use:

SecureRandom.random_number(99999)


Adjust the number of digits.


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/270f87fb-6bf0-48c7-a676-25ce8d16e256%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Abhishek Verma

unread,
May 2, 2017, 5:33:16 PM5/2/17
to Vagrant
Hi Alvaro,
Thanks for your reply. I can now create a unique mac address using
config.vm.hostname = "dummy-ngs-#{SecureRandom.random_number(9999)}"

but every time I reboot my machine , hostname gets reset.
i want to create unique hostname for machine when I create it, till i destroy it.

Any suggestions?


On Tuesday, May 2, 2017 at 8:32:55 PM UTC+1, Alvaro Miranda Aguilera wrote:
Hello

Not sure about mac, but you can use:

SecureRandom.random_number(99999)


Adjust the number of digits.


Alvaro

On Tue, May 2, 2017 at 5:34 PM, Abhishek Verma <abhishe...@uptopoint.com> wrote:
Hi All,

I am trying to setup a Vagrant machine with dynamic hostname.

I can setup hostname using "config.vm.hostname = "DummyReefServer"" in VagrantFile and it works fine.
But I wish to make this hostname dynamic , if possibile based on macaddress so everytime its unique..

Is it possible to setup hostname dunamic possibly something like this

config.vm.hostname = "DummyReefServer"+ethernet0.address

Thanks in Advance

Regards,
Abhishek

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



--
Alvaro

Jamie Jackson

unread,
May 2, 2017, 7:03:50 PM5/2/17
to vagra...@googlegroups.com
Maybe you could write the value to a file on the host, somewhere in
the vagrant directory (only if the file doesn't exist already), then
read it in from the file. I do a lot of this kind of thing to create
idempotent provisioners.

On Tue, May 2, 2017 at 5:33 PM, Abhishek Verma
> https://groups.google.com/d/msgid/vagrant-up/735dac67-7500-4641-9e8c-5c3d7eb4b454%40googlegroups.com.

Alvaro Miranda Aguilera

unread,
May 3, 2017, 6:41:33 AM5/3/17
to vagra...@googlegroups.com
try something like this:

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

  File.open("hostname.txt", "w") {|f| f.write("dummy-ngs-#{SecureRandom.random_number(9999)}")} unless File.file?("hostname.txt")

  hostname = File.read("hostname.txt")


  config.vm.hostname = hostname

  config.vm.box = "hashicorp/precise64"

end

Abhishek Verma

unread,
May 6, 2017, 3:14:36 AM5/6/17
to vagra...@googlegroups.com
Hey Guys , 
Thanks alot for your help.
Finally I found many ways to do the same thing. 
1. As Alvaro suggested, write to a temp file, 
2. or use Script inside VagrantFile to write to temp file
3. Create a script inside vagrant machine which runs on bootup , and repackage vagrant machine 
4. use provisioning and create a shell script next to VagrantFile which runs on bootup and configures the machine.

I went with number 4. (because i had to use macaddress not random numbers).
Although I wanted to set it up inside VagrantFile so machine has hostname before bootup, but I couldnt find out way to get macaddress and create a hostname string in VagrantFile. (New to Vagrant and dont know Ruby.)

But never mind, i got what I want .

Thanks to you all again.

Regards,
Abhishek


--
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 a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/rXpN5TCCFUo/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAHqq0eybpdJxaazonwk5ZbQX8QrW%2BW-z5FDZH%3DJq5LWbXGMJSg%40mail.gmail.com.

Sukhdeep Singh

unread,
Oct 27, 2021, 11:49:59 AM10/27/21
to Vagrant
Hey Abhishek,

If you can share that shell script with me, by using which you were assigning dynamic hostname to Vagrant VM, than it will be really helpful.
I am eagerly waiting for a reply from you, and other members also who are part of this conversation.

Mobile- 7986186510

Regards,
Sukhdeep Singh

To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.

info vepsun

unread,
Dec 20, 2021, 6:19:43 AM12/20/21
to Vagrant
  • Increased Enterprise Cloud Migration to AWS. ...
  • Expertise in Cloud Computing Is Inevitable for IT Professionals. ...
  • AWS Is the Fastest Growing Public Cloud Service. ...
  • AWS Is the Most Widely Used Platform in Cloud Adoption. ...
  • Free-tier Access and Affordable Pricing.
  • WWW.VEPSUN.IN
Reply all
Reply to author
Forward
0 new messages