Skip to content

Commit 0243eab

Browse files
Fix xml reports changed when node is not deleted (ansible-collections#1007)
* Fix xml reports changed when node is not deleted * Added changelog fragment * Added tests for xml no change remove * Added PR to changeling fragment Co-authored-by: Felix Fontein <[email protected]> Co-authored-by: Felix Fontein <[email protected]>
1 parent 75d1894 commit 0243eab

File tree

8 files changed

+131
-2
lines changed

8 files changed

+131
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- xml - fixed issue were changed was returned when removing non-existent xpath (https://github.com/ansible-collections/community.general/pull/1007).

plugins/modules/files/xml.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ def xpath_matches(tree, xpath, namespaces):
411411

412412
def delete_xpath_target(module, tree, xpath, namespaces):
413413
""" Delete an attribute or element from a tree """
414+
changed = False
414415
try:
415416
for result in tree.xpath(xpath, namespaces=namespaces):
417+
changed = True
416418
# Get the xpath for this result
417419
if is_attribute(tree, xpath, namespaces):
418420
# Delete an attribute
@@ -429,7 +431,7 @@ def delete_xpath_target(module, tree, xpath, namespaces):
429431
except Exception as e:
430432
module.fail_json(msg="Couldn't delete xpath target: %s (%s)" % (xpath, e))
431433
else:
432-
finish(module, tree, xpath, namespaces, changed=True)
434+
finish(module, tree, xpath, namespaces, changed=changed)
433435

434436

435437
def replace_children_of(children, match):

tests/integration/targets/xml/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
- include_tasks: test-count.yml
4444
- include_tasks: test-mutually-exclusive-attributes.yml
4545
- include_tasks: test-remove-attribute.yml
46+
- include_tasks: test-remove-attribute-nochange.yml
4647
- include_tasks: test-remove-element.yml
48+
- include_tasks: test-remove-element-nochange.yml
4749
- include_tasks: test-set-attribute-value.yml
4850
- include_tasks: test-set-children-elements.yml
4951
- include_tasks: test-set-children-elements-level.yml
@@ -53,6 +55,7 @@
5355
- include_tasks: test-pretty-print-only.yml
5456
- include_tasks: test-add-namespaced-children-elements.yml
5557
- include_tasks: test-remove-namespaced-attribute.yml
58+
- include_tasks: test-remove-namespaced-attribute-nochange.yml
5659
- include_tasks: test-set-namespaced-attribute-value.yml
5760
- include_tasks: test-set-namespaced-element-value.yml
5861
- include_tasks: test-set-namespaced-children-elements.yml
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
- name: Setup test fixture
3+
copy:
4+
src: results/test-remove-attribute.xml
5+
dest: /tmp/ansible-xml-beers.xml
6+
7+
8+
- name: Remove non-existing '/business/rating/@subjective'
9+
xml:
10+
path: /tmp/ansible-xml-beers.xml
11+
xpath: /business/rating/@subjective
12+
state: absent
13+
register: remove_attribute
14+
15+
- name: Compare to expected result
16+
copy:
17+
src: results/test-remove-attribute.xml
18+
dest: /tmp/ansible-xml-beers.xml
19+
check_mode: yes
20+
diff: yes
21+
register: comparison
22+
23+
- name: Test expected result
24+
assert:
25+
that:
26+
- remove_attribute.changed == false
27+
- comparison.changed == false # identical
28+
#command: diff -u {{ role_path }}/results/test-remove-attribute.xml /tmp/ansible-xml-beers.xml
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
- name: Setup test fixture
3+
copy:
4+
src: results/test-remove-element.xml
5+
dest: /tmp/ansible-xml-beers.xml
6+
7+
8+
- name: Remove non-existing '/business/rating'
9+
xml:
10+
path: /tmp/ansible-xml-beers.xml
11+
xpath: /business/rating
12+
state: absent
13+
register: remove_element
14+
15+
- name: Compare to expected result
16+
copy:
17+
src: results/test-remove-element.xml
18+
dest: /tmp/ansible-xml-beers.xml
19+
check_mode: yes
20+
diff: yes
21+
register: comparison
22+
23+
- name: Test expected result
24+
assert:
25+
that:
26+
- remove_element.changed == false
27+
- comparison.changed == false # identical
28+
#command: diff -u {{ role_path }}/results/test-remove-element.xml /tmp/ansible-xml-beers.xml
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
- name: Setup test fixture
3+
copy:
4+
src: results/test-remove-namespaced-attribute.xml
5+
dest: /tmp/ansible-xml-namespaced-beers.xml
6+
7+
8+
- name: Remove non-existing namespaced '/bus:business/rat:rating/@attr:subjective'
9+
xml:
10+
path: /tmp/ansible-xml-namespaced-beers.xml
11+
xpath: /bus:business/rat:rating/@attr:subjective
12+
namespaces:
13+
bus: http://test.business
14+
ber: http://test.beers
15+
rat: http://test.rating
16+
attr: http://test.attribute
17+
state: absent
18+
register: remove_namespaced_attribute
19+
20+
- name: Compare to expected result
21+
copy:
22+
src: results/test-remove-namespaced-attribute.xml
23+
dest: /tmp/ansible-xml-namespaced-beers.xml
24+
check_mode: yes
25+
diff: yes
26+
register: comparison
27+
28+
- name: Test expected result
29+
assert:
30+
that:
31+
- remove_namespaced_attribute.changed == false
32+
- comparison.changed == false # identical
33+
#command: diff -u {{ role_path }}/results/test-remove-namespaced-attribute.xml /tmp/ansible-xml-namespaced-beers.xml

tests/integration/targets/xml/tasks/test-remove-namespaced-attribute.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
- name: Test expected result
2929
assert:
3030
that:
31-
- remove_element.changed == true
31+
- remove_namespaced_attribute.changed == true
3232
- comparison.changed == false # identical
3333
#command: diff -u {{ role_path }}/results/test-remove-namespaced-attribute.xml /tmp/ansible-xml-namespaced-beers.xml
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
- name: Setup test fixture
3+
copy:
4+
src: results/test-remove-element.xml
5+
dest: /tmp/ansible-xml-namespaced-beers.xml
6+
7+
8+
- name: Remove non-existing namespaced '/bus:business/rat:rating'
9+
xml:
10+
path: /tmp/ansible-xml-namespaced-beers.xml
11+
xpath: /bus:business/rat:rating
12+
namespaces:
13+
bus: http://test.business
14+
ber: http://test.beers
15+
rat: http://test.rating
16+
attr: http://test.attribute
17+
state: absent
18+
register: remove_namespaced_element
19+
20+
- name: Compare to expected result
21+
copy:
22+
src: results/test-remove-element.xml
23+
dest: /tmp/ansible-xml-namespaced-beers.xml
24+
check_mode: yes
25+
diff: yes
26+
register: comparison
27+
28+
- name: Test expected result
29+
assert:
30+
that:
31+
- remove_namespaced_element.changed == false
32+
- comparison.changed == false # identical
33+
#command: diff -u {{ role_path }}/results/test-remove-element.xml /tmp/ansible-xml-namespaced-beers.xml

0 commit comments

Comments
 (0)