Ansible Module xml: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 33: Line 33:
===<tt>attribute</tt>===
===<tt>attribute</tt>===
The <code>matches</code> result key contains a JSON document listing the matching elements and the value of all their attributes. The <code>count</code> result key contains the number of matching elements.
The <code>matches</code> result key contains a JSON document listing the matching elements and the value of all their attributes. The <code>count</code> result key contains the number of matching elements.
<syntaxhighlight lang='yaml'>
- name: test
  xml:
    path: test.xml
    xpath: /root/child[@name='B']
    content: attribute
  register: xpath_result
</syntaxhighlight>
produces the following <code>xpath_result.matches</code>:
<syntaxhighlight lang=JSON>
{
  "child": {
    "color": "green",
    "name": "B"
  }
}
</syntaxhighlight>


===<tt>text</tt>===
===<tt>text</tt>===

Revision as of 21:19, 4 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

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

Module Parameters

path

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.

xpath

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

content

Return the content of the matching XPath expression. xpath must be set. It can have to values:

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: /root/child[@name='B']
    content: attribute
  register: xpath_result

produces the following xpath_result.matches:

{
  "child": {
    "color": "green",
     "name": "B"
  }
}

text

xmlstring

Use Cases

Query

Insertion

Update

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"