Ask Your Question
1

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

asked 2015-05-19 19:12:44 -0500

Tony gravatar image

updated 2015-05-20 14:34:18 -0500

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?

<breakfast_menu>

<food>

<name>Belgian Waffles</name>

<price>$5.95</price>

<calories>650</calories>

</food>

</breakfast_menu>

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2015-05-20 12:54:03 -0500

reidmv gravatar image

updated 2015-05-21 18:51:42 -0500

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:

  • ctime (read-only)
  • group
  • mode
  • path
  • mtime (read-only)
  • 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'
  ],
}
edit flag offensive delete link more

Comments

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"', }

Tony gravatar imageTony ( 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.

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

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

Tony gravatar imageTony ( 2015-05-22 08:41:32 -0500 )edit
0

answered 2015-06-01 14:28:10 -0500

souldonut gravatar image

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...

edit flag offensive delete link more

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-05-19 19:12:44 -0500

Seen: 1,679 times

Last updated: Jun 01 '15