选中并在puppet中添加多行

选中并在puppet中添加多行,puppet,Puppet,我需要通过puppet确保文件/etc/logrotate.conf必须包含该条目 /var/log/secure { monthly rotate 11 } 我试过了 $line_string = "/var/log/secure { monthly rotate 11 }" file_line {'ensure correct entry in /etc/logrotate.conf': path => '/etc/logrotate.conf

我需要通过puppet确保文件
/etc/logrotate.conf
必须包含该条目

/var/log/secure {
    monthly
    rotate 11
}
我试过了

$line_string = "/var/log/secure {
    monthly
    rotate 11
}"

file_line {'ensure correct entry in /etc/logrotate.conf':
  path  => '/etc/logrotate.conf',
  line  => $line_string,
  match => $line_string,
}
它第一次创建条目,但当我第二次应用puppet代码时,它会再次添加条目

[~] # puppet apply /home/vijay/logrot.pp
Notice: Compiled catalog for lxv9824 in environment production in 0.10 seconds
/var/lib/puppet/lib/puppet/provider/file_line/ruby.rb:36: warning: regexp has invalid interval
/var/lib/puppet/lib/puppet/provider/file_line/ruby.rb:36: warning: regexp has `}' without escape
Notice: /Stage[main]/Main/File_line[ensure correct entry in /etc/logrotate.conf]/ensure: created
Notice: Finished catalog run in 0.06 seconds

[~] # more /etc/logrotate.conf
/var/log/secure {
    monthly
    rotate 11
}
/var/log/secure {
    monthly
    rotate 11
}

如何防止puppet再次添加条目?

属性的参数中有新行时,来自
puppetlabs stdlib
文件行
资源不是幂等的。您可以使用
match
属性来解决此问题,但您提供的regexp无效(请注意
file\u line.rb
中的警告)

由于
/etc/logrotate.conf
似乎只包含该条目,因此您可以执行以下操作:

file { '/etc/logrotate.conf':
  ensure  => file,
  content => $line_string,
}
或者您可以这样做(使用非过时的puppet和未来的解析器,如果是3.8.x):

或者你可以使用augeas,我个人很讨厌,但是如果你需要更多的选择,请告诉我

还有一些模块可以连接到如下文件:

您还可以尝试修复regexp,从以下两个警告开始:

$line_string_regexp = "/var/log/secure \{
    monthly
    rotate 11
\}"

老实说,您在这里有很多选择。

您是否考虑过将文件放在/etc/logrotate.d中?
$line_string_regexp = "/var/log/secure \{
    monthly
    rotate 11
\}"