Ask Your Question
0

Add new lines to XML

asked 2015-07-19 13:45:14 -0500

R gravatar image

updated 2015-07-19 13:48:37 -0500

I have a XML file this:

<Book>
<BOOKNAME> Oracle 12c </BOOKNAME>
</Book>

I want to add the following lines to BOOK section

<AUTHOR> Richard Gosling </AUTHOR>
<ISBN> 0-07-913702-4</ISBN>
<Publisher> McGraw-Hill</Publisher>
<Price> 39.99</Price>

so that it looks the like following:

<Book>
<BOOKNAME> Oracle 12c </BOOKNAME>
<AUTHOR> Richard Gosling </AUTHOR>
<ISBN> 0-07-913702-4</ISBN>
<Publisher> McGraw-Hill</Publisher>
<Price> 39.99</Price>
</Book>

I looked at this link https://ask.puppetlabs.com/question/1... but terc xml forge module doesn't tell how to add new lines to XML. Please let me know.

Thanks in advance

edit retag flag offensive close merge delete

Comments

The xml file resides on a Linux (centos/RHEL) host. puppet --version 3.7.4 (Puppet Enterprise 3.7.2)

R gravatar imageR ( 2015-07-19 19:59:18 -0500 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2015-07-20 08:35:36 -0500

jonuwz gravatar image

updated 2015-07-22 07:51:03 -0500

Having puppet template entire XML files, particularly if they are written to by other applications is sketchy at best. If you only need to manage certain elements in an XML file - then you should only be managing those certain elements.

For that there's augeas :

augeas {"add stuff to book.xml":
  lens       => 'Xml.lns',
  incl       => '/tmp/book.xml',
  context    => '/files/tmp/book.xml',
  changes    => [
    "set Book[BOOKNAME/#text = \"Oracle 12c\"]/AUTHOR/#text 'Richard Gosling'",
    "set Book[BOOKNAME/#text = \"Oracle 12c\"]/ISBN/#text '0-07-913702-4'",
    "set Book[BOOKNAME/#text = \"Oracle 12c\"]/Publisher/#text 'McGraw-Hill'",
    "set Book[BOOKNAME/#text = \"Oracle 12c\"]/Price/#text '39.99'"
  ]
}

There's a ton of examples on how you could wrap this in a defined type to make it more reusable : http://projects.puppetlabs.com/projec...

Example : ( /tmp/book.pp contains the puppet code above )

cat /tmp/book.xml

<Book>
<BOOKNAME>Oracle 12c</BOOKNAME>
</Book>

# puppet apply /tmp/book.pp 
Notice: Compiled catalog for XXXXXX in environment production in 0.13 seconds
Notice: /Stage[main]/Main/Augeas[add stuff to book.xml]/returns: executed successfully
Notice: Finished catalog run in 0.48 seconds

# cat book.xml 

<Book>
<BOOKNAME>Oracle 12c</BOOKNAME>
<AUTHOR>Richard Gosling</AUTHOR>
<ISBN>0-07-913702-4</ISBN>
<Publisher>McGraw-Hill</Publisher>
<Price>39.99</Price>
</Book>
edit flag offensive delete link more

Comments

@jonuwz thanks a lot for the code snippet. Just a quick question about augeas, according to puppet doc "The Augeas resource in Puppet has a useful attribute called “context” which allows you to specify a root for all of your changes. This is especially nice when making many changes to the same file"

R gravatar imageR ( 2015-07-20 12:13:29 -0500 )edit

What does this mean? Does it alter the book.xml under /files/tmp/book.xml? Thanks a lot

R gravatar imageR ( 2015-07-20 12:14:28 -0500 )edit

rather than having to use the full path like this : "set /files/tmp/book.xml/Book/xx/AUTHOR/#text 'lala'", I can omit the /files/tmp/book.xml by setting the context. Its like cd-ing into a directory and editing files, rather than writing out the full path eatch time.

jonuwz gravatar imagejonuwz ( 2015-07-20 12:24:15 -0500 )edit

vow ok, thanks a lot for the awesome explanation.

R gravatar imageR ( 2015-07-20 12:36:54 -0500 )edit

When I apply the manifest (with the above code), it doesn't do anything to the xml file. There are no errors while running "puppet apply init.pp". Verified that Augeas is installed, is it because I couldn't find Xml.lns under /usr/share/augeas/lenses or under /opt/puppet/share/augeas/lenses/dist/.

R gravatar imageR ( 2015-07-20 14:14:03 -0500 )edit
0

answered 2015-07-20 03:08:18 -0500

The usual way would be for Puppet to simple manage the file's desired content, i.e.

In your manifest:

file { '/path/to/your/file.xml':
  ensure => file,
  source => "puppet:///modules/${module_name}/file.xml",
}

Then in file.xml you'd just have:

<Book>
<BOOKNAME> Oracle 12c </BOOKNAME>
<AUTHOR> Richard Gosling </AUTHOR>
<ISBN> 0-07-913702-4</ISBN>
<Publisher> McGraw-Hill</Publisher>
<Price> 39.99</Price>
</Book>

If there's a reason why you must build the file up section by section, then from reading the code here: https://github.com/TERC/puppet-xmlfil...

It looks like they've just copied and modified Puppet's built in file type.

So you'd have code in your manifest like:

xmlfile { '/path/to/your/file.xml':
  ensure => file,
  content => '...your additional content goes here...',
}

I haven't tried this or used this Forge module before; just read the source code. :)

Really, the author should provide better documentation for this module, and you might consider raising an issue against the project.

edit flag offensive delete link more

Comments

Thanks a lot I will try it out.

R gravatar imageR ( 2015-07-20 11:59:11 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-07-19 13:45:14 -0500

Seen: 913 times

Last updated: Jul 22 '15