Augeas

Changing XML files with augtool

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

Changing XML files with the augeas type in puppet

Case: We want to ensure the following in /var/lib/jenkins/config.xml:
  <useSecurity>true</useSecurity>
  <slaveAgentPort>9090</slaveAgentPort>
  <securityRealm class="org.jenkinsci.plugins.reverse_proxy_auth.ReverseProxySecurityRealm" plugin="reverse-proxy-auth-plugin@1.4.0">
    <inhibitInferRootDN>false</inhibitInferRootDN>
    <userSearch>uid={0}</userSearch>
    <updateInterval>15</updateInterval>
    <forwardedUser>ADFS_LOGIN</forwardedUser>
    <headerGroups>ADFS_GROUP</headerGroups>
    <headerGroupsDelimiter>;</headerGroupsDelimiter>
    <proxyTemplate/>
    <authContext/>
    <authorityUpdateCache/>
    <userSearchBase/>
  </securityRealm>

In puppet, we use the aegan type. We delete the elements before (possibly re-)creating them, so that we ensure that we get exactly this result:

  # jenkins config
  augeas { 'jenkins_config.xml':
    incl    => '/var/lib/jenkins/config.xml',
    lens    => 'Xml.lns',
    context => '/files/var/lib/jenkins/config.xml/hudson',
    changes => [

      #    
      # Manage Jenkins -> Configure Global Security
      #

      # Enable security
      "rm useSecurity",
      "set useSecurity/#text true",

      # TCP port for JNLP slave agents
      "rm slaveAgentPort",
      "set slaveAgentPort/#text 9090",

      # Security realm: HTTP Header by reverse proxy 
      "rm securityRealm",
      "set securityRealm/#attribute/class org.jenkinsci.plugins.reverse_proxy_auth.ReverseProxySecurityRealm",
      "set securityRealm/#attribute/plugin reverse-proxy-auth-plugin@1.4.0",
        "set securityRealm/inhibitInferRootDN/#text false",
        "set securityRealm/userSearch/#text uid={0}",
        "set securityRealm/updateInterval/#text 15",
        "set securityRealm/forwardedUser/#text ADFS_LOGIN",
        "set securityRealm/headerGroups/#text ADFS_GROUP",
        "set securityRealm/headerGroupsDelimiter/#text ;",
        "clear securityRealm/proxyTemplate",
        "clear securityRealm/authContext",
        "clear securityRealm/authorityUpdateCache",
        "clear securityRealm/userSearchBase"
    ]
  }
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r2 - 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