Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top