New module: Support manipulating XML files (#25323)
* Import original unmodified upstream version This is another attempt to get the xml module upstream. https://github.com/cmprescott/ansible-xml/ This is the original file from upstream, without commit 1e7a3f6b6e2bc01aa9cebfd80ac5cd4555032774 * Add additional changes required for upstreaming This PR includes the following changes: - Clean up of DOCUMENTATION - Rename "ensure" parameter to "state" parameter (kept alias) - Added EXAMPLES - Remove explicit type-case using str() for formatting - Clean up AnsibleModule parameter handling - Retained Python 2.4 compatibility - PEP8 compliancy - Various fixes as suggested by abadger during first review This fixes cmprescott/ansible-xml#108 * Added original integration tests There is some room for improvement wrt. idempotency and check-mode testing. * Some tests depend on lxml v3.0alpha1 or higher We are now expecting lxml v2.3.0 or higher. We skips tests if lxml is too old. Plus small fix. * Relicense to GPLv3+ header All past contributors have agreed to relicense this module to GPLv2+, and GPLv3 specifically. See: https://github.com/cmprescott/ansible-xml/issues/113 This fixes cmprescott/ansible-xml#73 * Fix small typo in integration tests * Python 3 support This PR also includes: - Python 3 support - Documentation fixes - Check-mode fixes and improvements - Bugfix in check-mode support - Always return xmlstring, even if there's no change - Check for lxml 2.3.0 or newer * Add return values * Various fixes after review
This commit is contained in:
parent
923445a484
commit
6874ba23ff
61 changed files with 2003 additions and 0 deletions
|
@ -302,6 +302,7 @@ Ansible Changes By Release
|
||||||
* win_route
|
* win_route
|
||||||
* win_security_policy
|
* win_security_policy
|
||||||
* win_wakeonlan
|
* win_wakeonlan
|
||||||
|
- xml
|
||||||
|
|
||||||
<a id="2.3"></a>
|
<a id="2.3"></a>
|
||||||
|
|
||||||
|
|
748
lib/ansible/modules/files/xml.py
Executable file
748
lib/ansible/modules/files/xml.py
Executable file
|
@ -0,0 +1,748 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2014, Red Hat, Inc.
|
||||||
|
# Tim Bielawa <tbielawa@redhat.com>
|
||||||
|
# Magnus Hedemark <mhedemar@redhat.com>
|
||||||
|
# Copyright 2017, Dag Wieers <dag@wieers.com>
|
||||||
|
#
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# Make coding more python3-ish
|
||||||
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
||||||
|
'status': ['preview'],
|
||||||
|
'supported_by': 'community'}
|
||||||
|
|
||||||
|
DOCUMENTATION = r'''
|
||||||
|
---
|
||||||
|
module: xml
|
||||||
|
short_description: Manage bits and pieces of XML files or strings
|
||||||
|
description:
|
||||||
|
- A CRUD-like interface to managing bits of XML files.
|
||||||
|
- You might also be interested in a brief tutorial from U(http://www.w3schools.com/xpath/).
|
||||||
|
version_added: '2.4'
|
||||||
|
options:
|
||||||
|
path:
|
||||||
|
description:
|
||||||
|
- Path to the file to operate on. File must exist ahead of time.
|
||||||
|
- This parameter is required, unless C(xmlstring) is given.
|
||||||
|
required: yes
|
||||||
|
aliases: [ dest, file ]
|
||||||
|
xmlstring:
|
||||||
|
description:
|
||||||
|
- A string containing XML on which to operate.
|
||||||
|
- This parameter is required, unless C(path) is given.
|
||||||
|
required: yes
|
||||||
|
xpath:
|
||||||
|
description:
|
||||||
|
- A valid XPath expression describing the item(s) you want to manipulate.
|
||||||
|
- Operates on the document root, C(/), by default.
|
||||||
|
default: /
|
||||||
|
namespaces:
|
||||||
|
description:
|
||||||
|
- The namespace C(prefix:uri) mapping for the XPath expression.
|
||||||
|
- Needs to be a C(dict), not a C(list) of items.
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Set or remove an xpath selection (node(s), attribute(s)).
|
||||||
|
default: present
|
||||||
|
choices: [ absent, present ]
|
||||||
|
aliases: [ ensure ]
|
||||||
|
value:
|
||||||
|
description:
|
||||||
|
- Desired state of the selected attribute.
|
||||||
|
- Either a string, or to unset a value, the Python C(None) keyword (YAML Equivalent, C(null)).
|
||||||
|
- Elements default to no value (but present).
|
||||||
|
- Attributes default to an empty string.
|
||||||
|
add_children:
|
||||||
|
description:
|
||||||
|
- Add additional child-element(s) to a selected element.
|
||||||
|
- Child elements must be given in a list and each item may be either a string
|
||||||
|
(eg. C(children=ansible) to add an empty C(<ansible/>) child element),
|
||||||
|
or a hash where the key is an element name and the value is the element value.
|
||||||
|
set_children:
|
||||||
|
description:
|
||||||
|
- Set the the child-element(s) of a selected element.
|
||||||
|
- Removes any existing children.
|
||||||
|
- Child elements must be specified as in C(add_children).
|
||||||
|
count:
|
||||||
|
description:
|
||||||
|
- Search for a given C(xpath) and provide the count of any matches.
|
||||||
|
type: bool
|
||||||
|
default: 'no'
|
||||||
|
print_match:
|
||||||
|
description:
|
||||||
|
- Search for a given C(xpath) and print out any matches.
|
||||||
|
type: bool
|
||||||
|
default: 'no'
|
||||||
|
pretty_print:
|
||||||
|
description:
|
||||||
|
- Pretty print XML output.
|
||||||
|
type: bool
|
||||||
|
default: 'no'
|
||||||
|
content:
|
||||||
|
description:
|
||||||
|
- Search for a given C(xpath) and get content.
|
||||||
|
choices: [ attribute, text ]
|
||||||
|
input_type:
|
||||||
|
description:
|
||||||
|
- Type of input for C(add_children) and C(set_children).
|
||||||
|
choices: [ xml, yaml ]
|
||||||
|
default: yaml
|
||||||
|
requirements:
|
||||||
|
- lxml >= 2.3.0
|
||||||
|
notes:
|
||||||
|
- This module does not handle complicated xpath expressions, so limit xpath selectors to simple expressions.
|
||||||
|
- Beware that in case your XML elements are namespaced, you need to use the C(namespaces) parameter.
|
||||||
|
author:
|
||||||
|
- Tim Bielawa (@tbielawa)
|
||||||
|
- Magnus Hedemark (@magnus919)
|
||||||
|
- Dag Wieers (@dagwieers)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = r'''
|
||||||
|
- name: Remove the subjective attribute of the rating element
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/rating/@subjective
|
||||||
|
state: absent
|
||||||
|
|
||||||
|
- name: Set the rating to 11
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
value: 11
|
||||||
|
|
||||||
|
# Retrieve and display the number of nodes
|
||||||
|
- name: Get count of beers nodes
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/beers/beer
|
||||||
|
count: yes
|
||||||
|
register: hits
|
||||||
|
|
||||||
|
- debug:
|
||||||
|
var: hits.count
|
||||||
|
|
||||||
|
- name: Add a phonenumber element to the business element
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/phonenumber
|
||||||
|
value: 555-555-1234
|
||||||
|
|
||||||
|
- name: Add several more beers to the beers element
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
add_children:
|
||||||
|
- beer: Old Rasputin
|
||||||
|
- beer: Old Motor Oil
|
||||||
|
- beer: Old Curmudgeon
|
||||||
|
|
||||||
|
- name: Add a validxhtml element to the website element
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/website/validxhtml
|
||||||
|
|
||||||
|
- name: Add an empty validatedon attribute to the validxhtml element
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/website/validxhtml/@validatedon
|
||||||
|
|
||||||
|
- name: Remove all children from the website element (option 1)
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/website/*
|
||||||
|
state: absent
|
||||||
|
|
||||||
|
- name: Remove all children from the website element (option 2)
|
||||||
|
xml:
|
||||||
|
path: /foo/bar.xml
|
||||||
|
xpath: /business/website
|
||||||
|
children: []
|
||||||
|
'''
|
||||||
|
|
||||||
|
RETURN = r'''
|
||||||
|
actions:
|
||||||
|
description: A dictionary with the original xpath, namespaces and state.
|
||||||
|
type: dict
|
||||||
|
returned: success
|
||||||
|
sample: {xpath: xpath, namespaces: [namespace1, namespace2], state=present}
|
||||||
|
count:
|
||||||
|
description: The count of xpath matches.
|
||||||
|
type: int
|
||||||
|
returned: when parameter 'count' is set
|
||||||
|
sample: 2
|
||||||
|
matches:
|
||||||
|
description: The xpath matches found.
|
||||||
|
type: list
|
||||||
|
returned: when parameter 'print_match' is set
|
||||||
|
msg:
|
||||||
|
description: A message related to the performed action(s).
|
||||||
|
type: string
|
||||||
|
returned: always
|
||||||
|
xmlstring:
|
||||||
|
description: An XML string of the resulting output.
|
||||||
|
type: string
|
||||||
|
returned: when parameter 'xmlstring' is set
|
||||||
|
'''
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from collections import MutableMapping
|
||||||
|
from distutils.version import LooseVersion
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
|
||||||
|
HAS_LXML = True
|
||||||
|
try:
|
||||||
|
from lxml import etree
|
||||||
|
except ImportError:
|
||||||
|
HAS_LXML = False
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule, json_dict_bytes_to_unicode
|
||||||
|
from ansible.module_utils.six import iteritems, string_types
|
||||||
|
from ansible.module_utils._text import to_bytes, to_native
|
||||||
|
|
||||||
|
_IDENT = "[a-zA-Z-][a-zA-Z0-9_\-\.]*"
|
||||||
|
_NSIDENT = _IDENT + "|" + _IDENT + ":" + _IDENT
|
||||||
|
# Note: we can't reasonably support the 'if you need to put both ' and " in a string, concatenate
|
||||||
|
# strings wrapped by the other delimiter' XPath trick, especially as simple XPath.
|
||||||
|
_XPSTR = "('(?:.*)'|\"(?:.*)\")"
|
||||||
|
|
||||||
|
_RE_SPLITSIMPLELAST = re.compile("^(.*)/(" + _NSIDENT + ")$")
|
||||||
|
_RE_SPLITSIMPLELASTEQVALUE = re.compile("^(.*)/(" + _NSIDENT + ")/text\\(\\)=" + _XPSTR + "$")
|
||||||
|
_RE_SPLITSIMPLEATTRLAST = re.compile("^(.*)/(@(?:" + _NSIDENT + "))$")
|
||||||
|
_RE_SPLITSIMPLEATTRLASTEQVALUE = re.compile("^(.*)/(@(?:" + _NSIDENT + "))=" + _XPSTR + "$")
|
||||||
|
_RE_SPLITSUBLAST = re.compile("^(.*)/(" + _NSIDENT + ")\\[(.*)\\]$")
|
||||||
|
_RE_SPLITONLYEQVALUE = re.compile("^(.*)/text\\(\\)=" + _XPSTR + "$")
|
||||||
|
|
||||||
|
|
||||||
|
def print_match(module, tree, xpath, namespaces):
|
||||||
|
match = tree.xpath(xpath, namespaces=namespaces)
|
||||||
|
match_xpaths = []
|
||||||
|
for m in match:
|
||||||
|
match_xpaths.append(tree.getpath(m))
|
||||||
|
match_str = json.dumps(match_xpaths)
|
||||||
|
msg = "selector '%s' match: %s" % (xpath, match_str)
|
||||||
|
finish(module, tree, xpath, namespaces, changed=False, msg=msg)
|
||||||
|
|
||||||
|
|
||||||
|
def count_nodes(module, tree, xpath, namespaces):
|
||||||
|
""" Return the count of nodes matching the xpath """
|
||||||
|
hits = tree.xpath("count(/%s)" % xpath, namespaces=namespaces)
|
||||||
|
finish(module, tree, xpath, namespaces, changed=False, msg=int(hits), hitcount=int(hits))
|
||||||
|
|
||||||
|
|
||||||
|
def is_node(tree, xpath, namespaces):
|
||||||
|
""" Test if a given xpath matches anything and if that match is a node.
|
||||||
|
|
||||||
|
For now we just assume you're only searching for one specific thing."""
|
||||||
|
if xpath_matches(tree, xpath, namespaces):
|
||||||
|
# OK, it found something
|
||||||
|
match = tree.xpath(xpath, namespaces=namespaces)
|
||||||
|
if isinstance(match[0], etree._Element):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def is_attribute(tree, xpath, namespaces):
|
||||||
|
""" Test if a given xpath matches and that match is an attribute
|
||||||
|
|
||||||
|
An xpath attribute search will only match one item"""
|
||||||
|
if xpath_matches(tree, xpath, namespaces):
|
||||||
|
match = tree.xpath(xpath, namespaces=namespaces)
|
||||||
|
if isinstance(match[0], etree._ElementStringResult):
|
||||||
|
return True
|
||||||
|
elif isinstance(match[0], etree._ElementUnicodeResult):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def xpath_matches(tree, xpath, namespaces):
|
||||||
|
""" Test if a node exists """
|
||||||
|
if tree.xpath(xpath, namespaces=namespaces):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def delete_xpath_target(module, tree, xpath, namespaces):
|
||||||
|
""" Delete an attribute or element from a tree """
|
||||||
|
try:
|
||||||
|
for result in tree.xpath(xpath, namespaces=namespaces):
|
||||||
|
# Get the xpath for this result
|
||||||
|
if is_attribute(tree, xpath, namespaces):
|
||||||
|
# Delete an attribute
|
||||||
|
parent = result.getparent()
|
||||||
|
# Pop this attribute match out of the parent
|
||||||
|
# node's 'attrib' dict by using this match's
|
||||||
|
# 'attrname' attribute for the key
|
||||||
|
parent.attrib.pop(result.attrname)
|
||||||
|
elif is_node(tree, xpath, namespaces):
|
||||||
|
# Delete an element
|
||||||
|
result.getparent().remove(result)
|
||||||
|
else:
|
||||||
|
raise Exception("Impossible error")
|
||||||
|
except Exception as e:
|
||||||
|
module.fail_json(msg="Couldn't delete xpath target: %s (%s)" % (xpath, e))
|
||||||
|
else:
|
||||||
|
finish(module, tree, xpath, namespaces, changed=True)
|
||||||
|
|
||||||
|
|
||||||
|
def replace_children_of(children, match):
|
||||||
|
for element in match.getchildren():
|
||||||
|
match.remove(element)
|
||||||
|
match.extend(children)
|
||||||
|
|
||||||
|
|
||||||
|
def set_target_children_inner(module, tree, xpath, namespaces, children, in_type):
|
||||||
|
matches = tree.xpath(xpath, namespaces=namespaces)
|
||||||
|
|
||||||
|
# Create a list of our new children
|
||||||
|
children = children_to_nodes(module, children, in_type)
|
||||||
|
children_as_string = [etree.tostring(c) for c in children]
|
||||||
|
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
# xpaths always return matches as a list, so....
|
||||||
|
for match in matches:
|
||||||
|
# Check if elements differ
|
||||||
|
if len(match.getchildren()) == len(children):
|
||||||
|
for idx, element in enumerate(match.getchildren()):
|
||||||
|
if etree.tostring(element) != children_as_string[idx]:
|
||||||
|
replace_children_of(children, match)
|
||||||
|
changed = True
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
replace_children_of(children, match)
|
||||||
|
changed = True
|
||||||
|
|
||||||
|
return changed
|
||||||
|
|
||||||
|
|
||||||
|
def set_target_children(module, tree, xpath, namespaces, children, in_type):
|
||||||
|
changed = set_target_children_inner(module, tree, xpath, namespaces, children, in_type)
|
||||||
|
# Write it out
|
||||||
|
finish(module, tree, xpath, namespaces, changed=changed)
|
||||||
|
|
||||||
|
|
||||||
|
def add_target_children(module, tree, xpath, namespaces, children, in_type):
|
||||||
|
if is_node(tree, xpath, namespaces):
|
||||||
|
new_kids = children_to_nodes(module, children, in_type)
|
||||||
|
for node in tree.xpath(xpath, namespaces=namespaces):
|
||||||
|
node.extend(new_kids)
|
||||||
|
finish(module, tree, xpath, namespaces, changed=True)
|
||||||
|
else:
|
||||||
|
finish(module, tree, xpath, namespaces)
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_xpstr(g):
|
||||||
|
return g[1:-1]
|
||||||
|
|
||||||
|
|
||||||
|
def split_xpath_last(xpath):
|
||||||
|
"""split an XPath of the form /foo/bar/baz into /foo/bar and baz"""
|
||||||
|
xpath = xpath.strip()
|
||||||
|
m = _RE_SPLITSIMPLELAST.match(xpath)
|
||||||
|
if m:
|
||||||
|
# requesting an element to exist
|
||||||
|
return (m.group(1), [(m.group(2), None)])
|
||||||
|
m = _RE_SPLITSIMPLELASTEQVALUE.match(xpath)
|
||||||
|
if m:
|
||||||
|
# requesting an element to exist with an inner text
|
||||||
|
return (m.group(1), [(m.group(2), _extract_xpstr(m.group(3)))])
|
||||||
|
|
||||||
|
m = _RE_SPLITSIMPLEATTRLAST.match(xpath)
|
||||||
|
if m:
|
||||||
|
# requesting an attribute to exist
|
||||||
|
return (m.group(1), [(m.group(2), None)])
|
||||||
|
m = _RE_SPLITSIMPLEATTRLASTEQVALUE.match(xpath)
|
||||||
|
if m:
|
||||||
|
# requesting an attribute to exist with a value
|
||||||
|
return (m.group(1), [(m.group(2), _extract_xpstr(m.group(3)))])
|
||||||
|
|
||||||
|
m = _RE_SPLITSUBLAST.match(xpath)
|
||||||
|
if m:
|
||||||
|
content = [x.strip() for x in m.group(3).split(" and ")]
|
||||||
|
return (m.group(1), [('/' + m.group(2), content)])
|
||||||
|
|
||||||
|
m = _RE_SPLITONLYEQVALUE.match(xpath)
|
||||||
|
if m:
|
||||||
|
# requesting a change of inner text
|
||||||
|
return (m.group(1), [("", _extract_xpstr(m.group(2)))])
|
||||||
|
return (xpath, [])
|
||||||
|
|
||||||
|
|
||||||
|
def nsnameToClark(name, namespaces):
|
||||||
|
if ":" in name:
|
||||||
|
(nsname, rawname) = name.split(":")
|
||||||
|
# return "{{%s}}%s" % (namespaces[nsname], rawname)
|
||||||
|
return "{{{0}}}{1}".format(namespaces[nsname], rawname)
|
||||||
|
else:
|
||||||
|
# no namespace name here
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
def check_or_make_target(module, tree, xpath, namespaces):
|
||||||
|
(inner_xpath, changes) = split_xpath_last(xpath)
|
||||||
|
if (inner_xpath == xpath) or (changes is None):
|
||||||
|
module.fail_json(msg="Can't process Xpath %s in order to spawn nodes! tree is %s" %
|
||||||
|
(xpath, etree.tostring(tree, pretty_print=True)))
|
||||||
|
return False
|
||||||
|
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
if not is_node(tree, inner_xpath, namespaces):
|
||||||
|
changed = check_or_make_target(module, tree, inner_xpath, namespaces)
|
||||||
|
|
||||||
|
# we test again after calling check_or_make_target
|
||||||
|
if is_node(tree, inner_xpath, namespaces) and changes:
|
||||||
|
for (eoa, eoa_value) in changes:
|
||||||
|
if eoa and eoa[0] != '@' and eoa[0] != '/':
|
||||||
|
# implicitly creating an element
|
||||||
|
new_kids = children_to_nodes(module, [nsnameToClark(eoa, namespaces)], "yaml")
|
||||||
|
if eoa_value:
|
||||||
|
for nk in new_kids:
|
||||||
|
nk.text = eoa_value
|
||||||
|
|
||||||
|
for node in tree.xpath(inner_xpath, namespaces=namespaces):
|
||||||
|
node.extend(new_kids)
|
||||||
|
changed = True
|
||||||
|
# module.fail_json(msg="now tree=%s" % etree.tostring(tree, pretty_print=True))
|
||||||
|
elif eoa and eoa[0] == '/':
|
||||||
|
element = eoa[1:]
|
||||||
|
new_kids = children_to_nodes(module, [nsnameToClark(element, namespaces)], "yaml")
|
||||||
|
for node in tree.xpath(inner_xpath, namespaces=namespaces):
|
||||||
|
node.extend(new_kids)
|
||||||
|
for nk in new_kids:
|
||||||
|
for subexpr in eoa_value:
|
||||||
|
# module.fail_json(msg="element=%s subexpr=%s node=%s now tree=%s" %
|
||||||
|
# (element, subexpr, etree.tostring(node, pretty_print=True), etree.tostring(tree, pretty_print=True))
|
||||||
|
check_or_make_target(module, nk, "./" + subexpr, namespaces)
|
||||||
|
changed = True
|
||||||
|
|
||||||
|
# module.fail_json(msg="now tree=%s" % etree.tostring(tree, pretty_print=True))
|
||||||
|
elif eoa == "":
|
||||||
|
for node in tree.xpath(inner_xpath, namespaces=namespaces):
|
||||||
|
if (node.text != eoa_value):
|
||||||
|
node.text = eoa_value
|
||||||
|
changed = True
|
||||||
|
|
||||||
|
elif eoa and eoa[0] == '@':
|
||||||
|
attribute = nsnameToClark(eoa[1:], namespaces)
|
||||||
|
|
||||||
|
for element in tree.xpath(inner_xpath, namespaces=namespaces):
|
||||||
|
changing = (attribute not in element.attrib or element.attrib[attribute] != eoa_value)
|
||||||
|
|
||||||
|
if changing:
|
||||||
|
changed = changed or changing
|
||||||
|
if eoa_value is None:
|
||||||
|
value = ""
|
||||||
|
else:
|
||||||
|
value = eoa_value
|
||||||
|
element.attrib[attribute] = value
|
||||||
|
|
||||||
|
# module.fail_json(msg="arf %s changing=%s as curval=%s changed tree=%s" %
|
||||||
|
# (xpath, changing, etree.tostring(tree, changing, element[attribute], pretty_print=True)))
|
||||||
|
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="unknown tree transformation=%s" % etree.tostring(tree, pretty_print=True))
|
||||||
|
|
||||||
|
return changed
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_xpath_exists(module, tree, xpath, namespaces):
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
if not is_node(tree, xpath, namespaces):
|
||||||
|
changed = check_or_make_target(module, tree, xpath, namespaces)
|
||||||
|
|
||||||
|
finish(module, tree, xpath, namespaces, changed)
|
||||||
|
|
||||||
|
|
||||||
|
def set_target_inner(module, tree, xpath, namespaces, attribute, value):
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not is_node(tree, xpath, namespaces):
|
||||||
|
changed = check_or_make_target(module, tree, xpath, namespaces)
|
||||||
|
except Exception as e:
|
||||||
|
module.fail_json(msg="Xpath %s causes a failure: %s\n -- tree is %s" %
|
||||||
|
(xpath, e, etree.tostring(tree, pretty_print=True)), exception=traceback.format_exc(e))
|
||||||
|
|
||||||
|
if not is_node(tree, xpath, namespaces):
|
||||||
|
module.fail_json(msg="Xpath %s does not reference a node! tree is %s" %
|
||||||
|
(xpath, etree.tostring(tree, pretty_print=True)))
|
||||||
|
|
||||||
|
for element in tree.xpath(xpath, namespaces=namespaces):
|
||||||
|
if not attribute:
|
||||||
|
changed = changed or (element.text != value)
|
||||||
|
if element.text != value:
|
||||||
|
element.text = value
|
||||||
|
else:
|
||||||
|
changed = changed or (element.get(attribute) != value)
|
||||||
|
if ":" in attribute:
|
||||||
|
attr_ns, attr_name = attribute.split(":")
|
||||||
|
# attribute = "{{%s}}%s" % (namespaces[attr_ns], attr_name)
|
||||||
|
attribute = "{{{0}}}{1}".format(namespaces[attr_ns], attr_name)
|
||||||
|
if element.get(attribute) != value:
|
||||||
|
element.set(attribute, value)
|
||||||
|
|
||||||
|
return changed
|
||||||
|
|
||||||
|
|
||||||
|
def set_target(module, tree, xpath, namespaces, attribute, value):
|
||||||
|
changed = set_target_inner(module, tree, xpath, namespaces, attribute, value)
|
||||||
|
finish(module, tree, xpath, namespaces, changed)
|
||||||
|
|
||||||
|
|
||||||
|
def pretty(module, tree):
|
||||||
|
xml_string = etree.tostring(tree, xml_declaration=True, encoding='UTF-8', pretty_print=module.params['pretty_print'])
|
||||||
|
|
||||||
|
result = dict(
|
||||||
|
changed=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
if module.params['path']:
|
||||||
|
xml_file = module.params['path']
|
||||||
|
xml_content = open(xml_file)
|
||||||
|
try:
|
||||||
|
if xml_string != xml_content.read():
|
||||||
|
result['changed'] = True
|
||||||
|
if not module.check_mode:
|
||||||
|
tree.write(xml_file, xml_declaration=True, encoding='UTF-8', pretty_print=module.params['pretty_print'])
|
||||||
|
finally:
|
||||||
|
xml_content.close()
|
||||||
|
|
||||||
|
elif module.params['xmlstring']:
|
||||||
|
result['xmlstring'] = xml_string
|
||||||
|
if xml_string != module.params['xmlstring']:
|
||||||
|
result['changed'] = True
|
||||||
|
|
||||||
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
|
def get_element_text(module, tree, xpath, namespaces):
|
||||||
|
if not is_node(tree, xpath, namespaces):
|
||||||
|
module.fail_json(msg="Xpath %s does not reference a node!" % xpath)
|
||||||
|
|
||||||
|
elements = []
|
||||||
|
for element in tree.xpath(xpath, namespaces=namespaces):
|
||||||
|
elements.append({element.tag: element.text})
|
||||||
|
|
||||||
|
finish(module, tree, xpath, namespaces, changed=False, msg=len(elements), hitcount=len(elements), matches=elements)
|
||||||
|
|
||||||
|
|
||||||
|
def get_element_attr(module, tree, xpath, namespaces):
|
||||||
|
if not is_node(tree, xpath, namespaces):
|
||||||
|
module.fail_json(msg="Xpath %s does not reference a node!" % xpath)
|
||||||
|
|
||||||
|
elements = []
|
||||||
|
for element in tree.xpath(xpath, namespaces=namespaces):
|
||||||
|
child = {}
|
||||||
|
for key in element.keys():
|
||||||
|
value = element.get(key)
|
||||||
|
child.update({key: value})
|
||||||
|
elements.append({element.tag: child})
|
||||||
|
|
||||||
|
finish(module, tree, xpath, namespaces, changed=False, msg=len(elements), hitcount=len(elements), matches=elements)
|
||||||
|
|
||||||
|
|
||||||
|
def child_to_element(module, child, in_type):
|
||||||
|
if in_type == 'xml':
|
||||||
|
infile = BytesIO(to_bytes(child, errors='surrogate_or_strict'))
|
||||||
|
|
||||||
|
try:
|
||||||
|
parser = etree.XMLParser()
|
||||||
|
node = etree.parse(infile, parser)
|
||||||
|
return node.getroot()
|
||||||
|
except etree.XMLSyntaxError as e:
|
||||||
|
module.fail_json(msg="Error while parsing child element: %s" % e)
|
||||||
|
elif in_type == 'yaml':
|
||||||
|
if isinstance(child, string_types):
|
||||||
|
return etree.Element(child)
|
||||||
|
elif isinstance(child, MutableMapping):
|
||||||
|
if len(child) > 1:
|
||||||
|
module.fail_json(msg="Can only create children from hashes with one key")
|
||||||
|
|
||||||
|
(key, value) = next(iteritems(child))
|
||||||
|
if isinstance(value, MutableMapping):
|
||||||
|
children = value.pop('_', None)
|
||||||
|
|
||||||
|
node = etree.Element(key, value)
|
||||||
|
|
||||||
|
if children is not None:
|
||||||
|
if not isinstance(children, list):
|
||||||
|
module.fail_json(msg="Invalid children type: %s, must be list." % type(children))
|
||||||
|
|
||||||
|
subnodes = children_to_nodes(module, children)
|
||||||
|
node.extend(subnodes)
|
||||||
|
else:
|
||||||
|
node = etree.Element(key)
|
||||||
|
node.text = value
|
||||||
|
return node
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="Invalid child type: %s. Children must be either strings or hashes." % type(child))
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="Invalid child input type: %s. Type must be either xml or yaml." % in_type)
|
||||||
|
|
||||||
|
|
||||||
|
def children_to_nodes(module=None, children=[], type='yaml'):
|
||||||
|
"""turn a str/hash/list of str&hash into a list of elements"""
|
||||||
|
return [child_to_element(module, child, type) for child in children]
|
||||||
|
|
||||||
|
|
||||||
|
def finish(module, tree, xpath, namespaces, changed=False, msg="", hitcount=0, matches=tuple()):
|
||||||
|
actions = dict(xpath=xpath, namespaces=namespaces, state=module.params['state'])
|
||||||
|
|
||||||
|
if not changed:
|
||||||
|
module.exit_json(changed=changed, actions=actions, msg=msg, count=hitcount, matches=matches)
|
||||||
|
|
||||||
|
if module.params['path']:
|
||||||
|
if not module.check_mode:
|
||||||
|
tree.write(module.params['path'], xml_declaration=True, encoding='UTF-8', pretty_print=module.params['pretty_print'])
|
||||||
|
module.exit_json(changed=changed, actions=actions, msg=msg, count=hitcount, matches=matches)
|
||||||
|
|
||||||
|
if module.params['xmlstring']:
|
||||||
|
xml_string = etree.tostring(tree, xml_declaration=True, encoding='UTF-8', pretty_print=module.params['pretty_print'])
|
||||||
|
module.exit_json(changed=changed, actions=actions, msg=msg, count=hitcount, matches=matches, xmlstring=xml_string)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
module = AnsibleModule(
|
||||||
|
argument_spec=dict(
|
||||||
|
path=dict(type='path', aliases=['dest', 'file']),
|
||||||
|
xmlstring=dict(type='str'),
|
||||||
|
xpath=dict(type='str', default='/'),
|
||||||
|
namespaces=dict(type='dict', default={}),
|
||||||
|
state=dict(type='str', default='present', choices=['absent', 'present'], aliases=['ensure']),
|
||||||
|
value=dict(),
|
||||||
|
attribute=dict(),
|
||||||
|
add_children=dict(type='list'),
|
||||||
|
set_children=dict(type='list'),
|
||||||
|
count=dict(type='bool', default=False),
|
||||||
|
print_match=dict(type='bool', default=False),
|
||||||
|
pretty_print=dict(type='bool', default=False),
|
||||||
|
content=dict(type='str', choices=['attribute', 'text']),
|
||||||
|
input_type=dict(type='str', default='yaml', choices=['xml', 'yaml'])
|
||||||
|
),
|
||||||
|
supports_check_mode=True,
|
||||||
|
mutually_exclusive=[
|
||||||
|
['value', 'set_children'],
|
||||||
|
['value', 'add_children'],
|
||||||
|
['set_children', 'add_children'],
|
||||||
|
['path', 'xmlstring'],
|
||||||
|
['content', 'set_children'],
|
||||||
|
['content', 'add_children'],
|
||||||
|
['content', 'value'],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
xml_file = module.params['path']
|
||||||
|
xml_string = module.params['xmlstring']
|
||||||
|
xpath = module.params['xpath']
|
||||||
|
namespaces = module.params['namespaces']
|
||||||
|
state = module.params['state']
|
||||||
|
value = json_dict_bytes_to_unicode(module.params['value'])
|
||||||
|
attribute = module.params['attribute']
|
||||||
|
set_children = json_dict_bytes_to_unicode(module.params['set_children'])
|
||||||
|
add_children = json_dict_bytes_to_unicode(module.params['add_children'])
|
||||||
|
pretty_print = module.params['pretty_print']
|
||||||
|
content = module.params['content']
|
||||||
|
input_type = module.params['input_type']
|
||||||
|
print_match = module.params['print_match']
|
||||||
|
count = module.params['count']
|
||||||
|
|
||||||
|
# Check if we have lxml 2.3.0 or newer installed
|
||||||
|
if not HAS_LXML:
|
||||||
|
module.fail_json(msg='The xml ansible module requires the lxml python library installed on the managed machine')
|
||||||
|
elif LooseVersion('.'.join(to_native(f) for f in etree.LXML_VERSION)) < LooseVersion('2.3.0'):
|
||||||
|
module.fail_json(msg='The xml ansible module requires lxml 2.3.0 or newer installed on the managed machine')
|
||||||
|
elif LooseVersion('.'.join(to_native(f) for f in etree.LXML_VERSION)) < LooseVersion('3.0.0'):
|
||||||
|
module.warn('Using lxml version lower than 3.0.0 does not guarantee predictable element attribute order.')
|
||||||
|
|
||||||
|
# Check if the file exists
|
||||||
|
if xml_string:
|
||||||
|
infile = BytesIO(to_bytes(xml_string, errors='surrogate_or_strict'))
|
||||||
|
elif os.path.isfile(xml_file):
|
||||||
|
infile = open(xml_file, 'rb')
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="The target XML source '%s' does not exist." % xml_file)
|
||||||
|
|
||||||
|
# Try to parse in the target XML file
|
||||||
|
try:
|
||||||
|
parser = etree.XMLParser(remove_blank_text=pretty_print)
|
||||||
|
doc = etree.parse(infile, parser)
|
||||||
|
except etree.XMLSyntaxError as e:
|
||||||
|
module.fail_json(msg="Error while parsing path: %s" % e)
|
||||||
|
|
||||||
|
if print_match:
|
||||||
|
print_match(module, doc, xpath, namespaces)
|
||||||
|
|
||||||
|
if count:
|
||||||
|
count_nodes(module, doc, xpath, namespaces)
|
||||||
|
|
||||||
|
if content == 'attribute':
|
||||||
|
get_element_attr(module, doc, xpath, namespaces)
|
||||||
|
elif content == 'text':
|
||||||
|
get_element_text(module, doc, xpath, namespaces)
|
||||||
|
|
||||||
|
# module.fail_json(msg="OK. Well, etree parsed the xml file...")
|
||||||
|
|
||||||
|
# module.exit_json(what_did={"foo": "bar"}, changed=True)
|
||||||
|
|
||||||
|
# File exists:
|
||||||
|
if state == 'absent':
|
||||||
|
# - absent: delete xpath target
|
||||||
|
delete_xpath_target(module, doc, xpath, namespaces)
|
||||||
|
# Exit
|
||||||
|
# - present: carry on
|
||||||
|
|
||||||
|
# children && value both set?: should have already aborted by now
|
||||||
|
# add_children && set_children both set?: should have already aborted by now
|
||||||
|
|
||||||
|
# set_children set?
|
||||||
|
if set_children:
|
||||||
|
set_target_children(module, doc, xpath, namespaces, set_children, input_type)
|
||||||
|
|
||||||
|
# add_children set?
|
||||||
|
if add_children:
|
||||||
|
add_target_children(module, doc, xpath, namespaces, add_children, input_type)
|
||||||
|
|
||||||
|
# No?: Carry on
|
||||||
|
|
||||||
|
# Is the xpath target an attribute selector?
|
||||||
|
if value is not None:
|
||||||
|
set_target(module, doc, xpath, namespaces, attribute, value)
|
||||||
|
|
||||||
|
# Format the xml only?
|
||||||
|
if pretty_print:
|
||||||
|
pretty(module, doc)
|
||||||
|
|
||||||
|
ensure_xpath_exists(module, doc, xpath, namespaces)
|
||||||
|
# module.fail_json(msg="don't know what to do")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
1
test/integration/targets/xml/aliases
Normal file
1
test/integration/targets/xml/aliases
Normal file
|
@ -0,0 +1 @@
|
||||||
|
posix/ci/group1
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business type="bar">
|
||||||
|
<name>Толстый бар</name>
|
||||||
|
<beers>
|
||||||
|
<beer>Окское</beer>
|
||||||
|
<beer>Невское</beer>
|
||||||
|
</beers>
|
||||||
|
<rating subjective="да">десять</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tolstyybar.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
14
test/integration/targets/xml/fixtures/ansible-xml-beers.xml
Normal file
14
test/integration/targets/xml/fixtures/ansible-xml-beers.xml
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business xmlns="http://test.business" xmlns:attr="http://test.attribute" type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers xmlns="http://test.beers">
|
||||||
|
<beer>Rochefort 10</beer>
|
||||||
|
<beer>St. Bernardus Abbot 12</beer>
|
||||||
|
<beer>Schlitz</beer>
|
||||||
|
</beers>
|
||||||
|
<rating xmlns="http://test.rating" attr:subjective="true">10</rating>
|
||||||
|
<website xmlns="http://test.website">
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
||||||
|
<beer>Окское</beer></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
||||||
|
<beer>Old Rasputin</beer></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
||||||
|
<beer>Natty Lite</beer><beer>Miller Lite</beer><beer>Coors Lite</beer></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
||||||
|
<beer name="Окское" type="экстра"/></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
||||||
|
<beer name="Ansible Brew" type="light"/></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?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>
|
||||||
|
<beer color="red">George Killian's Irish Red</beer>
|
||||||
|
<beer origin="CZ" color="blonde">Pilsner Urquell</beer>
|
||||||
|
</beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
<validxhtml validateon=""/>
|
||||||
|
</website>
|
||||||
|
<phonenumber>555-555-1234</phonenumber>
|
||||||
|
<owner dob="1976-04-12">
|
||||||
|
<name>
|
||||||
|
<last>Smith</last>
|
||||||
|
<first>John</first>
|
||||||
|
<middle>Q</middle>
|
||||||
|
</name>
|
||||||
|
</owner>
|
||||||
|
<website_bis>
|
||||||
|
<validxhtml validateon=""/>
|
||||||
|
</website_bis>
|
||||||
|
<testnormalelement>xml tag with no special characters</testnormalelement>
|
||||||
|
<test-with-dash>xml tag with dashes</test-with-dash>
|
||||||
|
<test-with-dash.and.dot>xml tag with dashes and dots</test-with-dash.and.dot>
|
||||||
|
<test-with.dash_and.dot_and-underscores>xml tag with dashes, dots and underscores</test-with.dash_and.dot_and-underscores>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business xmlns="http://test.business" xmlns:attr="http://test.attribute" type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers xmlns="http://test.beers">
|
||||||
|
<beer>Rochefort 10</beer>
|
||||||
|
<beer>St. Bernardus Abbot 12</beer>
|
||||||
|
<beer>Schlitz</beer>
|
||||||
|
<beer>Old Rasputin</beer></beers>
|
||||||
|
<rating xmlns="http://test.rating" attr:subjective="true">10</rating>
|
||||||
|
<website xmlns="http://test.website">
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>
|
15
test/integration/targets/xml/results/test-pretty-print.xml
Normal file
15
test/integration/targets/xml/results/test-pretty-print.xml
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<?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>
|
||||||
|
<beer>Old Rasputin</beer>
|
||||||
|
</beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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>10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
13
test/integration/targets/xml/results/test-remove-element.xml
Normal file
13
test/integration/targets/xml/results/test-remove-element.xml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?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>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business xmlns="http://test.business" xmlns:attr="http://test.attribute" type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers xmlns="http://test.beers">
|
||||||
|
<beer>Rochefort 10</beer>
|
||||||
|
<beer>St. Bernardus Abbot 12</beer>
|
||||||
|
<beer>Schlitz</beer>
|
||||||
|
</beers>
|
||||||
|
<rating xmlns="http://test.rating">10</rating>
|
||||||
|
<website xmlns="http://test.website">
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business xmlns="http://test.business" xmlns:attr="http://test.attribute" type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers xmlns="http://test.beers">
|
||||||
|
<beer>Rochefort 10</beer>
|
||||||
|
<beer>St. Bernardus Abbot 12</beer>
|
||||||
|
<beer>Schlitz</beer>
|
||||||
|
</beers>
|
||||||
|
<website xmlns="http://test.website">
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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="нет">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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="false">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers>
|
||||||
|
<beer alcohol="0.5" name="90 Minute IPA"><Water liter="0.2" quantity="200g"/><Starch quantity="10g"/><Hops quantity="50g"/><Yeast quantity="20g"/></beer><beer alcohol="0.3" name="Harvest Pumpkin Ale"><Water liter="0.2" quantity="200g"/><Hops quantity="25g"/><Yeast quantity="20g"/></beer></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers>
|
||||||
|
<beer>Окское</beer><beer>Невское</beer></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers>
|
||||||
|
<beer>90 Minute IPA</beer><beer>Harvest Pumpkin Ale</beer></beers>
|
||||||
|
<rating subjective="true">10</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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></address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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">пять</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
<rating>пять</rating></business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?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">5</rating>
|
||||||
|
<website>
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
<rating>5</rating></business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business xmlns="http://test.business" xmlns:attr="http://test.attribute" type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers xmlns="http://test.beers">
|
||||||
|
<beer>Rochefort 10</beer>
|
||||||
|
<beer>St. Bernardus Abbot 12</beer>
|
||||||
|
<beer>Schlitz</beer>
|
||||||
|
</beers>
|
||||||
|
<rating xmlns="http://test.rating" attr:subjective="false">10</rating>
|
||||||
|
<website xmlns="http://test.website">
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<business xmlns="http://test.business" xmlns:attr="http://test.attribute" type="bar">
|
||||||
|
<name>Tasty Beverage Co.</name>
|
||||||
|
<beers xmlns="http://test.beers">
|
||||||
|
<beer>Rochefort 10</beer>
|
||||||
|
<beer>St. Bernardus Abbot 12</beer>
|
||||||
|
<beer>Schlitz</beer>
|
||||||
|
</beers>
|
||||||
|
<rating xmlns="http://test.rating" attr:subjective="true">11</rating>
|
||||||
|
<website xmlns="http://test.website">
|
||||||
|
<mobilefriendly/>
|
||||||
|
<address>http://tastybeverageco.com</address>
|
||||||
|
</website>
|
||||||
|
</business>
|
68
test/integration/targets/xml/tasks/main.yml
Normal file
68
test/integration/targets/xml/tasks/main.yml
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
- name: Gather facts
|
||||||
|
setup:
|
||||||
|
|
||||||
|
- name: Install lxml (FreeBSD)
|
||||||
|
pkgng:
|
||||||
|
name: py27-lxml
|
||||||
|
state: present
|
||||||
|
when: ansible_os_family == "FreeBSD"
|
||||||
|
|
||||||
|
# Needed for MacOSX !
|
||||||
|
- name: Install lxml
|
||||||
|
pip:
|
||||||
|
name: lxml
|
||||||
|
state: present
|
||||||
|
# when: ansible_os_family == "Darwin"
|
||||||
|
|
||||||
|
- name: Get lxml major version
|
||||||
|
shell: python -c 'from lxml import etree; print(etree.LXML_VERSION[0])'
|
||||||
|
register: lxml_major_version
|
||||||
|
|
||||||
|
- name: Get lxml minor version
|
||||||
|
shell: python -c 'from lxml import etree; print(etree.LXML_VERSION[1])'
|
||||||
|
register: lxml_minor_version
|
||||||
|
|
||||||
|
- name: Set lxml capabilities as variables
|
||||||
|
set_fact:
|
||||||
|
# NOTE: Some tests require predictable element attribute order,
|
||||||
|
# which is only guaranteed starting from lxml v3.0alpha1
|
||||||
|
lxml_predictable_attribute_order: '{{ lxml_major_version.stdout|int >= 3 }}'
|
||||||
|
|
||||||
|
# NOTE: The xml module requires at least lxml v2.3.0
|
||||||
|
lxml_xpath_attribute_result_attrname: '{{ lxml_major_version.stdout|int >= 2 and lxml_minor_version.stdout|int >= 3 }}'
|
||||||
|
|
||||||
|
- name: Only run the tests when lxml v2.3.0+
|
||||||
|
when: lxml_xpath_attribute_result_attrname
|
||||||
|
block:
|
||||||
|
|
||||||
|
- include: test-add-children-elements.yml
|
||||||
|
- include: test-add-children-from-groupvars.yml
|
||||||
|
- include: test-add-children-with-attributes.yml
|
||||||
|
- include: test-add-element-implicitly.yml
|
||||||
|
- include: test-count.yml
|
||||||
|
- include: test-mutually-exclusive-attributes.yml
|
||||||
|
- include: test-remove-attribute.yml
|
||||||
|
- include: test-remove-element.yml
|
||||||
|
- include: test-set-attribute-value.yml
|
||||||
|
- include: test-set-children-elements.yml
|
||||||
|
- include: test-set-children-elements-level.yml
|
||||||
|
- include: test-set-element-value.yml
|
||||||
|
- include: test-set-element-value-empty.yml
|
||||||
|
- include: test-pretty-print.yml
|
||||||
|
- include: test-pretty-print-only.yml
|
||||||
|
- include: test-add-namespaced-children-elements.yml
|
||||||
|
- include: test-remove-namespaced-attribute.yml
|
||||||
|
- include: test-set-namespaced-attribute-value.yml
|
||||||
|
- include: test-set-namespaced-element-value.yml
|
||||||
|
- include: test-get-element-content.yml
|
||||||
|
- include: test-xmlstring.yml
|
||||||
|
- include: test-children-elements-xml.yml
|
||||||
|
|
||||||
|
# Unicode tests
|
||||||
|
- include: test-add-children-elements-unicode.yml
|
||||||
|
- include: test-add-children-with-attributes-unicode.yml
|
||||||
|
- include: test-set-attribute-value-unicode.yml
|
||||||
|
- include: test-count-unicode.yml
|
||||||
|
- include: test-get-element-content.yml
|
||||||
|
- include: test-set-children-elements-unicode.yml
|
||||||
|
- include: test-set-element-value-unicode.yml
|
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
add_children:
|
||||||
|
- beer: "Окское"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-children-elements-unicode.xml /tmp/ansible-xml-beers.xml
|
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
add_children:
|
||||||
|
- beer: "Old Rasputin"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-children-elements.xml /tmp/ansible-xml-beers.xml
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
add_children: "{{ bad_beers }}"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-children-from-groupvars.xml /tmp/ansible-xml-beers.xml
|
|
@ -0,0 +1,17 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
add_children:
|
||||||
|
- beer:
|
||||||
|
name: Окское
|
||||||
|
type: экстра
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-children-with-attributes-unicode.xml /tmp/ansible-xml-beers.xml
|
|
@ -0,0 +1,22 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
add_children:
|
||||||
|
- beer:
|
||||||
|
name: Ansible Brew
|
||||||
|
type: light
|
||||||
|
|
||||||
|
# NOTE: This test may fail if lxml does not support predictable element attribute order
|
||||||
|
# So we filter the failure out for these platforms (e.g. CentOS 6)
|
||||||
|
# The module still works fine, we simply are not comparing as smart as we should.
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-children-with-attributes.xml /tmp/ansible-xml-beers.xml
|
||||||
|
register: diff
|
||||||
|
failed_when: diff.rc != 0 and lxml_predictable_attribute_order
|
|
@ -0,0 +1,179 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy: src={{ role_path }}/fixtures/ansible-xml-beers.xml dest=/tmp/ansible-xml-beers-implicit.xml
|
||||||
|
|
||||||
|
- name: Add a phonenumber element to the business element. Implicit mkdir -p behavior where applicable
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/phonenumber value=555-555-1234
|
||||||
|
|
||||||
|
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 1/2
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/name/last value=Smith
|
||||||
|
|
||||||
|
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 2/2
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/name/first value=John
|
||||||
|
|
||||||
|
- name: Add a validxhtml element to the website element. Note that ensure is present by default and while value defaults to null for elements, if one doesn't specify it we don't know what to do.
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/website/validxhtml
|
||||||
|
|
||||||
|
- name: Add an empty validateon attribute to the validxhtml element. This actually makes the previous example redundant because of the implicit parent-node creation behavior.
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/website/validxhtml/@validateon
|
||||||
|
|
||||||
|
- name: Add an empty validateon attribute to the validxhtml element. Actually verifies the implicit parent-node creation behavior.
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/website_bis/validxhtml/@validateon
|
||||||
|
|
||||||
|
- name: Add an attribute with a value
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/@dob='1976-04-12'
|
||||||
|
|
||||||
|
- name: Add an element with a value, alternate syntax
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath="/business/beers/beer/text()=\"George Killian's Irish Red\"" # note the quote within an XPath string thing
|
||||||
|
|
||||||
|
- name: Add an element without special characters
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/testnormalelement value="xml tag with no special characters" pretty_print=true
|
||||||
|
|
||||||
|
- name: Add an element with dash
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/test-with-dash value="xml tag with dashes" pretty_print=true
|
||||||
|
|
||||||
|
- name: Add an element with dot
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/test-with-dash.and.dot value="xml tag with dashes and dots" pretty_print=true
|
||||||
|
|
||||||
|
- name: Add an element with underscore
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/test-with.dash_and.dot_and-underscores value="xml tag with dashes, dots and underscores" pretty_print=true
|
||||||
|
|
||||||
|
- name: Add an attribute on a conditional element
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath="/business/beers/beer[text()=\"George Killian's Irish Red\"]/@color='red'"
|
||||||
|
|
||||||
|
- name: Add two attributes on a conditional element
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath="/business/beers/beer[text()=\"Pilsner Urquell\" and @origin='CZ']/@color='blonde'"
|
||||||
|
|
||||||
|
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 3/2 -- complex lookup
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml xpath=/business/owner/name[first/text()='John']/middle value=Q
|
||||||
|
|
||||||
|
- name: Pretty Print this!
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml pretty_print=True
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-element-implicitly.yml /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
|
||||||
|
#
|
||||||
|
# Now we repeat the same, just to ensure proper use of namespaces
|
||||||
|
#
|
||||||
|
|
||||||
|
- name: Add a phonenumber element to the business element. Implicit mkdir -p behavior where applicable
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:phonenumber
|
||||||
|
value: 555-555-1234
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 1/2
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:owner/a:name/a:last
|
||||||
|
value: Smith
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 2/2
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:owner/a:name/a:first
|
||||||
|
value: John
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add a validxhtml element to the website element. Note that ensure is present by default and while value defaults to null for elements, if one doesn't specify it we don't know what to do.
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:website/a:validxhtml
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an empty validateon attribute to the validxhtml element. This actually makes the previous example redundant because of the implicit parent-node creation behavior.
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:website/a:validxhtml/@a:validateon
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an empty validateon attribute to the validxhtml element. Actually verifies the implicit parent-node creation behavior.
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:website_bis/a:validxhtml/@a:validateon
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an attribute with a value
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:owner/@a:dob='1976-04-12'
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an element with a value, alternate syntax
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: "/business/a:beers/a:beer/text()=\"George Killian's Irish Red\"" # note the quote within an XPath string thing
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an attribute on a conditional element
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: "/business/a:beers/a:beer[text()=\"George Killian's Irish Red\"]/@a:color='red'"
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add two attributes on a conditional element
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: "/business/a:beers/a:beer[text()=\"Pilsner Urquell\" and @a:origin='CZ']/@a:color='blonde'"
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add a owner element to the business element, testing implicit mkdir -p behavior 3/2 -- complex lookup
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/a:owner/a:name[a:first/text()='John']/a:middle
|
||||||
|
value: Q
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an element without special characters
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/testnormalelement
|
||||||
|
value: "xml tag with no special characters"
|
||||||
|
pretty_print: true
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
|
||||||
|
- name: Add an element with dash
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/test-with-dash
|
||||||
|
value: "xml tag with dashes"
|
||||||
|
pretty_print: true
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an element with dot
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/test-with-dash.and.dot
|
||||||
|
value: "xml tag with dashes and dots"
|
||||||
|
pretty_print: true
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Add an element with underscore
|
||||||
|
xml:
|
||||||
|
file: /tmp/ansible-xml-beers-implicit.xml
|
||||||
|
xpath: /business/test-with.dash_and.dot_and-underscores
|
||||||
|
value: "xml tag with dashes, dots and underscores"
|
||||||
|
pretty_print: true
|
||||||
|
namespaces:
|
||||||
|
a: http://example.com/some/namespace
|
||||||
|
|
||||||
|
- name: Pretty Print this!
|
||||||
|
xml: file=/tmp/ansible-xml-beers-implicit.xml pretty_print=True
|
|
@ -0,0 +1,18 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-namespaced-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
|
||||||
|
- name: Add namespaced child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
xpath: /bus:business/ber:beers
|
||||||
|
namespaces:
|
||||||
|
bus: http://test.business
|
||||||
|
ber: http://test.beers
|
||||||
|
add_children:
|
||||||
|
- beer: "Old Rasputin"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-namespaced-children-elements.xml /tmp/ansible-xml-namespaced-beers.xml
|
|
@ -0,0 +1,16 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element with xml format
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
input_type: xml
|
||||||
|
add_children:
|
||||||
|
- "<beer>Old Rasputin</beer>"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-add-children-elements.xml /tmp/ansible-xml-beers.xml
|
13
test/integration/targets/xml/tasks/test-count-unicode.yml
Normal file
13
test/integration/targets/xml/tasks/test-count-unicode.yml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers-unicode.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers-unicode.xml
|
||||||
|
|
||||||
|
- name: Count child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers-unicode.xml
|
||||||
|
xpath: /business/beers/beer
|
||||||
|
count: true
|
||||||
|
register: beers
|
||||||
|
failed_when: beers.count != 2
|
13
test/integration/targets/xml/tasks/test-count.yml
Normal file
13
test/integration/targets/xml/tasks/test-count.yml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add child element
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers/beer
|
||||||
|
count: true
|
||||||
|
register: beers
|
||||||
|
failed_when: beers.count != 3
|
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers-unicode.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers-unicode.xml
|
||||||
|
|
||||||
|
- name: Get element attributes
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers-unicode.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
content: 'attribute'
|
||||||
|
register: get_element_attribute
|
||||||
|
failed_when: get_element_attribute.matches[0]['rating'] is not defined or get_element_attribute.matches[0]['rating']['subjective'] != 'да'
|
||||||
|
|
||||||
|
- name: Get element text
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers-unicode.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
content: 'text'
|
||||||
|
register: get_element_text
|
||||||
|
failed_when: get_element_text.matches[0]['rating'] != 'десять'
|
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Get element attributes
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
content: 'attribute'
|
||||||
|
register: get_element_attribute
|
||||||
|
failed_when: get_element_attribute.matches[0]['rating'] is not defined or get_element_attribute.matches[0]['rating']['subjective'] != 'true'
|
||||||
|
|
||||||
|
- name: Get element text
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
content: 'text'
|
||||||
|
register: get_element_text
|
||||||
|
failed_when: get_element_text.matches[0]['rating'] != '10'
|
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Specify both children to add and a value
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
add_children:
|
||||||
|
- child01
|
||||||
|
- child02
|
||||||
|
value: conflict!
|
||||||
|
register: module_output
|
||||||
|
failed_when: "module_output.failed == 'false'"
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
shell: cat {{ role_path }}/fixtures/ansible-xml-beers.xml | sed 's/^[ ]*//g' > /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Pretty print without modification
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
pretty_print: True
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-pretty-print-only.xml /tmp/ansible-xml-beers.xml
|
16
test/integration/targets/xml/tasks/test-pretty-print.yml
Normal file
16
test/integration/targets/xml/tasks/test-pretty-print.yml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Pretty print
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
pretty_print: True
|
||||||
|
add_children:
|
||||||
|
- beer: "Old Rasputin"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-pretty-print.xml /tmp/ansible-xml-beers.xml
|
14
test/integration/targets/xml/tasks/test-remove-attribute.yml
Normal file
14
test/integration/targets/xml/tasks/test-remove-attribute.yml
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Remove '/business/rating/@subjective'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating/@subjective
|
||||||
|
ensure: absent
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-remove-attribute.xml /tmp/ansible-xml-beers.xml
|
14
test/integration/targets/xml/tasks/test-remove-element.yml
Normal file
14
test/integration/targets/xml/tasks/test-remove-element.yml
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Remove '/business/rating'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
ensure: absent
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-remove-element.xml /tmp/ansible-xml-beers.xml
|
|
@ -0,0 +1,19 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-namespaced-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
|
||||||
|
- name: Remove namespaced '/bus:business/rat:rating/@attr:subjective'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
xpath: /bus:business/rat:rating/@attr:subjective
|
||||||
|
namespaces:
|
||||||
|
bus: http://test.business
|
||||||
|
ber: http://test.beers
|
||||||
|
rat: http://test.rating
|
||||||
|
attr: http://test.attribute
|
||||||
|
ensure: absent
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-remove-namespaced-attribute.xml /tmp/ansible-xml-namespaced-beers.xml
|
|
@ -0,0 +1,19 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-namespaced-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
|
||||||
|
- name: Remove namespaced '/bus:business/rat:rating'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
xpath: /bus:business/rat:rating
|
||||||
|
namespaces:
|
||||||
|
bus: http://test.business
|
||||||
|
ber: http://test.beers
|
||||||
|
rat: http://test.rating
|
||||||
|
attr: http://test.attribute
|
||||||
|
ensure: absent
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-remove-element.xml /tmp/ansible-xml-namespaced-beers.xml
|
|
@ -0,0 +1,16 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set '/business/rating/@subjective' to 'нет'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
attribute: subjective
|
||||||
|
value: "нет"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-attribute-value-unicode.xml /tmp/ansible-xml-beers.xml
|
||||||
|
changed_when: False
|
|
@ -0,0 +1,16 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set '/business/rating/@subjective' to 'false'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
attribute: subjective
|
||||||
|
value: "false"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-attribute-value.xml /tmp/ansible-xml-beers.xml
|
||||||
|
changed_when: False
|
|
@ -0,0 +1,71 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
command: cp {{ role_path }}/fixtures/ansible-xml-beers.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set child elements
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
set_children:
|
||||||
|
- beer:
|
||||||
|
name: "90 Minute IPA"
|
||||||
|
alcohol: "0.5"
|
||||||
|
_:
|
||||||
|
- Water:
|
||||||
|
quantity: 200g
|
||||||
|
liter: "0.2"
|
||||||
|
- Starch:
|
||||||
|
quantity: 10g
|
||||||
|
- Hops:
|
||||||
|
quantity: 50g
|
||||||
|
- Yeast:
|
||||||
|
quantity: 20g
|
||||||
|
- beer:
|
||||||
|
name: "Harvest Pumpkin Ale"
|
||||||
|
alcohol: "0.3"
|
||||||
|
_:
|
||||||
|
- Water:
|
||||||
|
quantity: 200g
|
||||||
|
liter: "0.2"
|
||||||
|
- Hops:
|
||||||
|
quantity: 25g
|
||||||
|
- Yeast:
|
||||||
|
quantity: 20g
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-children-elements-level.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set child elements
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
set_children:
|
||||||
|
- beer:
|
||||||
|
name: "90 Minute IPA"
|
||||||
|
alcohol: "0.5"
|
||||||
|
_:
|
||||||
|
- Water:
|
||||||
|
quantity: 200g
|
||||||
|
liter: "0.2"
|
||||||
|
- Starch:
|
||||||
|
quantity: 10g
|
||||||
|
- Hops:
|
||||||
|
quantity: 50g
|
||||||
|
- Yeast:
|
||||||
|
quantity: 20g
|
||||||
|
- beer:
|
||||||
|
name: "Harvest Pumpkin Ale"
|
||||||
|
alcohol: "0.3"
|
||||||
|
_:
|
||||||
|
- Water:
|
||||||
|
quantity: 200g
|
||||||
|
liter: "0.2"
|
||||||
|
- Hops:
|
||||||
|
quantity: 25g
|
||||||
|
- Yeast:
|
||||||
|
quantity: 20g
|
||||||
|
register: set_children_again
|
||||||
|
|
||||||
|
- fail:
|
||||||
|
msg: "Setting children is not idempotent!"
|
||||||
|
when: set_children_again.changed
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
command: cp {{ role_path }}/fixtures/ansible-xml-beers.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set child elements
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
set_children:
|
||||||
|
- beer: "Окское"
|
||||||
|
- beer: "Невское"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-children-elements-unicode.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set child elements
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
set_children:
|
||||||
|
- beer: "Окское"
|
||||||
|
- beer: "Невское"
|
||||||
|
register: set_children_again
|
||||||
|
|
||||||
|
- fail:
|
||||||
|
msg: "Setting children is not idempotent!"
|
||||||
|
when: set_children_again.changed
|
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
command: cp {{ role_path }}/fixtures/ansible-xml-beers.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set child elements
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
set_children:
|
||||||
|
- beer: "90 Minute IPA"
|
||||||
|
- beer: "Harvest Pumpkin Ale"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-children-elements.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set child elements
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/beers
|
||||||
|
set_children:
|
||||||
|
- beer: "90 Minute IPA"
|
||||||
|
- beer: "Harvest Pumpkin Ale"
|
||||||
|
register: set_children_again
|
||||||
|
|
||||||
|
- fail:
|
||||||
|
msg: "Setting children is not idempotent!"
|
||||||
|
when: set_children_again.changed
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Set /business/website/address to empty string.
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/website/address
|
||||||
|
value: ''
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-element-value-empty.xml /tmp/ansible-xml-beers.xml
|
|
@ -0,0 +1,37 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add 2nd '/business/rating' with value 'пять'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business
|
||||||
|
add_children:
|
||||||
|
- rating: "пять"
|
||||||
|
|
||||||
|
- name: Set '/business/rating' to 'пять'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
value: "пять"
|
||||||
|
register: set_element_first_run
|
||||||
|
|
||||||
|
- name: Set '/business/rating' to 'false'... again
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
value: "пять"
|
||||||
|
register: set_element_second_run
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-element-value-unicode.xml /tmp/ansible-xml-beers.xml
|
||||||
|
changed_when: no
|
||||||
|
|
||||||
|
- name: Test registered 'changed' on run 1 and unchanged on run 2
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- set_element_first_run.changed
|
||||||
|
- not set_element_second_run.changed
|
||||||
|
...
|
|
@ -0,0 +1,37 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Add 2nd '/business/rating' with value '5'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business
|
||||||
|
add_children:
|
||||||
|
- rating: "5"
|
||||||
|
|
||||||
|
- name: Set '/business/rating' to '5'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
value: "5"
|
||||||
|
register: set_element_first_run
|
||||||
|
|
||||||
|
- name: Set '/business/rating' to 'false'... again
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-beers.xml
|
||||||
|
xpath: /business/rating
|
||||||
|
value: "5"
|
||||||
|
register: set_element_second_run
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-element-value.xml /tmp/ansible-xml-beers.xml
|
||||||
|
changed_when: no
|
||||||
|
|
||||||
|
- name: Test registered 'changed' on run 1 and unchanged on run 2
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- set_element_first_run.changed
|
||||||
|
- not set_element_second_run.changed
|
||||||
|
...
|
|
@ -0,0 +1,21 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-namespaced-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
|
||||||
|
- name: Set namespaced '/bus:business/rat:rating/@attr:subjective' to 'false'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
xpath: /bus:business/rat:rating
|
||||||
|
namespaces:
|
||||||
|
bus: http://test.business
|
||||||
|
ber: http://test.beers
|
||||||
|
rat: http://test.rating
|
||||||
|
attr: http://test.attribute
|
||||||
|
attribute: attr:subjective
|
||||||
|
value: "false"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-namespaced-attribute-value.xml /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
changed_when: no
|
|
@ -0,0 +1,39 @@
|
||||||
|
---
|
||||||
|
- name: Setup test fixture
|
||||||
|
copy:
|
||||||
|
src: '{{ role_path }}/fixtures/ansible-xml-namespaced-beers.xml'
|
||||||
|
dest: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
|
||||||
|
- name: Set namespaced '/bus:business/rat:rating' to '11'
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
namespaces:
|
||||||
|
bus: http://test.business
|
||||||
|
ber: http://test.beers
|
||||||
|
rat: http://test.rating
|
||||||
|
attr: http://test.attribute
|
||||||
|
xpath: /bus:business/rat:rating
|
||||||
|
value: "11"
|
||||||
|
register: set_element_first_run
|
||||||
|
|
||||||
|
- name: Set namespaced '/bus:business/rat:rating' to '11' again
|
||||||
|
xml:
|
||||||
|
path: /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
namespaces:
|
||||||
|
bus: http://test.business
|
||||||
|
ber: http://test.beers
|
||||||
|
rat: http://test.rating
|
||||||
|
attr: http://test.attribute
|
||||||
|
xpath: /bus:business/rat:rating
|
||||||
|
value: "11"
|
||||||
|
register: set_element_second_run
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-set-namespaced-element-value.xml /tmp/ansible-xml-namespaced-beers.xml
|
||||||
|
changed_when: no
|
||||||
|
|
||||||
|
- name: Test registered 'changed' on run 1 and unchanged on run 2
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- set_element_first_run.changed
|
||||||
|
- not set_element_second_run.changed
|
31
test/integration/targets/xml/tasks/test-xmlstring.yml
Normal file
31
test/integration/targets/xml/tasks/test-xmlstring.yml
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
- name: Read from xmlstring
|
||||||
|
xml:
|
||||||
|
xmlstring: "{{ lookup('file', '{{ role_path }}/fixtures/ansible-xml-beers.xml') }}"
|
||||||
|
pretty_print: True
|
||||||
|
register: xmlresponse
|
||||||
|
|
||||||
|
- name: Write result to file
|
||||||
|
copy:
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
content: "{{ xmlresponse.xmlstring }}"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-pretty-print-only.xml /tmp/ansible-xml-beers.xml
|
||||||
|
|
||||||
|
- name: Read from xmlstring
|
||||||
|
xml:
|
||||||
|
xmlstring: "{{ lookup('file', '{{ role_path }}/fixtures/ansible-xml-beers.xml') }}"
|
||||||
|
xpath: /business/beers
|
||||||
|
pretty_print: True
|
||||||
|
add_children:
|
||||||
|
- beer: "Old Rasputin"
|
||||||
|
register: xmlresponse_modification
|
||||||
|
|
||||||
|
- name: Write result to file
|
||||||
|
copy:
|
||||||
|
dest: /tmp/ansible-xml-beers.xml
|
||||||
|
content: "{{ xmlresponse_modification.xmlstring }}"
|
||||||
|
|
||||||
|
- name: Test expected result
|
||||||
|
command: diff -u {{ role_path }}/results/test-pretty-print.xml /tmp/ansible-xml-beers.xml
|
6
test/integration/targets/xml/vars/main.yml
Normal file
6
test/integration/targets/xml/vars/main.yml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# -*- mode: yaml -*
|
||||||
|
---
|
||||||
|
bad_beers:
|
||||||
|
- beer: "Natty Lite"
|
||||||
|
- beer: "Miller Lite"
|
||||||
|
- beer: "Coors Lite"
|
Loading…
Reference in a new issue