如何在puppet中使用augeas创建格式良好的xml?

时间:2013-11-06 21:28:26

标签: xml puppet augeas

我正在尝试使用puppet来编辑jenkins config.xml。由于种种原因,我认为augeas最有意义,而且我几乎拥有我需要的东西,但格式非常粗糙。

这是我的傀儡文件:

augeas { 'jenkins_config.xml' :
  incl    => '/tmp/config.xml',
  lens    => 'Xml.lns',
  context => '/files/tmp/config.xml/hudson',
  changes => [
    "set securityRealm/#attribute/class hudson.security.PAMSecurityRealm",
    "set securityRealm/#attribute/plugin pam-auth@1.0",
    "set securityRealm/serviceName/#text sshd",
  ],
}

我在寻找:

<hudson>
  <securityRealm class="hudson.security.PAMSecurityRealm" plugin="pam-auth@1.0">
    <serviceName>sshd</serviceName>
  </securityRealm>
</hudson>

我得到了什么:

<hudson>
<securityRealm class="hudson.security.PAMSecurityRealm" plugin="pam-auth@1.0"><serviceName>sshd</serviceName>
</securityRealm>
</hudson>

内容很好(顺便说一下,这太棒了),但阅读起来并不好玩。 augeas可以为我处理缩进和换行吗?如果我必须自己做,有人可以提供缩进提示吗?我的尝试都失败了。

1 个答案:

答案 0 :(得分:5)

简短的回答是你没有。

Augeas(目前)不允许您管理它,它只允许您按原样编辑文件,并使用默认值添加新参数。此外,它无法检测当前的标识以重复使用它。

实现目标的一种方法是使用漂亮的打印机命令,并在Augeas更改后插入它,例如:

augeas { 'manage your file':
  changes => [ 'blah'],
} ~>
exec { 'pretty print file':
  command     => '/bin/pretty-print-cmd file',
  refreshonly => true,
}

当应用Augeas更改时,这将触发漂亮的打印命令。对于漂亮的打印,您可以使用xmlstarlet作为例子。