From 6874ba23ff09d5fd26253d9f4b2c6078044a0474 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 9 Aug 2017 05:21:03 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + lib/ansible/modules/files/xml.py | 748 ++++++++++++++++++ test/integration/targets/xml/aliases | 1 + .../fixtures/ansible-xml-beers-unicode.xml | 13 + .../xml/fixtures/ansible-xml-beers.xml | 14 + .../fixtures/ansible-xml-namespaced-beers.xml | 14 + .../test-add-children-elements-unicode.xml | 14 + .../results/test-add-children-elements.xml | 14 + .../test-add-children-from-groupvars.xml | 14 + ...t-add-children-with-attributes-unicode.xml | 14 + .../test-add-children-with-attributes.xml | 14 + .../results/test-add-element-implicitly.yml | 32 + .../test-add-namespaced-children-elements.xml | 14 + .../xml/results/test-pretty-print-only.xml | 14 + .../targets/xml/results/test-pretty-print.xml | 15 + .../xml/results/test-remove-attribute.xml | 14 + .../xml/results/test-remove-element.xml | 13 + .../test-remove-namespaced-attribute.xml | 14 + .../test-remove-namespaced-element.xml | 13 + .../test-set-attribute-value-unicode.xml | 14 + .../xml/results/test-set-attribute-value.xml | 14 + .../test-set-children-elements-level.xml | 11 + .../test-set-children-elements-unicode.xml | 11 + .../results/test-set-children-elements.xml | 11 + .../results/test-set-element-value-empty.xml | 14 + .../test-set-element-value-unicode.xml | 14 + .../xml/results/test-set-element-value.xml | 14 + .../test-set-namespaced-attribute-value.xml | 14 + .../test-set-namespaced-element-value.xml | 14 + test/integration/targets/xml/tasks/main.yml | 68 ++ .../test-add-children-elements-unicode.yml | 15 + .../xml/tasks/test-add-children-elements.yml | 15 + .../test-add-children-from-groupvars.yml | 14 + ...t-add-children-with-attributes-unicode.yml | 17 + .../test-add-children-with-attributes.yml | 22 + .../xml/tasks/test-add-element-implicitly.yml | 179 +++++ .../test-add-namespaced-children-elements.yml | 18 + .../xml/tasks/test-children-elements-xml.yml | 16 + .../targets/xml/tasks/test-count-unicode.yml | 13 + .../targets/xml/tasks/test-count.yml | 13 + .../test-get-element-content-unicode.yml | 21 + .../xml/tasks/test-get-element-content.yml | 21 + .../test-mutually-exclusive-attributes.yml | 15 + .../xml/tasks/test-pretty-print-only.yml | 11 + .../targets/xml/tasks/test-pretty-print.yml | 16 + .../xml/tasks/test-remove-attribute.yml | 14 + .../targets/xml/tasks/test-remove-element.yml | 14 + .../test-remove-namespaced-attribute.yml | 19 + .../tasks/test-remove-namespaced-element.yml | 19 + .../test-set-attribute-value-unicode.yml | 16 + .../xml/tasks/test-set-attribute-value.yml | 16 + .../test-set-children-elements-level.yml | 71 ++ .../test-set-children-elements-unicode.yml | 27 + .../xml/tasks/test-set-children-elements.yml | 27 + .../tasks/test-set-element-value-empty.yml | 14 + .../tasks/test-set-element-value-unicode.yml | 37 + .../xml/tasks/test-set-element-value.yml | 37 + .../test-set-namespaced-attribute-value.yml | 21 + .../test-set-namespaced-element-value.yml | 39 + .../targets/xml/tasks/test-xmlstring.yml | 31 + test/integration/targets/xml/vars/main.yml | 6 + 61 files changed, 2003 insertions(+) create mode 100755 lib/ansible/modules/files/xml.py create mode 100644 test/integration/targets/xml/aliases create mode 100644 test/integration/targets/xml/fixtures/ansible-xml-beers-unicode.xml create mode 100644 test/integration/targets/xml/fixtures/ansible-xml-beers.xml create mode 100644 test/integration/targets/xml/fixtures/ansible-xml-namespaced-beers.xml create mode 100644 test/integration/targets/xml/results/test-add-children-elements-unicode.xml create mode 100644 test/integration/targets/xml/results/test-add-children-elements.xml create mode 100644 test/integration/targets/xml/results/test-add-children-from-groupvars.xml create mode 100644 test/integration/targets/xml/results/test-add-children-with-attributes-unicode.xml create mode 100644 test/integration/targets/xml/results/test-add-children-with-attributes.xml create mode 100644 test/integration/targets/xml/results/test-add-element-implicitly.yml create mode 100644 test/integration/targets/xml/results/test-add-namespaced-children-elements.xml create mode 100644 test/integration/targets/xml/results/test-pretty-print-only.xml create mode 100644 test/integration/targets/xml/results/test-pretty-print.xml create mode 100644 test/integration/targets/xml/results/test-remove-attribute.xml create mode 100644 test/integration/targets/xml/results/test-remove-element.xml create mode 100644 test/integration/targets/xml/results/test-remove-namespaced-attribute.xml create mode 100644 test/integration/targets/xml/results/test-remove-namespaced-element.xml create mode 100644 test/integration/targets/xml/results/test-set-attribute-value-unicode.xml create mode 100644 test/integration/targets/xml/results/test-set-attribute-value.xml create mode 100644 test/integration/targets/xml/results/test-set-children-elements-level.xml create mode 100644 test/integration/targets/xml/results/test-set-children-elements-unicode.xml create mode 100644 test/integration/targets/xml/results/test-set-children-elements.xml create mode 100644 test/integration/targets/xml/results/test-set-element-value-empty.xml create mode 100644 test/integration/targets/xml/results/test-set-element-value-unicode.xml create mode 100644 test/integration/targets/xml/results/test-set-element-value.xml create mode 100644 test/integration/targets/xml/results/test-set-namespaced-attribute-value.xml create mode 100644 test/integration/targets/xml/results/test-set-namespaced-element-value.xml create mode 100644 test/integration/targets/xml/tasks/main.yml create mode 100644 test/integration/targets/xml/tasks/test-add-children-elements-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-add-children-elements.yml create mode 100644 test/integration/targets/xml/tasks/test-add-children-from-groupvars.yml create mode 100644 test/integration/targets/xml/tasks/test-add-children-with-attributes-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-add-children-with-attributes.yml create mode 100644 test/integration/targets/xml/tasks/test-add-element-implicitly.yml create mode 100644 test/integration/targets/xml/tasks/test-add-namespaced-children-elements.yml create mode 100644 test/integration/targets/xml/tasks/test-children-elements-xml.yml create mode 100644 test/integration/targets/xml/tasks/test-count-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-count.yml create mode 100644 test/integration/targets/xml/tasks/test-get-element-content-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-get-element-content.yml create mode 100644 test/integration/targets/xml/tasks/test-mutually-exclusive-attributes.yml create mode 100644 test/integration/targets/xml/tasks/test-pretty-print-only.yml create mode 100644 test/integration/targets/xml/tasks/test-pretty-print.yml create mode 100644 test/integration/targets/xml/tasks/test-remove-attribute.yml create mode 100644 test/integration/targets/xml/tasks/test-remove-element.yml create mode 100644 test/integration/targets/xml/tasks/test-remove-namespaced-attribute.yml create mode 100644 test/integration/targets/xml/tasks/test-remove-namespaced-element.yml create mode 100644 test/integration/targets/xml/tasks/test-set-attribute-value-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-set-attribute-value.yml create mode 100644 test/integration/targets/xml/tasks/test-set-children-elements-level.yml create mode 100644 test/integration/targets/xml/tasks/test-set-children-elements-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-set-children-elements.yml create mode 100644 test/integration/targets/xml/tasks/test-set-element-value-empty.yml create mode 100644 test/integration/targets/xml/tasks/test-set-element-value-unicode.yml create mode 100644 test/integration/targets/xml/tasks/test-set-element-value.yml create mode 100644 test/integration/targets/xml/tasks/test-set-namespaced-attribute-value.yml create mode 100644 test/integration/targets/xml/tasks/test-set-namespaced-element-value.yml create mode 100644 test/integration/targets/xml/tasks/test-xmlstring.yml create mode 100644 test/integration/targets/xml/vars/main.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index c27b964803..27ae1b0299 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -302,6 +302,7 @@ Ansible Changes By Release * win_route * win_security_policy * win_wakeonlan +- xml diff --git a/lib/ansible/modules/files/xml.py b/lib/ansible/modules/files/xml.py new file mode 100755 index 0000000000..155a3bd181 --- /dev/null +++ b/lib/ansible/modules/files/xml.py @@ -0,0 +1,748 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright 2014, Red Hat, Inc. +# Tim Bielawa +# Magnus Hedemark +# Copyright 2017, Dag Wieers +# +# 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 . + +# 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() 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() diff --git a/test/integration/targets/xml/aliases b/test/integration/targets/xml/aliases new file mode 100644 index 0000000000..4485d76162 --- /dev/null +++ b/test/integration/targets/xml/aliases @@ -0,0 +1 @@ +posix/ci/group1 diff --git a/test/integration/targets/xml/fixtures/ansible-xml-beers-unicode.xml b/test/integration/targets/xml/fixtures/ansible-xml-beers-unicode.xml new file mode 100644 index 0000000000..d0e3e39af4 --- /dev/null +++ b/test/integration/targets/xml/fixtures/ansible-xml-beers-unicode.xml @@ -0,0 +1,13 @@ + + + Толстый бар + + Окское + Невское + + десять + + +
http://tolstyybar.com
+
+
diff --git a/test/integration/targets/xml/fixtures/ansible-xml-beers.xml b/test/integration/targets/xml/fixtures/ansible-xml-beers.xml new file mode 100644 index 0000000000..5afc797414 --- /dev/null +++ b/test/integration/targets/xml/fixtures/ansible-xml-beers.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/fixtures/ansible-xml-namespaced-beers.xml b/test/integration/targets/xml/fixtures/ansible-xml-namespaced-beers.xml new file mode 100644 index 0000000000..61747d4bbb --- /dev/null +++ b/test/integration/targets/xml/fixtures/ansible-xml-namespaced-beers.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-add-children-elements-unicode.xml b/test/integration/targets/xml/results/test-add-children-elements-unicode.xml new file mode 100644 index 0000000000..525330c217 --- /dev/null +++ b/test/integration/targets/xml/results/test-add-children-elements-unicode.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + Окское + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-add-children-elements.xml b/test/integration/targets/xml/results/test-add-children-elements.xml new file mode 100644 index 0000000000..f9ff25176a --- /dev/null +++ b/test/integration/targets/xml/results/test-add-children-elements.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + Old Rasputin + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-add-children-from-groupvars.xml b/test/integration/targets/xml/results/test-add-children-from-groupvars.xml new file mode 100644 index 0000000000..565ba402b6 --- /dev/null +++ b/test/integration/targets/xml/results/test-add-children-from-groupvars.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + Natty LiteMiller LiteCoors Lite + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-add-children-with-attributes-unicode.xml b/test/integration/targets/xml/results/test-add-children-with-attributes-unicode.xml new file mode 100644 index 0000000000..374652244f --- /dev/null +++ b/test/integration/targets/xml/results/test-add-children-with-attributes-unicode.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-add-children-with-attributes.xml b/test/integration/targets/xml/results/test-add-children-with-attributes.xml new file mode 100644 index 0000000000..5a3907f6f2 --- /dev/null +++ b/test/integration/targets/xml/results/test-add-children-with-attributes.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-add-element-implicitly.yml b/test/integration/targets/xml/results/test-add-element-implicitly.yml new file mode 100644 index 0000000000..fa1ddfca2f --- /dev/null +++ b/test/integration/targets/xml/results/test-add-element-implicitly.yml @@ -0,0 +1,32 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + George Killian's Irish Red + Pilsner Urquell + + 10 + + +
http://tastybeverageco.com
+ +
+ 555-555-1234 + + + Smith + John + Q + + + + + + xml tag with no special characters + xml tag with dashes + xml tag with dashes and dots + xml tag with dashes, dots and underscores +
diff --git a/test/integration/targets/xml/results/test-add-namespaced-children-elements.xml b/test/integration/targets/xml/results/test-add-namespaced-children-elements.xml new file mode 100644 index 0000000000..3d27e8aa3c --- /dev/null +++ b/test/integration/targets/xml/results/test-add-namespaced-children-elements.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + Old Rasputin + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-pretty-print-only.xml b/test/integration/targets/xml/results/test-pretty-print-only.xml new file mode 100644 index 0000000000..f47909ac69 --- /dev/null +++ b/test/integration/targets/xml/results/test-pretty-print-only.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
diff --git a/test/integration/targets/xml/results/test-pretty-print.xml b/test/integration/targets/xml/results/test-pretty-print.xml new file mode 100644 index 0000000000..b5c38262fd --- /dev/null +++ b/test/integration/targets/xml/results/test-pretty-print.xml @@ -0,0 +1,15 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + Old Rasputin + + 10 + + +
http://tastybeverageco.com
+
+
diff --git a/test/integration/targets/xml/results/test-remove-attribute.xml b/test/integration/targets/xml/results/test-remove-attribute.xml new file mode 100644 index 0000000000..8a621cf144 --- /dev/null +++ b/test/integration/targets/xml/results/test-remove-attribute.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-remove-element.xml b/test/integration/targets/xml/results/test-remove-element.xml new file mode 100644 index 0000000000..454d905cd4 --- /dev/null +++ b/test/integration/targets/xml/results/test-remove-element.xml @@ -0,0 +1,13 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-remove-namespaced-attribute.xml b/test/integration/targets/xml/results/test-remove-namespaced-attribute.xml new file mode 100644 index 0000000000..732a0ed224 --- /dev/null +++ b/test/integration/targets/xml/results/test-remove-namespaced-attribute.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-remove-namespaced-element.xml b/test/integration/targets/xml/results/test-remove-namespaced-element.xml new file mode 100644 index 0000000000..16df98e201 --- /dev/null +++ b/test/integration/targets/xml/results/test-remove-namespaced-element.xml @@ -0,0 +1,13 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-attribute-value-unicode.xml b/test/integration/targets/xml/results/test-set-attribute-value-unicode.xml new file mode 100644 index 0000000000..de3bc3f600 --- /dev/null +++ b/test/integration/targets/xml/results/test-set-attribute-value-unicode.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-attribute-value.xml b/test/integration/targets/xml/results/test-set-attribute-value.xml new file mode 100644 index 0000000000..143fe7bf4e --- /dev/null +++ b/test/integration/targets/xml/results/test-set-attribute-value.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-children-elements-level.xml b/test/integration/targets/xml/results/test-set-children-elements-level.xml new file mode 100644 index 0000000000..0ef2b7e6e6 --- /dev/null +++ b/test/integration/targets/xml/results/test-set-children-elements-level.xml @@ -0,0 +1,11 @@ + + + Tasty Beverage Co. + + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-children-elements-unicode.xml b/test/integration/targets/xml/results/test-set-children-elements-unicode.xml new file mode 100644 index 0000000000..f19d53566a --- /dev/null +++ b/test/integration/targets/xml/results/test-set-children-elements-unicode.xml @@ -0,0 +1,11 @@ + + + Tasty Beverage Co. + + ОкскоеНевское + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-children-elements.xml b/test/integration/targets/xml/results/test-set-children-elements.xml new file mode 100644 index 0000000000..be313a5a8d --- /dev/null +++ b/test/integration/targets/xml/results/test-set-children-elements.xml @@ -0,0 +1,11 @@ + + + Tasty Beverage Co. + + 90 Minute IPAHarvest Pumpkin Ale + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-element-value-empty.xml b/test/integration/targets/xml/results/test-set-element-value-empty.xml new file mode 100644 index 0000000000..785beb645d --- /dev/null +++ b/test/integration/targets/xml/results/test-set-element-value-empty.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-element-value-unicode.xml b/test/integration/targets/xml/results/test-set-element-value-unicode.xml new file mode 100644 index 0000000000..734fe6dbf1 --- /dev/null +++ b/test/integration/targets/xml/results/test-set-element-value-unicode.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + пять + + +
http://tastybeverageco.com
+
+пять
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-element-value.xml b/test/integration/targets/xml/results/test-set-element-value.xml new file mode 100644 index 0000000000..fc97ec3bed --- /dev/null +++ b/test/integration/targets/xml/results/test-set-element-value.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 5 + + +
http://tastybeverageco.com
+
+5
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-namespaced-attribute-value.xml b/test/integration/targets/xml/results/test-set-namespaced-attribute-value.xml new file mode 100644 index 0000000000..44abda43f0 --- /dev/null +++ b/test/integration/targets/xml/results/test-set-namespaced-attribute-value.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 10 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/results/test-set-namespaced-element-value.xml b/test/integration/targets/xml/results/test-set-namespaced-element-value.xml new file mode 100644 index 0000000000..0cc8a79e39 --- /dev/null +++ b/test/integration/targets/xml/results/test-set-namespaced-element-value.xml @@ -0,0 +1,14 @@ + + + Tasty Beverage Co. + + Rochefort 10 + St. Bernardus Abbot 12 + Schlitz + + 11 + + +
http://tastybeverageco.com
+
+
\ No newline at end of file diff --git a/test/integration/targets/xml/tasks/main.yml b/test/integration/targets/xml/tasks/main.yml new file mode 100644 index 0000000000..e189c55813 --- /dev/null +++ b/test/integration/targets/xml/tasks/main.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-add-children-elements-unicode.yml b/test/integration/targets/xml/tasks/test-add-children-elements-unicode.yml new file mode 100644 index 0000000000..59c688c915 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-children-elements-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 diff --git a/test/integration/targets/xml/tasks/test-add-children-elements.yml b/test/integration/targets/xml/tasks/test-add-children-elements.yml new file mode 100644 index 0000000000..1f9d02629b --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-children-elements.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: "Old Rasputin" + + - name: Test expected result + command: diff -u {{ role_path }}/results/test-add-children-elements.xml /tmp/ansible-xml-beers.xml diff --git a/test/integration/targets/xml/tasks/test-add-children-from-groupvars.yml b/test/integration/targets/xml/tasks/test-add-children-from-groupvars.yml new file mode 100644 index 0000000000..a514277341 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-children-from-groupvars.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-add-children-with-attributes-unicode.yml b/test/integration/targets/xml/tasks/test-add-children-with-attributes-unicode.yml new file mode 100644 index 0000000000..9696543818 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-children-with-attributes-unicode.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-add-children-with-attributes.yml b/test/integration/targets/xml/tasks/test-add-children-with-attributes.yml new file mode 100644 index 0000000000..11e6af9714 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-children-with-attributes.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-add-element-implicitly.yml b/test/integration/targets/xml/tasks/test-add-element-implicitly.yml new file mode 100644 index 0000000000..0ef714ee19 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-element-implicitly.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-add-namespaced-children-elements.yml b/test/integration/targets/xml/tasks/test-add-namespaced-children-elements.yml new file mode 100644 index 0000000000..09974c8e79 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-add-namespaced-children-elements.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-children-elements-xml.yml b/test/integration/targets/xml/tasks/test-children-elements-xml.yml new file mode 100644 index 0000000000..4350bcaab6 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-children-elements-xml.yml @@ -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: + - "Old Rasputin" + + - name: Test expected result + command: diff -u {{ role_path }}/results/test-add-children-elements.xml /tmp/ansible-xml-beers.xml diff --git a/test/integration/targets/xml/tasks/test-count-unicode.yml b/test/integration/targets/xml/tasks/test-count-unicode.yml new file mode 100644 index 0000000000..17444f4ff4 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-count-unicode.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-count.yml b/test/integration/targets/xml/tasks/test-count.yml new file mode 100644 index 0000000000..acaf8ff1ae --- /dev/null +++ b/test/integration/targets/xml/tasks/test-count.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-get-element-content-unicode.yml b/test/integration/targets/xml/tasks/test-get-element-content-unicode.yml new file mode 100644 index 0000000000..0658c6c49f --- /dev/null +++ b/test/integration/targets/xml/tasks/test-get-element-content-unicode.yml @@ -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'] != 'десять' diff --git a/test/integration/targets/xml/tasks/test-get-element-content.yml b/test/integration/targets/xml/tasks/test-get-element-content.yml new file mode 100644 index 0000000000..3546be0027 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-get-element-content.yml @@ -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' diff --git a/test/integration/targets/xml/tasks/test-mutually-exclusive-attributes.yml b/test/integration/targets/xml/tasks/test-mutually-exclusive-attributes.yml new file mode 100644 index 0000000000..a3769f6902 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-mutually-exclusive-attributes.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: 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'" diff --git a/test/integration/targets/xml/tasks/test-pretty-print-only.yml b/test/integration/targets/xml/tasks/test-pretty-print-only.yml new file mode 100644 index 0000000000..cd8d52e0cf --- /dev/null +++ b/test/integration/targets/xml/tasks/test-pretty-print-only.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-pretty-print.yml b/test/integration/targets/xml/tasks/test-pretty-print.yml new file mode 100644 index 0000000000..dd63f26a72 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-pretty-print.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-remove-attribute.yml b/test/integration/targets/xml/tasks/test-remove-attribute.yml new file mode 100644 index 0000000000..6af599b984 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-remove-attribute.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-remove-element.yml b/test/integration/targets/xml/tasks/test-remove-element.yml new file mode 100644 index 0000000000..f8edd734b1 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-remove-element.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-remove-namespaced-attribute.yml b/test/integration/targets/xml/tasks/test-remove-namespaced-attribute.yml new file mode 100644 index 0000000000..19f41a0827 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-remove-namespaced-attribute.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-remove-namespaced-element.yml b/test/integration/targets/xml/tasks/test-remove-namespaced-element.yml new file mode 100644 index 0000000000..5be96af5a6 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-remove-namespaced-element.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-attribute-value-unicode.yml b/test/integration/targets/xml/tasks/test-set-attribute-value-unicode.yml new file mode 100644 index 0000000000..e6ab507e76 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-attribute-value-unicode.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-attribute-value.yml b/test/integration/targets/xml/tasks/test-set-attribute-value.yml new file mode 100644 index 0000000000..ad6a041dd9 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-attribute-value.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-children-elements-level.yml b/test/integration/targets/xml/tasks/test-set-children-elements-level.yml new file mode 100644 index 0000000000..4cd0168aa1 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-children-elements-level.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-children-elements-unicode.yml b/test/integration/targets/xml/tasks/test-set-children-elements-unicode.yml new file mode 100644 index 0000000000..cdb9da6bc1 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-children-elements-unicode.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-children-elements.yml b/test/integration/targets/xml/tasks/test-set-children-elements.yml new file mode 100644 index 0000000000..a7ec643668 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-children-elements.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-element-value-empty.yml b/test/integration/targets/xml/tasks/test-set-element-value-empty.yml new file mode 100644 index 0000000000..0f5a73938f --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-element-value-empty.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-element-value-unicode.yml b/test/integration/targets/xml/tasks/test-set-element-value-unicode.yml new file mode 100644 index 0000000000..4e13808a2f --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-element-value-unicode.yml @@ -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 +... diff --git a/test/integration/targets/xml/tasks/test-set-element-value.yml b/test/integration/targets/xml/tasks/test-set-element-value.yml new file mode 100644 index 0000000000..07e6d6a24a --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-element-value.yml @@ -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 +... diff --git a/test/integration/targets/xml/tasks/test-set-namespaced-attribute-value.yml b/test/integration/targets/xml/tasks/test-set-namespaced-attribute-value.yml new file mode 100644 index 0000000000..1201abb4c9 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-namespaced-attribute-value.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-set-namespaced-element-value.yml b/test/integration/targets/xml/tasks/test-set-namespaced-element-value.yml new file mode 100644 index 0000000000..80b4e3b543 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-set-namespaced-element-value.yml @@ -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 diff --git a/test/integration/targets/xml/tasks/test-xmlstring.yml b/test/integration/targets/xml/tasks/test-xmlstring.yml new file mode 100644 index 0000000000..4ae642fd57 --- /dev/null +++ b/test/integration/targets/xml/tasks/test-xmlstring.yml @@ -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 diff --git a/test/integration/targets/xml/vars/main.yml b/test/integration/targets/xml/vars/main.yml new file mode 100644 index 0000000000..7c5675bd93 --- /dev/null +++ b/test/integration/targets/xml/vars/main.yml @@ -0,0 +1,6 @@ +# -*- mode: yaml -* +--- +bad_beers: +- beer: "Natty Lite" +- beer: "Miller Lite" +- beer: "Coors Lite"