qua*_*nta 7 configuration-management puppet augeas
假设我在/etc/syslog.conf文件中有以下内容:
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
Run Code Online (Sandbox Code Playgroud)
我想将其更改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"],
}
}
Run Code Online (Sandbox Code Playgroud)
或者我也可以使用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'",
],
}
}
Run Code Online (Sandbox Code Playgroud)
或修改目的地:
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'",
],
}
}
Run Code Online (Sandbox Code Playgroud)
但是我如何取消注释这一行?
更新
这是我一直试图在之后插入一行的内容#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
Run Code Online (Sandbox Code Playgroud)
或者:
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
Run Code Online (Sandbox Code Playgroud)
但它没有用:
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\" }"
Run Code Online (Sandbox Code Playgroud)
{,Un} 注释对于 Augeas 来说是一个复杂的问题,因为它的性质。简短的回答是 Augeas 目前不能{,un}评论节点。
原因(和建议的解决方案)在这张票中详细说明。
至于你insert失败的原因,是因为你创建了一个facle节点而不是一个entry节点。facle不是 中的已知节点名称syslog.aug。
所以这里有一些你可以做的事情:
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>
Run Code Online (Sandbox Code Playgroud)
第一行确保此更改是幂等的。如果您使用 Puppet,这可以简化:您可以通过使用onlyif.