Adding comments to config files with augeas

1,710 views
Skip to first unread message

Bryan Ross

unread,
Sep 10, 2009, 11:42:30 AM9/10/09
to puppet...@googlegroups.com
Hi,

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

David Lutterkort

unread,
Sep 10, 2009, 5:07:22 PM9/10/09
to puppet...@googlegroups.com
On Thu, 2009-09-10 at 16:42 +0100, Bryan Ross wrote:
> 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.

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.

Bryan Ross

unread,
Sep 11, 2009, 5:06:54 AM9/11/09
to puppet...@googlegroups.com
2009/9/10 David Lutterkort <lut...@redhat.com>:

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

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

Reply all
Reply to author
Forward
0 new messages