puppet 在文件内容中添加时间戳

joh*_*now 5 puppet

木偶的超级新手。找不到如何在文件中添加时间戳的好例子puppet.pp

node '123' {
  file { '/tmp/hello':
    content => "hello world",
  }

  file { '/tmp/timestamped':
    content => 'date',
  }
Run Code Online (Sandbox Code Playgroud)

只是想在将此清单应用于带时间戳的文件打印当前日期

版本是:4.10

c4f*_*t0r 6

木偶版本 < 4.8.0

如果您使用的是4.8.0 之前的版本,可以strftime()在 stdlib 模块中使用函数(http://www.puppetmodule.info/github/simp/puppetlabs-stdlib/puppet_functions_ruby3x/strftime

人偶版本 > 4.8.0

如果您使用的是较新版本的 puppet,则应使用Timestamp.new().strftime()( https://puppet.com/docs/puppet/latest/function.html#strftime )

示例(您只需要使用其中一项作业):

#ISO 8601
$timestamp = Timestamp.new().strftime('%Y-%m-%dT%H:%M:%S%:z')
notice ($timestamp)

#RFC 822, 1036, 1124, 2822
$timestamp = Timestamp.new().strftime('%a, %d %b %Y %H:%M:%S %z') 
notice ($timestamp)

file {'/tmp/timestamped':
   content => "$timestamp"
}
Run Code Online (Sandbox Code Playgroud)


Sir*_*rch 5

这应该有效。使用generate 创建并分配给变量。然后将变量分配为文件的内容。

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

file {'/tmp/timestamped':
   content => "$timestamp"
}
Run Code Online (Sandbox Code Playgroud)