How to compare files in recipe


#1

I’m programming a recipe in chef. I want to guarantee a configuration file on the target node is consistent with my configuration file template. I have the template erb file. I currently use “template” resource to replace the target configuration file with my template erb file.
The problem is that chef client is pulled up every 30 minutes, which means the configuration file is replaced with the same file every 30 mins. Is there any better way to do that? I need a clause to compare the file with the template, so that I do not need to replace the file, if it is the same with the template.


#2

Chef already only updates the file when there is a need to do so. This is referred as “idempotent updates”, and we do it as much as possible on all resources. You should see the template marked as “up to date” in the client output.


#3

Are you sure you’re not changing the file, maybe a timestamp or something changing each chef-client run? The template resource compares your existing file with the generated template and only replaces it if it is different.


#4

Thanks. Just checked the timestamp. It’s true that chef only replace when the content get changed.