Coder Social home page Coder Social logo

ansible-xml's Introduction

Build Status

ansible-xml

Ansible module for manipulating bits and pieces of XML files and strings.

Requirements

  • Pythons bindings to libxml (usually in a package called python-lxml, install with sudo apt-get install python-lxml or sudo yum install python-lxml).

Notes

  • Pull requests are welcome!
  • This software is available under the terms of the GPLv2 license.
  • Hi there, we have unit tests!

What is XPath?

"XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps."

Basically, it's a syntax which allows you to select a specific, or collection, of elements or attributes in an XML file.

Learn more at the Mozilla Developer Network

Unittests

Also included in this repository are Unittests. Reference these, in addition to the Travis-CI configuration, if you need some more examples.

Examples

Given:

<?xml version='1.0' encoding='UTF-8'?>
<business type="bar">
    <name>Tasty Beverage Co.</name>
    <beers>
        <beer>Rochefort 10</beer>
        <beer>St. Bernardus Abbot 12</beer>
        <beer>Schlitz</beer>
    </beers>
    <rating subjective="true">10</rating>
    <website>
        <mobilefriendly />
        <address>http://tastybeverageco.com</address>
    </website>
</business>

Remove the subjective attribute of the rating element:

xml: file=/foo/bar.xml xpath=/business/rating/@subjective ensure=absent

Set the rating to 11

xml: file=/foo/bar.xml xpath=/business/rating value=11

Get count of beers nodes

xml: file=/foo/bar.xml xpath=/business/beers/beer count=true
  register: hits

debug: var=hits.count

Add a phonenumber element to the business element Implicit mkdir -p behavior where applicable (parent xml nodes created automatically)

xml: file=/foo/bar.xml xpath=/business/phonenumber value=555-555-1234

Add several more beers to the beers element, assuming a vars.yaml file with:

new_beers:
    - beer: "Old Rasputin"
    - beer: "Old Motor Oil"
    - beer: "Old Curmudgeon"

Then the playbook syntax would look like this:

xml: file=/foo/bar.xml xpath=/business/beers add_children={{ new_beers }}

The same, but do it inline

xml:
  file: /foo/bar.xml
  xpath: /business/beers
  add_children:
      - beer: "Old Rasputin"
      - beer: "Old Motor Oil"
      - beer: "Old Curmudgeon"

Add a validxhtml element to the website element. Note that ensure is present by default, and value defaults to null for elements. The result is something like <website><validxhtml />...</website>

xml: file=/foo/bar.xml xpath=/business/website/validxhtml

Add an empty validatedon attribute to the validxhtml element. This actually makes the last example redundant because of the implicit parent-node creation behavior. The result is something like <website><validxhtml validatedon='' />...</website>

xml: file=/foo/bar.xml xpath=/business/website/validxhtml/@validatedon

(1/2) Remove all children from the website element:

xml: file=/foo/bar.xml xpath=/business/website/* ensure=absent

(2/2) Remove all children from the website element:

xml:
  file: /foo/bar.xml
  xpath: /business/website
  children: []

Question? If You have <beers><child01 /><child02 /></beers>

What happens if you say:

xml: file=/foo/bar.xml xpath=/beers

value defaults to an element, so then this would erase the children elements.

ansible-xml's People

Contributors

tbielawa avatar siboulet avatar tdhite avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.