Changing a xml file using augeas

Example 1 - Configuring XML with augtool (simple example)

1) Define a XML file to play with:
$ cat /tmp/foo.xml
<TopLevel>
   <FooBar foo="bar">2</FooBar>
   <Foo>
      <Bar foo="bar">foo</Bar>
      <Lol />
   </Foo>
</TopLevel>

2) Start augtool:

$augtool --noload --noautoload --echo

3) Set XML lens:

augtool> set /augeas/load/xml/lens "Xml.lns"
augtool> set /augeas/load/xml/incl "/tmp/foo.xml"
augtool> load

4) Print internal tree:

augtool> print /files
/files
/files/tmp
/files/tmp/foo.xml
/files/tmp/foo.xml/TopLevel
/files/tmp/foo.xml/TopLevel/#text[1] = "\n\t"
/files/tmp/foo.xml/TopLevel/FooBar
/files/tmp/foo.xml/TopLevel/FooBar/#attribute
/files/tmp/foo.xml/TopLevel/FooBar/#attribute/foo = "bar"
/files/tmp/foo.xml/TopLevel/FooBar/#text = "2"
/files/tmp/foo.xml/TopLevel/#text[2] = "\t"
/files/tmp/foo.xml/TopLevel/Foo
/files/tmp/foo.xml/TopLevel/Foo/#text[1] = "\n\t\t"
/files/tmp/foo.xml/TopLevel/Foo/Bar
/files/tmp/foo.xml/TopLevel/Foo/Bar/#attribute
/files/tmp/foo.xml/TopLevel/Foo/Bar/#attribute/foo = "bar"
/files/tmp/foo.xml/TopLevel/Foo/Bar/#text = "foo"
/files/tmp/foo.xml/TopLevel/Foo/#text[2] = "\t\t"
/files/tmp/foo.xml/TopLevel/Foo/Lol = "#empty"
/files/tmp/foo.xml/TopLevel/Foo/#text[3] = "\t"

5) Define a attribute, and a value for the element. First, you have to remove the #empty value for Lol, then you have to set the attribute, and then the value (in that order)

augtool> set /files/tmp/foo.xml/TopLevel/Foo/Lol
augtool> set /files/tmp/foo.xml/TopLevel/Foo/Lol/#attribute/jk "abc"
augtool> set /files/tmp/foo.xml/TopLevel/Foo/Lol/#text "def"

6) Verify changes:

augtool> print /files
...
/files/tmp/foo.xml/TopLevel/Foo/Lol
/files/tmp/foo.xml/TopLevel/Foo/Lol/#attribute
/files/tmp/foo.xml/TopLevel/Foo/Lol/#attribute/jk = "abc"
/files/tmp/foo.xml/TopLevel/Foo/Lol/#text = "def"
...

7) Flush changes to disk:

augtool> save

Example 2 - Configuring XML with augtool (more advanced example)

We want to go from:
# cat config.xml
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
</hudson>

To:

# cat after.xml 
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
  <useSecurity>true</useSecurity>
  <securityRealm class="org.jenkinsci.plugins.reverse_proxy_auth.ReverseProxySecurityRealm" plugin="reverse-proxy-auth-plugin@1.4.0">
    <proxyTemplate/>
    <authContext/>
    <authorityUpdateCache/>
    <inhibitInferRootDN>false</inhibitInferRootDN>
    <userSearchBase></userSearchBase>
    <userSearch>uid={0}</userSearch>
    <updateInterval>15</updateInterval>
    <forwardedUser>ADFS_LOGIN</forwardedUser>
    <headerGroups>ADFS_GROUP</headerGroups>
    <headerGroupsDelimiter>;</headerGroupsDelimiter>
  </securityRealm>
  <slaveAgentPort>9090</slaveAgentPort>
</hudson>

1) Load augtool and set the environment:

augtool> set /augeas/load/xml/lens "Xml.lns"
augtool> set /agueas/load/xml/incl "/tmp/augeas/config.xml"
augtool> load

2) Set the parameters, and flush them to disk:

augtool> set /files/tmp/augeas/config.xml/hudson/useSecurity/#text true
augtool> set /files/tmp/augeas/config.xml/hudson/slaveAgentPort/#text 9090
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/#attribute/class org.jenkinsci.plugins.reverse_proxy_auth.ReverseProxySecurityRealm
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/#attribute/plugin reverse-proxy-auth-plugin@1.4.0
augtool> touch /files/tmp/augeas/config.xml/hudson/securityRealm/proxyTemplate
augtool> touch /files/tmp/augeas/config.xml/hudson/securityRealm/authContext
augtool> touch /files/tmp/augeas/config.xml/hudson/securityRealm/authorityUpdateCache
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/inhibitInferRootDN/#text false
augtool> touch /files/tmp/augeas/config.xml/hudson/securityRealm/userSearchBase
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/userSearch/#text uid={0}
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/updateInterval/#text 15
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/forwardedUser/#text ADFS_LOGIN
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/headerGroups/#text ADFS_GROUP
augtool> set /files/tmp/augeas/config.xml/hudson/securityRealm/headerGroupsDelimiter/#text ;
augtool> save

3) augeas does not worry about formatting, and output unindented xml among other bad practices. Use xmllint to clean the file:

# xmllint --format config.xml --output config.xml

Example 3 - Example 2, configured in puppet

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2014-07-24 - TerjeAndersen
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback