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?

mzhaase
  • 3,798
  • 2
  • 20
  • 32
43Tesseracts
  • 125
  • 1
  • 6

2 Answers2

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.

Cie6ohpa
  • 231
  • 1
  • 6