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:
Dag Wieers 2017-08-09 05:21:03 +02:00 committed by Toshio Kuratomi
commit 6874ba23ff
61 changed files with 2003 additions and 0 deletions

View 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

View file

@ -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

View file

@ -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

View 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: 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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View 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: 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

View 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

View 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

View file

@ -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'] != 'десять'

View file

@ -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'

View file

@ -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'"

View file

@ -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

View 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

View 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

View 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

View file

@ -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

View file

@ -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

View 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: 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

View 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: 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

View file

@ -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

View file

@ -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

View file

@ -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

View 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: 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

View file

@ -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
...

View file

@ -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
...

View file

@ -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

View file

@ -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

View 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