How to set/change host name using Chef?
Solution 1
Author's note
This answer is over 6 years old.
As @lamont points out chef now supports a hostname resources
Original answer
There is a community hostname cookbook.
Solution 2
NOTE: there is now a hostname resource in the chef-client itself that was based on the chef_hostname cookbook in this answer, which is what everyone should use
I just released an initial version of a chef_hostname cookbook:
https://supermarket.chef.io/cookbooks/chef_hostname
To use it declare that you depend on it in your metadata.rb:
depends "chef_hostname"
And then in you recipe code just use the hostname
resource that it provides to set the hostname:
hostname "foo.example.com"
Or set the hostname to the node.name:
hostname node.name
Or set to any attribute that you care you use:
hostname node["set_fqdn"]
This cookbook solves many outstanding issues. Including all the Issues and PRs that are open against the hostname cookbook:
- supports fedora correctly
- supports centos7 and systemd systems that use hostnamectl
- does correct idempotent line-editing of files like /etc/hosts and /etc/sysctl.conf
- node['fqdn'] works correctly after the hostname is set
- is considerably more portable already, and I'll be adding more operating systems later
- defaults to node['ipaddress'] for /etc/hosts, allows tweaking
- allows for disabling /etc/hosts editing completely
Solution 3
Checkout my updated fork of the hostname cookbook that fixes a bug in hostname where the domain name gets appended twice to the FQDN.
Also, this fork allows you to set the ip to node["ipaddress"] instead of the default 127.0.1.1 or some other static ip.
https://github.com/nathantsoi/chef-cookbook-hostname
or
https://supermarket.getchef.com/cookbooks/hostnames
Gaurav Borole
iOS Application Developer and Manage Amazon web services. To contact me visit: https://www.linkedin.com/profile/view?id=69900317 https://gauravborole.wordpress.com/author/gauravborole/
Updated on June 20, 2022Comments
-
Gaurav Borole almost 2 years
I have few nodes in running mode, I have to set hostname to those nodes.
Is there any Cookbook, in that we can set attribute host_name and run that recipe on respective nodes?