6

I would like to update a section in an XML config file or add a new one if does not exist already using Augeas.

The XML file looks like this:

<?xml version="1.0"?>
<security>
          <users>
            ...
            <user>
              <id>deployment</id>
              <firstName>Deployment</firstName>
              <lastName>User</lastName>
              <password>somepasshere</password>
              <status>active</status>
              <email>changeme1@yourcompany.com</email>
            </user>
          </users>
</security>

I would like to update the last name/first name/email if the ID exists already or add a new user section if it's a new ID.

In AugTool I use:

augtool> set /augeas/load/Xml/lens Xml.lns
augtool> set /augeas/load/Xml/incl /security.xml
augtool> load

I'm still learning Augeas, so this was my first try to get the node :

augtool> print /files/security.xml/security/users/user/*[ #text= 'deployment']

What would be the command to update or create a new section user in users ?

thank you!

PapelPincel
  • 4,255
  • 6
  • 39
  • 49

1 Answers1

7

First, with recent Augeas versions (>= 1.0.0), you can use --transform to set up the transformation. Let's say the file is ./users.xml:

$ augtool -r . --noautoload --transform "Xml.lns incl /users.xml"
augtool> defnode user /files/users.xml/security/users/user[id/#text="deployment"] # Create a new user entry if it doesn't exist yet, assign node to the "user" variable
augtool> set $user/id/#text "deployment"         # Set id if node was just created
augtool> set $user/firstName/#text "Deployment"  # Set firstName
augtool> set $user/lastName/#text "User"         # Set lastName
augtool> set $user/email/#text "changeme1@yourcompany.com"  # set email
...
augtool> save
Saved 1 file(s)

You can even turn this into a script, say user.augtool:

#!/usr/bin/augtool -sf
defnode user /files/users.xml/security/users/user[id/#text="deployment"]
set $user/id/#text "deployment"
set $user/firstName/#text "Deployment"
set $user/lastName/#text "User"
set $user/email/#text "changeme1@yourcompany.com"

which you can then launch:

$ chmod +x user.augtool
$ ./user.augtool --transform "Xml.lns incl /users.xml" -r .
Saved 1 file(s)
raphink
  • 3,625
  • 1
  • 28
  • 39
  • Thank you! one remark though, I could'nt save the xml file if it's been loaded with the command : augtool -r . --noautoload --transform "Xml.lns incl /users.xml" I'm using augeas ver 1.0.0 – PapelPincel Jan 27 '14 at 10:27
  • 1
    no erros, I doesn't print the message "Saved 1 file(s)" at the end and the files isn't modified. I have another issue, but this time with augeas resource type in Puppet (http://stackoverflow.com/questions/21384569/why-does-augeas-puppet-resource-type-need-3-arguments-for-defnode). If you have any idea that would be great :) thank you – PapelPincel Jan 27 '14 at 15:24
  • It could be that your file is not mapped properly (can you print the content ok?) or that the changes don't modify the file (in that case, Augeas will not save it). – raphink Jan 27 '14 at 16:05