Gaurav Borole
Gaurav Borole

Reputation: 846

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?

Upvotes: 5

Views: 12245

Answers (3)

Mark O'Connor
Mark O'Connor

Reputation: 77961

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.

Upvotes: 5

lamont
lamont

Reputation: 3974

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

Upvotes: 2

Nathan
Nathan

Reputation: 33

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

Upvotes: 1

Related Questions