customink-webops / hostsfile

Easily manage your hosts file with Chef
Apache License 2.0
155 stars 82 forks source link

If a hostfile_entry block fails, /etc/hosts is left in a bad state #73

Open synthesizerpatel opened 9 years ago

synthesizerpatel commented 9 years ago

I ran into this as I was putting together a kitchen test where I had created my nodes incorrectly. They had ipaddresses, but not fqdn (which I was using as the hostname argument).

This resulted in chef crashing out and the hosts file having a line with just an ip address without any hostname to the right of it.

I'll see if I can come up with a patch to fix this - I'm still learning chef/ruby. But the correct behavior for the file modification should be to (Using shell for clarity)

cp /etc/hosts to /etc/hosts.tmp

Make your modifications on hosts.tmp, and only if all the modification operations succeed would you then

mv hosts.tmp hosts

A mv operation on linux and osx is an atomic operation guaranteed(*) to succeed. Otherwise, this cookbook was exactly what I was looking for and a perfect resource. Thanks for sharing your work!