template "/etc/hosts" do source "hosts.erb" owner "root" group "root" mode 0644 end
#hosts.erb <% if @node[:hosts][:entries] %> <% @node[:hosts][:entries].each do |ip, name| %> <%= ip %> <%= name %> <% end %> <% end %>
In which we use fill data in form of hash structure with key as ip and value as hostname to the template.
The caveat here is that by using hash data structure we can potentially get different /etc/hosts each time we run chef client even though there is no change of data. This is due to no order guarantee of elements in hash data structure.
It does matter or not depending on the context. In case of /etc/hosts the order of host in that file may not be important. But a change notification triggers other action like start/stop other services, it can create un-wanted outcome. In addition there is some sort of compliance file monitoring tool like tripwire in place, it may create annoying alarms.
Anyway we can simply avoid it by create more consistent template in case of /etc/hosts, rewrite the template as following
#hosts.erb <% if @node[:hosts][:entries] %> <% @node[:hosts][:entries].keys.sort.each do |ip| %> <% name = @node[:hosts][:entries][ip] %> <%= ip %> <%= name %> <% end %> <% end %>
No comments:
Post a Comment