Setting host name with chef

I’m trying to set the hostname with chef. I’m using the hostname cookbook.
Everything looks to be configured fine. However, even though the hostname
has been set correctly, a log statement shows the hostname is still the
automatic EC2 assigned one. Running ohai after the chef-client run is
complete also shows the hostname to be the automatic EC2 one. Again, the
configuration of the files are correct, and a hostname -f shows the correct
value. Running chef a second time, the log statement shows the correct
value. Obviously the results of setting the hostname aren’t available until
the next run. How can I fix this?


I think that is just the way how chef works, when it comes to gathering attributes. So the before every chef client run, it gathers the Ohio details from the node and the attributes from the cookbooks. This happens on every run, hence the right things or should I say the new hostname shows up in the second run.

Wouldn’t reloading ohai work for this use case?

ohai 'reload' do
  action :reload


It didn’t help. Tried that. So, at this point, I can’t test our main java
application with test kitchen on ec2. Kitchen vagrant lets me set a
hostname, kitchen ec2 does not.


Is there not a delay between a hostname change and the update of ec2 metadatas ? (just a guess)

This is with test kitchen, i.e. chef-solo, so does that apply?

I ran into some issues using the hostname cookbook & EC2 as well:

I ended up switching to the system cookbook: & using lazy evaluation of node[‘fqdn’] in my recipes, as noted in the readme for the system cookbook. Maybe that can help you?


Well, I tried the system cookbook, and got:

Starting Chef Client, version 11.12.4
       Compiling Cookbooks...
       Converging 1 resources
       Recipe: slice-hostname::default
         * system_hostname[] action set

       Error executing action `set` on resource



Oh look… the mailing list is still truncating my posts… sigh.

Try posting it to github as a gist instead and then paste the link