ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
827 stars 1.52k forks source link

Add option to Ansible xml module to preserve empty element tag formatting #8361

Open AndriyRu opened 5 months ago

AndriyRu commented 5 months ago

Summary

Summary This feature request proposes adding an option to the Ansible xml module that allows users to preserve the formatting of empty XML element tags (keeping open and close tags) when modifying an XML document.

Use Case Some XML consumers and legacy systems require empty XML elements to maintain the full open and close tag format () rather than being converted to a self-closing tag (). The current behavior of the Ansible xml module, which relies on the lxml library, is to convert empty elements to self-closing tags by default during serialization.

For example, after using the xml module to modify an unrelated part of an XML document, the original tag:

<MyTag></MyTag>

gets serialized as:

<MyTag/>

This change in format can lead to issues with systems that expect or require the former format.

Proposed Solution Introduce a new boolean option for the xml module, tentatively named preserve_empty_tags, which when set to True, would keep empty XML elements in their expanded form (with both an opening and a closing tag). This is akin to the short_empty_elements=False parameter available in both Python's ElementTree and lxml.etree.

Implementation This new option could be implemented within the xml module's code where the final tree.write() method is called. It would conditionally include the short_empty_elements=False option based on the value of preserve_empty_tags.

Benefits By enabling users to preserve XML tag formatting, this feature would improve compatibility with various XML parsers and prevent potential issues when interfacing with systems where the formatting of empty tags is significant.

Additional Context The short_empty_elements option is available starting from lxml version 4.4.0, and therefore the xml module can check the installed lxml version before applying this new feature to maintain backward compatibility.

Issue Type

Feature Idea

Component Name

xml

Additional Information

- name: Edit xml file
  hosts: localhost
  gather_facts: no
  become: false
  tasks:
    - name: Edit xml file dynamically
      xml:
        path: "/path/to/the/file"
        xpath: /MainSettings/Store/IsOpened
        value: 'false'
        pretty_print: true
        preserve_empty_tags: true
        state: present

Code of Conduct

ansibullbot commented 5 months ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 5 months ago

cc @cmprescott @dagwieers @sm4rk0 @tbielawa click here for bot help

zerwes commented 2 months ago

even more confusing ... if you set multiple tags, the last one is NOT shortened in case it is empty https://gist.github.com/zerwes/356a8cc901ac315c9b04841ea63a3b8c a configurable consistent behavior would be desirable (no need to pray idempotence paradigm if the bowels do not behave ...)