How manage xml elements as resources?

2,407 views
Skip to first unread message

Schofield

unread,
Jan 7, 2013, 4:24:43 PM1/7/13
to puppet...@googlegroups.com
I would like to use puppet classes to inject and/or replace xml elements into an existing xml configuration file.  This seems to me to be a common problem already solved.  Can anyone suggest the easiest way to do this?
 
An example use case would look like something like this.
Given xml file:
 
<host>
  <server name="one"/>
  <server name="two"/>
</host>
 
I want to inject <datasource name="example"/> into server one resulting in.
 
<host>
  <server name="one">
    <datasource name="example"/>
  </server>
  <server name="two"/>
</host>
 
Of course puppet would still need to make sure datasource example is always under server one should someone manually modify the server one element in the future.

Roman Shaposhnik

unread,
Jan 7, 2013, 6:34:16 PM1/7/13
to puppet...@googlegroups.com
On Mon, Jan 7, 2013 at 1:24 PM, Schofield <dbsch...@gmail.com> wrote:
> I would like to use puppet classes to inject and/or replace xml elements
> into an existing xml configuration file. This seems to me to be a common
> problem already solved. Can anyone suggest the easiest way to do this?

Well, the tool du jour for that would be augeas. That said, if the config
file actually happens to be under your manifest control fully (even though
different resources are depositing bits and pieces of configuration info
into it) you could go with virtual resource template and sed.

Thanks,
Roman.

j4m3s

unread,
Jan 8, 2013, 6:01:43 AM1/8/13
to puppet...@googlegroups.com, ro...@shaposhnik.org
I started to look into this for JBoss - it uses an xml config file but the app itself makes changes to the file
when you deploy an application (in domain mode) - so using the normal file resource in puppet simply wouldn't work.

I found the Augeas documentation to be rather hard to get into (or even find).  I was expecting there to be
a straightforward (xpath-style?) lens for editing xml files but couldn't find anything.  There wasn't a JBoss AS lens
either, which again was surprising since JBoss is a RedHat product and it looks like Augeas is sponsored by RedHat.

In the end I found a workaround for JBoss so I can just use normal puppet File type, though my XPath thoughts
did make me wonder whether I ought to actually be looking for an xpath type/ resource/ plugin for puppet (sorry
for the incorrect terminology).  Augeas actually uses a syntax that looks a lot like xpath...

Dominic Cleal

unread,
Jan 8, 2013, 9:54:19 AM1/8/13
to puppet...@googlegroups.com
On 08/01/13 11:01, j4m3s wrote:
> I started to look into this for JBoss - it uses an xml config file but
> the app itself makes changes to the file
> when you deploy an application (in domain mode) - so using the normal
> file resource in puppet simply wouldn't work.
>
> I found the Augeas documentation to be rather hard to get into (or even
> find). I was expecting there to be
> a straightforward (xpath-style?) lens for editing xml files but couldn't
> find anything. There wasn't a JBoss AS lens
> either, which again was surprising since JBoss is a RedHat product and
> it looks like Augeas is sponsored by RedHat.

If it's just XML, there's a lens for that already - it wouldn't need
another. XML support is limited before Augeas 1.0 (released last month)
as it only supports double quotes.

Docs are a problem, but that's because it's primarily a library and so
docs are targeted at those developers. The Augeas type in Puppet is
very low-level, so it's tricky to use. Most docs about it are here. but
there's no XML example yet:

http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas

> In the end I found a workaround for JBoss so I can just use normal
> puppet File type, though my XPath thoughts
> did make me wonder whether I ought to actually be looking for an xpath
> type/ resource/ plugin for puppet (sorry
> for the incorrect terminology). Augeas actually uses a syntax that
> looks a lot like xpath...

Indeed, it's inspired by XPath. I tend to start with augtool to see how
the tree looks on the original file and the finished file, then it's
much easier to create expressions to make the changes:

$ augtool --noautoload -r .
augtool> set /augeas/load/Xml/lens Xml.lns
augtool> set /augeas/load/Xml/incl /jbossas.xml
augtool> load
augtool> print /files/jbossas.xml/
/files/jbossas.xml
/files/jbossas.xml/host
/files/jbossas.xml/host/#text[1] = "\n "
etc.

The commands available in Puppet mirror those in the augtool (a test
utility).

I came up with this example to apply the transformation you mentioned:

augeas { "jbossas-datasource":
lens => "Xml.lns",
incl => "/etc/jbossas.xml",
changes => [
"set host/server[#attribute/name='one'] ''",
"set host/server[#attribute/name='one']/datasource #empty",
"set
host/server[#attribute/name='one']/datasource/#attribute[name='example']/name
example",
],
}

Cheers,

--
Dominic Cleal
Red Hat Engineering
Reply all
Reply to author
Forward
0 new messages