Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set/change host name using Chef?

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?

like image 818
Gaurav Borole Avatar asked Aug 28 '13 09:08

Gaurav Borole


3 Answers

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.

like image 152
Mark O'Connor Avatar answered Nov 15 '22 05:11

Mark O'Connor


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
like image 40
lamont Avatar answered Nov 15 '22 06:11

lamont


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

like image 24
Nathan Avatar answered Nov 15 '22 06:11

Nathan