Modifying XML files with Puppet's Augeas resource

January 31, 2020 

This article is based on an earlier article written in Finnish.

Here I describe how to make changes to an XML file with Augeas - a useful approach when managing the whole file as a template is out of the question. The configuration file used as an example, web.xml, belong to Pwm, and we intend to set the webapp context parameter.

As discussed before it is important to get an idea of how Augeas sees the file before attempting to modify it with Puppet. First we tell which Augeas lens to use (here: Xml) and which file(s) to associate with the Xml lens. This can be done with in one step:

$ augtool
augtool> set /augeas/load/Xml/incl[1] /var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml
augtool> load

Perhaps a bit clearer approach is to split this into two steps:

$ augtool
augtool> set /augeas/load/xml/lens "Xml.lns"
augtool> set /augeas/load/xml/incl "/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml"
augtool> load

Now ensure that Augeas can actually see the file you are about to modify:

augtool> ls var/lib/tomcat8/webapps/pwm/WEB-INF/
web.xml/ = (none)

Finally check how Augeas views the file. This command produces a huge number of output of which only the essential is shown below:

augtool> print var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/
--- snip ---
/files/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/web-app/context-param/param-name
/files/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/web-app/context-param/param-name/#text = "applicationPath"
/files/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/web-app/context-param/#text[3] = " "
/files/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/web-app/context-param/param-value
/files/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/web-app/context-param/param-value/#text = "unspecified"
/files/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml/web-app/context-param/#text[4] = " "
--- snip --

Based on this we can create the Puppet Augeas resource:

 augeas { 'pwm applicationPath':
   incl    => "/var/lib/tomcat8/webapps/pwm/WEB-INF/web.xml",
   lens    => 'Xml.lns',
   changes => "set web-app/context-param/param-value/#text /etc/pwm",
 }
Samuli Seppänen
Samuli Seppänen
Author archive
menucross-circle