# Any example for how to edit XML file by terc-xmlfile

Is there any example for how to edit XML by puppet

There is one module xmlfile. However, anyone can share the sample of puppet script? thank you.

For example, how can the price = 5.95 change to 6.95?

<food>

<name>Belgian Waffles</name>

<price>$5.95</price> <calories>650</calories> </food> </breakfast_menu> edit retag close merge delete ## 2 Answers Sort by » oldest newest most voted Linux Example xmlfile { '/tmp/example.xml': ensure => present, content => file('module/example.xml'), # with the content in the question } xmlfile_modification { 'price': file => '/tmp/example.xml', changes => 'set /breakfast_menu/food/price "$6.95"',
}


Windows Example

xmlfile { 'C:/example_result.xml':
ensure  => present,
content => '<breakfast_menu><food><price>$1.00</price></food></breakfast_menu>', } xmlfile_modification { 'price': file => 'C:/example_result.xml', changes => 'set /breakfast_menu/food/price "$6.95"',
}


From the xmlfile source and the xmlfile_modification source:

xmlfile

Previously an extension of the base file resource type, now a partial reimplementation with the deprecation of :parent. An xmlfile behaves like a file in all ways except that its content can be modified via xmlfile_modification resources. ···· This enables the mixing of exported or virtual content and templated or static content, while managing the end-result as a single resource. ···· The following attributes are inherited from the file type:

• group
• mode
• path
• owner
• selinuxignoredefaults
• selrange
• selrole
• seltype
• seluser
• source

See: http://docs.puppetlabs.com/references... for details

xmlfile_modification

Apply a change or an array of changes constrained by conditionals, to a specified xml file using syntax similar to the augeas XML lens. Changes are applied in memory during content generation when the catalog is applied.

Requires:

• REXML

Sample usage with strings:

xmlfile_modification { "test" :
file    => "/etc/activemq/activemq.conf.xml",
changes => 'set /beans/broker/transportConnectors/transportConnector[last()+1]/#attribute/name "test"',
onlyif  => 'match /beans/broker/transportConnectors/transportConnector[#attribute/name == "test"] size < 1',
}


Sample usage with arrays:

xmlfile_modification { "test" :
file    => "/etc/activemq/activemq.conf.xml",
changes => [
'set /beans/broker/transportConnectors/transportConnector[last()+1]/#attribute/name "tests"',
'set /beans/broker/transportConnectors/transportConnector[last()+1]/#attribute/value "tests"',
],
onlyif =>  [
'match /beans/broker/transportConnectors/transportConnector[#attribute/name == "tests"] size < 1'
],
}

more

Thank you the sample. I tired but the value is not changed. and there is no error message. Any way I can do some debug? thank you. Here is my script. xmlfile_modification { 'price': file => 'd:\\puppet\\myxml.xml', changes => 'set /breakfast_menu/food/price "\$6666"', }

( 2015-05-20 19:48:24 -0500 )edit

I edited the comment to include a Windows-specific example. I had some trouble trying this out on Windows though. I wasn't able to get it to work with backslashed file paths, I had to use forward slashes. It also didn't seem idempotent, always re-creating the file. Works basically but seems buggy.

( 2015-05-21 19:01:43 -0500 )edit

for the windows example, it shows error. Could not set 'present' on ensure: invalid argument.

( 2015-05-22 08:41:32 -0500 )edit

This may be way overkill, but I ended up using the Powershell XML parser to edit XML on Windows. I have one public (and potentially kinda rusty) example that works here:

https://github.com/matthewrstone/pupp...

more