如何用augtool来维护`/ etc / aliases`文件?

我想确保我有线…

root: /dev/null greenman: /dev/null 

…在我的/etc/aliases文件中,我一直在学习augtool 。 我知道我可以用augtool做这样的事情…

 $ sudo augtool set /files/etc/aliases/15/name greenman 

…但我不喜欢硬编码15 。 在一些系统上, greenman: /dev/null可能是非常有可能的greenman: /dev/null可能是第10个名称/值对。 有没有办法避免在/files/etc/aliases/15/name

谢谢

在Augeas中使用seq条目(这些编号的条目,例如15 )可能有点棘手。

仍然使用augtool

你可以使自己的augtool脚本看起来像这样:

 #!/usr/bin/augtool -sf # First, match /etc/aliases, only if there's no "greenman" entry yet # This will define a `$yellowwoman` variable which will contain `/etc/aliases`, # but only if there is no "greenman" entry yet (ie the count of the # "greenman" entries is 0) defvar yellowwoman /files/etc/aliases[count(*[name="greenman"])=0] # Add the greenman entry at the end # (using `01`, which is never used automatically in `seq` entries) set $yellowwoman/01/name greenman # Now set the value for the "greenman" entry, # which you can be sure exists. # Don't reuse the `$yellowwoman` variable, otherwise you'll # only set the value when "greeman" wasn't there yet # By selecting the "greenman" entry, you will always set the value # even if the entry already existed in the file set /files/etc/aliases/*[name="greenman"]/value /dev/null 

shebang( #!/usr/bin/augtool -sf )使用-s在更改后自动保存, -f使用命令获取文件,使其成为可自行执行的脚本,因此您可以将文件可执行和运行它:

 $ chmod +x greenman.augtool $ sudo ./greenman.augtool Saved 1 file(s) $ sudo ./greenman.augtool # it should be idempotent, too 

如果您不想让脚本可执行,您也可以将它传递给augtool

 $ sudo augtool --autosave --file greenman.augtool Saved 1 file(s) 

如果你不想使用 – --autosave save ,你可以添加save为脚本的最后一行。

使用“真正”的编程语言

Bash很好,但是处理它的极限可能会导致复杂的解决scheme。 Augeas在很多语言中都有很多绑定。 只需select一个,编写代码就会变得更容易,因为您可以使用持续的Augeas处理程序。 这是一个Ruby的例子:

 #!/usr/bin/env ruby require 'augeas' Augeas.open do |aug| if aug.match('/files/etc/aliases/*[name="greenman"]').empty? # Create a new "greeman" entry aug.set('/files/etc/aliases/01/name', 'greenman') end # Set the value aug.set('/files/etc/aliases/*[name="greenman"]/value', '/dev/null') # Commit your changes aug.save! end 

使用木偶

在Puppet中,最好的解决scheme是依靠augeas提供者的augeas提供者的mailaliastypes。 它使用Augeas Ruby库安全地编辑/etc/aliases

 mailalias { 'greenman': ensure => present, recipient => '/dev/null', provider => augeas, } 

注意: mailalias是标准的Puppettypes ,但默认的提供者不使用Augeas。