问题
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