check and add multiple lines in puppet

不羁岁月 提交于 2019-12-10 23:32:07

问题


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

/var/log/secure {
    monthly
    rotate 11
}

I tried

$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
}

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


回答1:


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).

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

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

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

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

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.



来源:https://stackoverflow.com/questions/38539178/check-and-add-multiple-lines-in-puppet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!