Update time in template file.

1,494 views
Skip to first unread message

rjl

unread,
Dec 19, 2010, 4:16:34 PM12/19/10
to Puppet Users
Hi all,
How can I put in an update time in a template file? The problem I am
running it to is this...

Here is my file to update the timestamp

snmp.conf.erb

# HEADER: [ <%= Time.now.gmtime %> ] Modified by puppet.
defVersion 2c
devCommunity somecommunity


The problem is that everytime puppet runs, it updates the time stamp.
I only want it to update if there was an actual change to the content
of the file

Thanks in advace.

rjl

Daniel Pittman

unread,
Dec 19, 2010, 5:37:04 PM12/19/10
to puppet...@googlegroups.com
On Mon, Dec 20, 2010 at 08:16, rjl <rjli...@gmail.com> wrote:

> How can I put in an update time in a template file? The problem I am
> running it to is this... Here is my file to update the timestamp

[...]

> The problem is that everytime puppet runs, it updates the time stamp.
> I only want it to update if there was an actual change to the content
> of the file

It would: there is no way for puppet to know that the changed content
of the template header is "unimportant", compared to other changes.
(Given this is an ERB template, there is no possible way to ever
determine that, unfortunately, and we can't hook template generation
well enough either.)

Your only solution would be to use some detail that did not ever
change: personally, I used the "Time Stamp" feature that Emacs offers
to statically update the timestamp when the file was saved, rather
than puppet.

If you wanted something more automatic I would probably look to VCS
keyword expansion, or to using the mtime of the template on the server
as the date, rather than the time that puppet expanded it.

Regards,
Daniel
--
✣ Daniel Pittman            ✉ dan...@rimspace.net            ☎ +61 401 155 707
              ♽ made with 100 percent post-consumer electrons

Stefan Schulte

unread,
Dec 19, 2010, 5:41:01 PM12/19/10
to Puppet Users
Hi

And how whould puppet know what you consider "an actual change". You
specified that the actual systemtime should be in the first line. Since
it's not true on your client, puppet will change the file (cause
checksums are different).

Or to put it this way: Imagine puppet distributed the file with an actual
timestamp. Someone now opens the file and change the timestamp in your
header. What do you expect puppet to do now?

If you really depend on your headerline consider the following: Don't
use Time.now but use something like
File.stat('/etc/puppet/modules/snmp/template/snmp.conf.erb').mtime.gmtime
[Maybe theres a way to address the filename from the template itself,
but I havent figured that out; __FILE__ and ARGV are not working]

This will be the same in every puppet run (except you changed the
template). But if you assign your class to a new node, the header will
still reflact the time when you changed the file on your master, and not
when puppet installed that file on your node.

-Stefan

Felix Frank

unread,
Dec 20, 2010, 7:03:51 AM12/20/10
to puppet...@googlegroups.com

Hi,

a template probably won't do this for you. Whenever puppet generates
your client's catalog, the template text is put into that. So if the
master generates the header, it will produce a different catalog each time.

The most simple thing would be for you to rely on the mtime instead.
This has some drawbacks, of course.

If the template is simple enough (and, unlike me, you know some ruby),
it may be feasible to write a simple plugin instead. That way, you *can*
limit header generation to times when there are actual changes.

HTH,
Felix

rjl

unread,
Dec 20, 2010, 10:58:22 AM12/20/10
to Puppet Users
Thanks to everyone for all of your input. It was very helpful. I like
Felix's suggestion about writing a plugin.

rjl

On Dec 20, 5:03 am, Felix Frank <felix.fr...@alumni.tu-berlin.de>
wrote:

Alan Barrett

unread,
Dec 22, 2010, 1:55:21 AM12/22/10
to Puppet Users
On Sun, 19 Dec 2010, Stefan Schulte wrote:
> If you really depend on your headerline consider the following: Don't
> use Time.now but use something like
> File.stat('/etc/puppet/modules/snmp/template/snmp.conf.erb').mtime.gmtime
> [Maybe theres a way to address the filename from the template itself,
> but I havent figured that out; __FILE__ and ARGV are not working]

Feature request #4359 asks for a way that templates can get their own
name. It's not possible at the moment.

--apb (Alan Barrett)

Stefan Schulte

unread,
Dec 22, 2010, 6:06:32 AM12/22/10
to Puppet Users

I just had a look in parser/templatewrapper.rb and it turns out that we
can use a file accessor. At least the following template worked for me:

I am <%= file %>

The content of the file was then:
I am /etc/puppet/modules/test/templates/test.erb

I also updated your feature request.

-Stefan

Reply all
Reply to author
Forward
0 new messages