AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 557378
Accepted
Red Cricket
Red Cricket
Asked: 2013-11-26 13:57:15 +0800 CST2013-11-26 13:57:15 +0800 CST 2013-11-26 13:57:15 +0800 CST

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

  • 772

我想确保我有线条......

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

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

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

...但我不喜欢硬编码15. 在某些系统上很有greenman: /dev/null可能是第 10 个名称/值对。有没有办法避免在 中使用数字/files/etc/aliases/15/name?

谢谢

augeas
  • 1 1 个回答
  • 1834 Views

1 个回答

  • Voted
  1. Best Answer
    raphink
    2013-11-26T14:51:46+08:002013-11-26T14:51:46+08:00

    在 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 (i.e. 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为脚本的最后一行。

    使用“真正的”编程语言

    Bash 很好,但应对其限制可能会导致复杂的解决方案。Augeas 有很多语言的绑定。只需选择一个,编写代码就会变得更容易,因为您将能够使用持久的 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 中,最好的解决方案是依赖来自augeasproviders的提供者mailalias的类型。它使用 Augeas Ruby 库来安全地编辑:augeas/etc/aliases

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

    注意:mailalias是标准的 Puppet 类型,但默认提供程序不使用 Augeas。

    • 4

相关问题

  • 使用 Puppet 而不是 Augeas 本身有什么好处?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve