How to comment out / uncomment a line in a configuration file with Augeas?
{,Un}commenting is a complex matter with Augeas, because of its nature. The short answer is that Augeas cannot {,un}comment nodes currently.
The reason (and proposed solutions) is detailed in this ticket.
As for the reason why your insert fails, it's because you created a facle
node instead of an entry
node. facle
is not a known node name in syslog.aug
.
So here is something you could do instead:
augtool> print /files/etc/syslog.conf/
/files/etc/syslog.conf
/files/etc/syslog.conf/#comment[1] = "titi"
/files/etc/syslog.conf/#comment[2] = "kern.* /dev/console"
/files/etc/syslog.conf/#comment[3] = "toto"
augtool> defvar kerncomment /files/etc/syslog.conf/#comment[. =~ regexp('kern.* +/dev/console')][count(/files/etc/syslog.conf/entry[selector/facility = "kern" and selector/level = "*" and action/file = "/var/log/kern.log"]) = 0]
augtool> ins entry after $kerncomment
augtool> defvar kernentry /files/etc/syslog.conf/entry[preceding-sibling::*[1][$kerncomment]]
augtool> set $kernentry/selector/facility kern
augtool> set $kernentry/selector/level *
augtool> set $kernentry/action/file /var/log/kern.log
augtool> rm $kerncomment
augtool> print /files/etc/syslog.conf/
/files/etc/syslog.conf
/files/etc/syslog.conf/#comment[1] = "titi"
/files/etc/syslog.conf/entry
/files/etc/syslog.conf/entry/selector
/files/etc/syslog.conf/entry/selector/facility = "kern"
/files/etc/syslog.conf/entry/selector/level = "*"
/files/etc/syslog.conf/entry/action
/files/etc/syslog.conf/entry/action/file = "/var/log/kern.log"
/files/etc/syslog.conf/#comment[3] = "toto"
augtool> save
Saved 1 file(s)
augtool>
The first line ensures this change is idempotent. This can be simplified if you use Puppet: you can avoid the complexity of the first line by using onlyif
.
There is not a simple "uncomment this line" facility in Augeas AFAIK. You can use ins
to locate the existing comment, insert the new entry with the set
commands as you have and then remove the comment.
Per request, here's an example of how I set up "serial" and "terminal" for a serial console for GRUB:
augeas { "grub-serial-ttyS${portnum}":
context => "/files/etc/grub.conf",
changes => [
'rm serial',
'ins serial after timeout',
"set serial/unit '${portnum}'",
"set serial/speed '${portspeed}'",
'rm terminal',
'ins terminal after serial',
"set terminal/timeout '5'",
"clear terminal/console",
"clear terminal/serial",
],
}
The only caveat is that timeout
has to exist.
Actually, I'm not sure this really is a good example, but here it is anyway.