How to generate random string at end of hostname?


#1

How to generate random string at end of hostname?

For every node we provision with knife ec2/openstack/etc… we want the hostnames to have a format of:

staticstring-staticstring-randomstring

Example: web-server-hk3dp99x

If the host reboots the name will not change.

For each newly provisioned node the random string will change.

Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


#2

It is highly recommended you use the raw hostnames as generated by EC2/Nova. Trying to change the hostname from Chef is tricky even at the best of times. If you really do want to change it, I would recommend doing it before Chef is bootstrapped in your own provisioning system (cloud-init on EC2, for example).

–Noah

On Jul 19, 2014, at 11:17 AM, Justin Franks justin.franks@lithium.com wrote:

How to generate random string at end of hostname?
For every node we provision with knife ec2/openstack/etc… we want the hostnames to have a format of:
staticstring-staticstring-randomstring
Example: web-server-hk3dp99x
If the host reboots the name will not change.
For each newly provisioned node the random string will change.
Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


#3

Hi Justin,

I created a simple golang project for this usecase:
https://github.com/optiflows/bapteme (with gobuild link on it)
And a cookbook to install it: https://github.com/optiflows-cookbooks/bapteme

Any comment is welcome as I don’t use it in production for the moment.

Guilhem Lettron

On Sat, Jul 19, 2014 at 8:29 PM, Noah Kantrowitz noah@coderanger.net
wrote:

It is highly recommended you use the raw hostnames as generated by
EC2/Nova. Trying to change the hostname from Chef is tricky even at the
best of times. If you really do want to change it, I would recommend doing
it before Chef is bootstrapped in your own provisioning system (cloud-init
on EC2, for example).

–Noah

On Jul 19, 2014, at 11:17 AM, Justin Franks justin.franks@lithium.com
wrote:

How to generate random string at end of hostname?
For every node we provision with knife ec2/openstack/etc… we want the
hostnames to have a format of:
staticstring-staticstring-randomstring
Example: web-server-hk3dp99x
If the host reboots the name will not change.
For each newly provisioned node the random string will change.
Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


#4

I’d agree with Noah, some platforms don’t provide names though. Here’s
the hacky generator used by knife-openstack and others through
knife-cloud:

Thanks,
Matt Ray
Director of Partner Integration :: Chef
512.731.2218 :: matt@getchef.com
mattray :: GitHub :: IRC :: Twitter

On Sat, Jul 19, 2014 at 1:29 PM, Noah Kantrowitz noah@coderanger.net wrote:

It is highly recommended you use the raw hostnames as generated by EC2/Nova. Trying to change the hostname from Chef is tricky even at the best of times. If you really do want to change it, I would recommend doing it before Chef is bootstrapped in your own provisioning system (cloud-init on EC2, for example).

–Noah

On Jul 19, 2014, at 11:17 AM, Justin Franks justin.franks@lithium.com wrote:

How to generate random string at end of hostname?
For every node we provision with knife ec2/openstack/etc… we want the hostnames to have a format of:
staticstring-staticstring-randomstring
Example: web-server-hk3dp99x
If the host reboots the name will not change.
For each newly provisioned node the random string will change.
Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


#5

ruby -e “require ‘securerandom’; puts SecureRandom.hex(3)”

Seth

On Jul 21, 2014, at 11:15 AM, Matt Ray matt@getchef.com wrote:

I’d agree with Noah, some platforms don’t provide names though. Here’s
the hacky generator used by knife-openstack and others through
knife-cloud:
https://github.com/opscode/knife-cloud/blob/master/lib/chef/knife/cloud/server/create_command.rb#L126

Thanks,
Matt Ray
Director of Partner Integration :: Chef
512.731.2218 :: matt@getchef.com
mattray :: GitHub :: IRC :: Twitter

On Sat, Jul 19, 2014 at 1:29 PM, Noah Kantrowitz noah@coderanger.net wrote:

It is highly recommended you use the raw hostnames as generated by EC2/Nova. Trying to change the hostname from Chef is tricky even at the best of times. If you really do want to change it, I would recommend doing it before Chef is bootstrapped in your own provisioning system (cloud-init on EC2, for example).

–Noah

On Jul 19, 2014, at 11:17 AM, Justin Franks justin.franks@lithium.com wrote:

How to generate random string at end of hostname?
For every node we provision with knife ec2/openstack/etc… we want the hostnames to have a format of:
staticstring-staticstring-randomstring
Example: web-server-hk3dp99x
If the host reboots the name will not change.
For each newly provisioned node the random string will change.
Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


#6

Seth,
Where does this Ruby go? Where do I “puts” it?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


From: Seth Vargo sethvargo@getchef.com
Sent: Monday, July 21, 2014 1:35 PM
To: chef@lists.opscode.com
Subject: [chef] Re: How to generate random string at end of hostname?

ruby -e “require ‘securerandom’; puts SecureRandom.hex(3)”

Seth

On Jul 21, 2014, at 11:15 AM, Matt Ray matt@getchef.com wrote:

I’d agree with Noah, some platforms don’t provide names though. Here’s
the hacky generator used by knife-openstack and others through
knife-cloud:
https://github.com/opscode/knife-cloud/blob/master/lib/chef/knife/cloud/server/create_command.rb#L126

Thanks,
Matt Ray
Director of Partner Integration :: Chef
512.731.2218 :: matt@getchef.com
mattray :: GitHub :: IRC :: Twitter

On Sat, Jul 19, 2014 at 1:29 PM, Noah Kantrowitz noah@coderanger.net wrote:

It is highly recommended you use the raw hostnames as generated by EC2/Nova. Trying to change the hostname from Chef is tricky even at the best of times. If you really do want to change it, I would recommend doing it before Chef is bootstrapped in your own provisioning system (cloud-init on EC2, for example).

–Noah

On Jul 19, 2014, at 11:17 AM, Justin Franks justin.franks@lithium.com wrote:

How to generate random string at end of hostname?
For every node we provision with knife ec2/openstack/etc… we want the hostnames to have a format of:
staticstring-staticstring-randomstring
Example: web-server-hk3dp99x
If the host reboots the name will not change.
For each newly provisioned node the random string will change.
Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


#7

Justin,

If you have a Ruby script that’s setting the hostname for you before Chef runs, you could just interpolate what Seth said into your hostname like:

#!/opt/chef/embedded/bin/ruby

set_hostname.rb

require ‘secure random’

hostname_template = ARGV[0]
hostname_id = SecureRandom.hex(3)
hostname = “#{hostname_template}-#{hostname_id}”

File.open(‘/etc/hosts’, ‘a’) do |file|
file.write "127.0.0.1 localhost #{hostname}"
end

File.open(‘/etc/hostname’, ‘w’) do |file|
file.write hostname
end

system(‘service hostname start’)

Cheers,

Tony

On Jul 21, 2014, at 5:17 PM, Justin Franks justin.franks@lithium.com wrote:

Seth,
Where does this Ruby go? Where do I “puts” it?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219


From: Seth Vargo sethvargo@getchef.com
Sent: Monday, July 21, 2014 1:35 PM
To: chef@lists.opscode.com
Subject: [chef] Re: How to generate random string at end of hostname?

ruby -e “require ‘securerandom’; puts SecureRandom.hex(3)”

Seth

On Jul 21, 2014, at 11:15 AM, Matt Ray matt@getchef.com wrote:

I’d agree with Noah, some platforms don’t provide names though. Here’s
the hacky generator used by knife-openstack and others through
knife-cloud:
https://github.com/opscode/knife-cloud/blob/master/lib/chef/knife/cloud/server/create_command.rb#L126

Thanks,
Matt Ray
Director of Partner Integration :: Chef
512.731.2218 :: matt@getchef.com
mattray :: GitHub :: IRC :: Twitter

On Sat, Jul 19, 2014 at 1:29 PM, Noah Kantrowitz noah@coderanger.net wrote:

It is highly recommended you use the raw hostnames as generated by EC2/Nova. Trying to change the hostname from Chef is tricky even at the best of times. If you really do want to change it, I would recommend doing it before Chef is bootstrapped in your own provisioning system (cloud-init on EC2, for example).

–Noah

On Jul 19, 2014, at 11:17 AM, Justin Franks justin.franks@lithium.com wrote:

How to generate random string at end of hostname?
For every node we provision with knife ec2/openstack/etc… we want the hostnames to have a format of:
staticstring-staticstring-randomstring
Example: web-server-hk3dp99x
If the host reboots the name will not change.
For each newly provisioned node the random string will change.
Suggestions on the best approach?


Justin Franks
Lead Operations Engineer
SaaS, Cloud, Data Centers & Infrastructure
Lithium Technologies, Inc
225 Bush St., 15th Floor
San Francisco, CA 94104
tel: +1 415 757 3100 x3219