Puppet

如何使用 Augeas 註釋掉/取消註釋配置文件中的一行?

  • March 15, 2012

假設我在/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'",
       ],
   }
}

但是我如何取消註釋這一行?


更新

這是我一直試圖在之後插入一行的內容#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 目前無法{,un}評論節點。

此票證詳細說明了原因(和建議的解決方案)。

至於你的插入失敗的原因,是因為你創建了一個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> 

第一行確保此更改是冪等的。如果您使用 Puppet,這可以簡化:您可以通過使用onlyif.

引用自:https://serverfault.com/questions/340755