I want to write a chef recipe that will set the hostname of the EC2 instance as :
AWSRegionInstanceTyperandomNo
like awsedev001
how can I do this in a chef recipe? Do we have any recipe for that ?
Please help
How would you do this in regular Unix land?
You would first add the full FQDN to /etc/hostname
for persistence:
file '/etc/hostname' do
content 'hostname.example.com'
end
Then you would set the hostname for the currently running os:
execute 'hostname hostname.example.com' do
not_if 'hostname -eq "hostname.example.com"'
end
This is a simple Chef recipe example, but it can be made more complex to suit your needs. You may also want to look at the hostname cookbook on the Chef community site .
In AWS in order to change the hostname you need to edit
/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=ip-172-16-1-23.ec2.internal
NOZEROCONF=yes
After editing the second line you can reboot and you are set.
You can use the https://supermarket.chef.io/cookbooks/chef_hostname cookbook off of the community site.
In your cookbook metadata.rb just declare:
depends "chef_hostname"
And then in your recipe code use the hostname resource to set the hostname:
hostname "foo.example.com"
Or:
hostname node.name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.