3

I'm trying to use puppet to edit the jenkins config.xml. I have decided augeas makes the most sense for various reasons, and I nearly have what I need, but the formatting is pretty rough.

This is my puppet file:

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",
  ],
}

What I'm looking for:

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

What I'm getting:

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

The contents are fine (which is so awesome, by the way) but it's no fun to read. Can augeas handle the indentation and newlines for me? If I have to do it myself, can anyone provide tips on indentation? My attempts all failed.

Josh Gagnon
  • 5,342
  • 3
  • 26
  • 36
  • Similar: [Creating XML with “set” in Puppet Augeas](http://stackoverflow.com/q/18603757/55075) – kenorb Apr 08 '15 at 20:43

1 Answers1

5

The short answer is you don't.

Augeas doesn't (currently) allow you to manage that, it only allows you to edit the file as it is, and uses defaults for adding new parameters. In addition, it cannot detect current identation to re-use it.

One way for you to achieve what you want would be to use a pretty-printer command, and plug it after the Augeas changes, something like:

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

This will trigger the pretty printing command when Augeas changes are applied. For pretty printing, you could go with xmlstarlet for example.

raphink
  • 3,625
  • 1
  • 28
  • 39
  • It's a bummer that there isn't a perfect "Oh, just set this parameter." solution, but using a pretty-printer sounds quite feasible. And at least the important part - jenkins seeing correct config values - is working. – Josh Gagnon Nov 07 '13 at 12:51
  • Patches welcome :-) That said, Augeas' aim is to edit configuration files, not to format them. – raphink Nov 07 '13 at 13:09
  • xmltidy is quite simple to use for indent. Its usage: xmltidy FileName.xml – Ding-Yi Chen Mar 06 '14 at 07:07