I've got a 'define' that I use to set kernel parameters in
/etc/sysctl.conf using the augeas type. It works well, but I'd like
to be able to add a comment line directly above my the parameter to
explain what it does and why it's been changed. We currently just add
a comment in site.pp, but that's not much use to local admins.
Unfortunately, I can't seem to find any way of inserting arbitrary
strings using augeas. I tried the 'insert' command, but after some
reading I understand now that ins/insert is only meant for controlling
where a parameter should be put in the file.
Here's a simplified version of a (broken) sysctl define. I know why
this doesn't work - it's just to give an idea of what I'd like to get
working!
define sysctl::config ($value, $comment) {
augeas { "sysctl_$name":
context => "/files/etc/sysctl.conf",
changes => [
"set $name $value",
"insert '# ${comment}' before $name",
],
onlyif => "get $name != $value",
}
}
Anyone have any ideas?
TIA,
Bryan
Comments for most files[1] show up as nodes with label '#comment' and a
value that contains the comment without the leading '#[ \t]*'.
Therefore, to add a comment, you need to first create a '#comment' node
and then set it to whatever you want the comment to be.
> Here's a simplified version of a (broken) sysctl define. I know why
> this doesn't work - it's just to give an idea of what I'd like to get
> working!
>
> define sysctl::config ($value, $comment) {
> augeas { "sysctl_$name":
> context => "/files/etc/sysctl.conf",
> changes => [
> "set $name $value",
> "insert '# ${comment}' before $name",
> ],
> onlyif => "get $name != $value",
> }
> }
Almost ;) What you want for changes is
changes => [
"set $name $value",
"insert #comment before $name",
"set #comment[last()] 'Look Ma, a comment'"
]
David
[1] There are some inconsistencies that we are gradually fixing; the
lens for sysctl.conf gets it right.
Many thanks David - works perfectly! I can also understand where my
thinking was wrong.
For anyone googling this, here's a working example:
node default {
sysctl::config { "fs.file-max":
value => 65536,
comment => "Maximum number of filehandles",
}
}
class sysctl {
define config ($value, $comment) {
file { "/etc/sysctl.conf":
mode => 644,
owner => root,
group => root,
ensure => present,
}
augeas { "sysctl_$name":
context => "/files/etc/sysctl.conf",
changes => [
"set $name $value",
"insert #comment before $name",
"set #comment[last()] '$comment'"
],
onlyif => "get $name != $value",
notify => Exec["/sbin/sysctl -p"],
}
exec { "/sbin/sysctl -p":
subscribe => File["/etc/sysctl.conf"],
refreshonly => true,
}
}
}
Cheers,
Bryan