本文介绍了检查并在 puppet 中添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to ensure through puppet that the file /etc/logrotate.conf must have the entry

/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,
}

它第一次创建条目,但是当我第二次应用傀儡代码时,它再次添​​加了条目

It creates the entry the first time, but when I apply the puppet code a second time it adds the entry again

[~] # 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 再次添加条目?

How can I prevent puppet from adding the entry a second time?

推荐答案

puppetlabs-stdlib 中的 file_line 资源在 line 不是幂等的> 属性在其参数中有换行符.您可以使用 match 属性对此进行处理,但您提供的正则表达式无效(请注意来自 file_line.rb 的警告).

The file_line resource from puppetlabs-stdlib is not idempotent when the line attribute has newlines in its parameter. You could do something about this with the match attribute, but the regexp you supplied is invalid (note the warnings from file_line.rb).

由于 /etc/logrotate.conf 似乎只包含该条目,您可以这样做:

Since it appears that the /etc/logrotate.conf contains only that entry, you could do:

file { '/etc/logrotate.conf':
  ensure  => file,
  content => $line_string,
}

或者你可以这样做(如果是 3.8.x,使用非过时的 puppet 和未来的解析器):

or you could do (with non-obsolete puppet and future parser if 3.8.x):

['/var/log/secure {', '    monthly', '    rotate 11', '}'].each |$line| {
  file_line { "ensure $line in /etc/logrotate.conf":
    path  => '/etc/logrotate.conf',
    line  => $line,
    match => $line,
  }
}

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

or you could use augeas, which I personally loathe, but let me know if you need more options.

还有一些模块可以连接到这样的文件上:https://github.com/puppetlabs/puppetlabs-concat

There are also modules to concatenate onto files like this one: https://github.com/puppetlabs/puppetlabs-concat

您也可以尝试修复您的正则表达式,从两个警告开始:

You could also try fixing your regexp, starting with the two warnings:

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

老实说,你有很多选择.

You have a lot of options here to be honest.

这篇关于检查并在 puppet 中添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:31