Ask Your Question

Coyping File only if content is changed

asked 2017-11-16 01:06:06 -0600

Anu Thakur gravatar image

updated 2017-11-16 01:06:41 -0600


i have requirement where i have to create a file but next time when my puppet cycle runs it should check that if there is any change in the file only then copy it with the timestamp. I tried many possible ways but did not find anything to compare the contents of the file.

Is there any way possible to do that in puppet?

Best Regards, Anu Thakur

edit retag flag offensive close merge delete


Hi Anu, Unless I didn't understand your request, you are willing to replace the file if it's different from what you declared in Puppet (Fix any configuration drift...). If that is the case, then this is exactly the core of what Puppet is doing. Please clarify...

stivesso gravatar imagestivesso ( 2017-11-16 01:56:49 -0600 )edit

I need the file changed to be copied on the destination with the timestamp and keep the original also there. Say, i have abc.txt with content "qwertyuiop" now that the content has been changed to "asdfghjkl" of abc.txt ,

Anu Thakur gravatar imageAnu Thakur ( 2017-11-16 05:20:58 -0600 )edit

i want 2 files in the destination folder: abc.txt with "qwertyuiop" and abc.txt<timestamp> with "asdfghjkl". But if the contents are not changed as compared to the latest file, it should not copy the file in the destination folder.

Anu Thakur gravatar imageAnu Thakur ( 2017-11-16 05:22:53 -0600 )edit

What stivesso is saying is that puppet is managing the state of files that it says should be there... so if you put a file in a puppet module, then puppet will manage it with that state exactly. What it sounds like you want is some sort of 'backup management' when someone external to puppet ...

DarylW gravatar imageDarylW ( 2017-11-16 08:22:08 -0600 )edit

... modifies a file. In that case, you should probably look to some external, stand alone solution to perform that activity, and manage that with puppet. I have previously found that trying to make puppet 'create backups' every half hour can be done, but it is very hacky and not pretty. In most..

DarylW gravatar imageDarylW ( 2017-11-16 08:23:02 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-01-11 13:23:51 -0600

gabe_sky gravatar image

updated 2018-01-11 13:26:25 -0600

A file resource can take a "backup" attribute, that tells Puppet to save a copy of the file before overwriting. If you give it a string, it will append that to the filename to make a backup. You could do a (sort of kludgey) thing like this:

$time_utc = generate('/usr/bin/date','+%s')

file { '/tmp/myfile':
  ensure => file,
  content => "something\n",
  backup => ".${time_utc}.bak",
edit flag offensive delete link more

answered 2017-11-20 12:20:53 -0600

i think the optimal way to do this would be for the file to be under git - then you have a history of the file - just allow puppet to put the file you want in place rather than getting it to do backups.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-11-16 01:06:06 -0600

Seen: 364 times

Last updated: Jan 11