假设我在/etc/syslog.conf文件中有以下内容:
# Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.* /dev/console
我想将其更改为kern.* /var/log/kern.log以获取内核日志的可读时间戳。
木偶可以做到这一点:
class syslog::config { file { "/etc/syslog.conf": ensure => present, source => "puppet:///modules/syslog/syslog.conf", require => Class["syslog::install"], notify => Class["syslog::service"], } }
或者我也可以使用sed -i 。
通过Augeas ,我可以在文件末尾追加这一行:
class syslog::config { augeas { "syslogkern": context => "/files/etc/syslog.conf", changes => [ "set entry[last()+1]/selector/facility kern", "set entry[last()]/selector/level *", "set entry[last()]/action/file '/var/log/kern.log'", ], } }
或修改目的地:
class syslog::config { augeas { "syslogkern": context => "/files/etc/syslog.conf", onlyif => "get #comment[3] == 'kern.*\t\t\t\t\t\t\t/dev/console'", changes => [ "set #comment[3] 'kern.*\t\t\t\t\t\t\t/var/log/kern.log'", ], } }
但是,我如何取消这条线呢?
UPDATE
以下是我在#comment[3]之后试图插入一行的内容:
augtool> ins facle after /files/etc/syslog.conf/#comment[3] augtool> set /files/etc/syslog.conf/facle/selector/facility kern augtool> set /files/etc/syslog.conf/facle/selector/level * augtool> set /files/etc/syslog.conf/facle/action/file /var/log/kern.log
要么:
augtool> ins facle after /files/etc/syslog.conf/#comment[3] augtool> set /files/etc/syslog.conf/facle[last()] kernlog augtool> set /files/etc/syslog.conf/facle[. = 'kernlog']/selector/facility kern augtool> set /files/etc/syslog.conf/facle[. = 'kernlog']/selector/level * augtool> set /files/etc/syslog.conf/facle[. = 'kernlog']/action/file /var/log/kern.log
但它没有工作:
augtool> save error: Failed to execute command error: saving failed (run 'print /augeas//error' for details) augtool> print /augeas//error /augeas/files/etc/syslog.conf/error = "put_failed" /augeas/files/etc/syslog.conf/error/path = "/files/etc/syslog.conf" /augeas/files/etc/syslog.conf/error/lens = "/usr/share/augeas/lenses/dist/syslog.aug:243.18-.51:" /augeas/files/etc/syslog.conf/error/message = "Failed to match \n ({ } | { /#comment/ = /[^\\001-\\004\\t\\n !+-][^\\001-\\004\\n]*[^\\001-\\004\\t\\n ]|[^\\001-\\004\\t\\n !+-]/ } | { /entry/ })*({ /program/ } | { /hostname/ })*\n with tree\n { \"#comment\" = \"Log all kernel messages to the console.\" } { \"#comment\" = \"Logging much else clutters up the screen.\" } { \"#comment\" = \"kern.*\t\t\t\t\t\t\t/var/log/kern.log\" } { \"facle\" = \"kernlog\" } { \"entry\" } { } { \"#comment\" = \"Log anything (except mail) of level info or higher.\" } { \"#comment\" = \"Don't log private authentication messages!\" } { \"entry\" } { } { \"#comment\" = \"The authpriv file has restricted access.\" } { \"entry\" } { } { \"#comment\" = \"Log all the mail messages in one place.\" } { \"entry\" } { } { } { \"#comment\" = \"Log cron stuff\" } { \"entry\" } { } { \"#comment\" = \"Everybody gets emergency messages\" } { \"entry\" } { } { \"#comment\" = \"Save news errors of level crit and higher in a special file.\" } { \"entry\" } { } { \"#comment\" = \"Save boot messages also to boot.log\" } { \"entry\" } { } { } { \"#comment\" = \"INN\" } { } { \"entry\" } { \"entry\" } { \"entry\" }"
{,Un}评论与Augeas是一个复杂的问题,因为它的性质。 简单的答案是,Augeas目前不能评论节点。
原因(和build议的解决scheme)在这张票上详细说明。
至于你的插入失败的原因,这是因为你创build了一个facle节点,而不是一个entry节点。 facle不是facle中的已知节点名称。
所以这里是你可以做的事情:
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>
第一行确保这种改变是幂等的。 如果你使用Puppet,这可以被简化:你可以通过使用onlyif来避免第一行的复杂性。
Augeas AFAIK没有简单的“取消这条线”设施。 您可以使用ins来查找现有的注释,像使用set命令一样插入新的条目,然后删除注释。
根据要求,下面是一个如何为GRUB的串行控制台设置“串行”和“terminal”的示例:
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", ], }
唯一的警告是timeout必须存在。
实际上,我不确定这是否是一个好例子,但在这里呢。