Ansible Module xml: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 159: Line 159:
==Insertion==
==Insertion==
==Update==
==Update==
===Update the Value of an Attribute===
==Deletion==
==Deletion==



Revision as of 00:37, 5 July 2021

External

Internal

Overview

The plugin is named community.general.xml and it offers functionality aimed at interacting with XML files, using XPath.

Example

<box>
    <object name="A" color="red">square</child>
    <object name="B" color="green">circle</child>
    <object name="C" color="blue">triangle</child>
</box>
- name: XML manipulation
  xml:
    path: /tmp/test.xml # the file must exist
    xpath: /box/object[@name='B']

Module Parameters

path (dest, file)

The path to the file to operate on. The file must exists, otherwise the module will fail, with a message similar to: The target XML source '...' does not exist.. The parameter is required unless xmlstring is provided.

- name: test
  xml:
    path: /tmp/test.xml # the file must exist
    ...

xpath

A valid XPath expression describing the file item(s) to manipulate. Operates on the document root, /, by default.

- name: test
  xml:
    ...
    xpath: /box/object[@name='B']
    ...

count

Boolean. Configure the module to provide the xpath match count. By default is turned off.

xmlstring

A string containing in-line XML content on which to operate. This parameter is required, unless path is given.

pretty_print

A boolean value ("yes", "no") that tells whether to pretty print the content of the generated files. By default, pretty printing is turned off.

backup

Boolean (yes, no). Create a backup file including timestamp information so the original file can be recovered in case the transformation is wrong.

namespaces

print_match

Boolean, by default is turned off. Search for a given XPath and print out any matches. This parameter requires xpath to be set.

state

strip_cdata_tags

content: Query with XPath and Specifying what Kind of Result to Provide

Specifies the kind of content we want to get from the matching XPath expression:

xpath must be set.

attribute

The matches result key contains a JSON document listing the matching elements and the value of all their attributes. The count result key contains the number of matching elements.

- name: test
  xml:
    path: test.xml
    xpath: /box/object[@name='B']
    content: 'attribute'
  register: xpath_result

produces the following xpath_result.matches:

[
  {
    "object": {
      "color": "green",
       "name": "B"
    }
  }
]

text

The matches result key contains a JSON document listing the matching elements name and its element text. The count result key contains the number of matching elements.

- name: test
  xml:
    path: test.xml
    xpath: /box/object[@name='B']
    content: 'text'
  register: xpath_result

produces the following xpath_result.matches:

[
  {
    "object": "circle"
  }
]

Updating Attribute Values with attribute/value Usage Pattern

Use the xpath to match the element(s), use the attribute element to specify the element whose value must be modified or deleted, and use the value element to provide the new value (desired state) as a string. Use None to remove the attribute. The update is idempotent.

Updating an attribute:

- name: test
  xml:
    file: test.xml
    xpath: /box/object[@name='B']
    attribute: 'color'
    value: 'fuchsia'

Removing an attribute: TODO, did not work.

attribute

Specify the name of the attribute to select, as string, when using parameter value. Do not prepend with '@'.

value

Configuration element useful when updating or deleting XML content. value represents the desired state of the attribute specified in attribute. It could be either a string, or to unset a value, the Python None keyword (YAML Equivalent, null).

Elements default to no value (but present). Attributes default to an empty string.

Children Manipulation

Children can be added with add_children or updated with set_children:

input_type

"xml" or "yaml": the input type for add_children, set_children.

add_children

Add additional child element(s) to the element that matched xpath. The elements to be added must be given in a list. Each item may be either a string (example: children=ansible to add an empty <ansible/> child or a hash where the key is an element name and the value is the element value. xpath must be set if add_children is used.

- name: test
  xml:
    file: test.xml
    xpath: /box
    pretty_print: true
    add_children:
      - object: rectangle
      - object: ellipse

This will modify the file and add the following content:

<box>
...
<object>rectangle</object><object>ellipse</object></box>

The output is not "pretty printed", to pretty print, use pretty_print: yes

How to add elements with attributes, and recursive elements?

set_children

Set the child element(s) of a selected element for a given xpath. Removes any existing children. Child elements must be specified as in add_children.

insertbefore

Add additional child element(s) before the first selected element for a given xpath. Child elements must be given in a list and each item may be either a string (eg. children=ansible to add an empty <ansible/> child element), or a hash where the key is an element name and the value is the element value. This parameter requires xpath to be set.

insertafter

Add additional child element(s) after the last selected element for a given xpath. Child elements must be given in a list and each item may be either a string (eg. children=ansible to add an empty <ansible/> child element), or a hash where the key is an element name and the value is the element value. This parameter requires xpath to be set.

Use Cases

Query

Insertion

Update

Update the Value of an Attribute

Deletion

- name: Bump Java build heap size to {{build.jvm.heap_max}} MB
  xml:
    path: "{{ansible_env.PROJECT_ROOT}}/.idea/compiler.xml"
    xpath: /project/component[@name='CompilerConfiguration']/option[@name='BUILD_PROCESS_HEAP_SIZE']
    attribute: "value"
    value: "{{build.jvm.heap_max}}"
- name: Set Eclipse as default compiler
  xml:
    path: "{{idea_path}}/.idea/compiler.xml"
    xpath: /project/component[@name='CompilerConfiguration']/option[@name='DEFAULT_COMPILER']
    attribute: "value"
    value: "Eclipse"