0

I am trying to learn how to use puppet, following along with their Quick Start Guide.

I would like to add a time stamp using generate:

$timestamp = generate('/bin/date')

class helloworld::motd {
    file { '/etc/motd':
    owner => 'root',
    group => 'root',
    mode => '0644',
    content => "Production puppetmaster is in control. Last run: ${timestamp}\n",
    }
}

But the resulting motd file has no date/time:

Production puppetmaster is in control. Last run:

What am I doing wrong?

2
  • 2
    Try moving the $timestamp assignment into the class block.
    – jordanm
    Sep 11 '16 at 19:25
  • Still blank output. Sep 11 '16 at 20:04
4

I wouldn't use generate() at all, that is something to be avoided at all costs.

Instead, take a look at the stdlib Puppet module, and the following functions in particular:

strftime()
time()

Example: The following function call would result in a timestamp in the YYYY-MM-DD HH:MM:SS format:

$timestamp = stftime("%Y-%m-%d %k:%M:%S") 
0

If you insist on using the built-in function generate() from the puppet language you can use it this way:

$timestamp = generate('/bin/date', '+%Y%d%m %H:%M:%S')

A more elaborate example is:

$myuptime = generate("/bin/sh", "-c", "/usr/bin/uptime | /usr/bin/awk '{ print \$3}' | cut -d, -f 1 ")

Please note the use of the quotes and the escaped $-sign.
I'm using the name $myuptime for my variable because $uptime is also available as a builtin.
Tested on puppet version 6.4.2 on Ubuntu 16.04.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.