diff --git a/changelogs/fragments/to_xml_disable_xml_declaration.yaml b/changelogs/fragments/to_xml_disable_xml_declaration.yaml new file mode 100644 index 0000000..711b2a5 --- /dev/null +++ b/changelogs/fragments/to_xml_disable_xml_declaration.yaml @@ -0,0 +1,4 @@ +--- +minor_changes: + - >- + to_xml - Added support to disable xml declartion with full_document flag. diff --git a/docs/ansible.utils.to_xml_filter.rst b/docs/ansible.utils.to_xml_filter.rst index 022fcb2..8e7a5c1 100644 --- a/docs/ansible.utils.to_xml_filter.rst +++ b/docs/ansible.utils.to_xml_filter.rst @@ -73,6 +73,27 @@ Parameters
Conversion library to use within the filter plugin.
+ + +
+ full_document + +
+ boolean +
+ + + + + + + +
The option to disable xml declaration(defaults to True).
+ +
diff --git a/plugins/filter/to_xml.py b/plugins/filter/to_xml.py index d8281e8..63a7500 100644 --- a/plugins/filter/to_xml.py +++ b/plugins/filter/to_xml.py @@ -48,6 +48,11 @@ DOCUMENTATION = """ - When indent="tabs", a single tab is always used for indentation. type: int default: 4 + full_document: + description: + - The option to disable xml declaration(defaults to True). + type: bool + default: True """ EXAMPLES = r""" diff --git a/plugins/plugin_utils/to_xml.py b/plugins/plugin_utils/to_xml.py index 1c3f882..e25c095 100644 --- a/plugins/plugin_utils/to_xml.py +++ b/plugins/plugin_utils/to_xml.py @@ -35,7 +35,7 @@ def _raise_error(msg): raise AnsibleFilterError(error) -def to_xml(data, engine, indent, indent_width): +def to_xml(data, engine, indent, indent_width, full_document): """Convert data which is in json to xml" :param data: The data passed in (data|to_xml(...)) @@ -43,6 +43,7 @@ def to_xml(data, engine, indent, indent_width): :param engine: Conversion library default=xmltodict :param indent: Indent char default='tabs' :param indent_width: Indent char multiplier default=4 + :param full_document: Flag to disable xml declaration """ indent_char = "\t" @@ -54,7 +55,12 @@ def to_xml(data, engine, indent, indent_width): if not HAS_XMLTODICT: _raise_error("Missing required library xmltodict") try: - res = xmltodict.unparse(data, pretty=True, indent=indent_char) + res = xmltodict.unparse( + data, + pretty=True, + indent=indent_char, + full_document=full_document, + ) except Exception: _raise_error("Input json is not valid") return res diff --git a/tests/integration/targets/utils_to_xml/tasks/simple.yaml b/tests/integration/targets/utils_to_xml/tasks/simple.yaml index 44cd57d..1cf90a2 100644 --- a/tests/integration/targets/utils_to_xml/tasks/simple.yaml +++ b/tests/integration/targets/utils_to_xml/tasks/simple.yaml @@ -34,3 +34,9 @@ that: "{{ msg in result.msg }}" vars: msg: "Error when using plugin 'to_xml': engine: dicttoxml is not supported" + +- name: Integration tests with and without default engine as xmltodict and + ansible.builtin.assert: + that: "{{ output1 == item.test }}" + loop: + - test: "{{ data|ansible.utils.to_xml('xmltodict', full_document=False) }}" diff --git a/tests/integration/targets/utils_to_xml/vars/main.yaml b/tests/integration/targets/utils_to_xml/vars/main.yaml index bf6f123..2dfb167 100644 --- a/tests/integration/targets/utils_to_xml/vars/main.yaml +++ b/tests/integration/targets/utils_to_xml/vars/main.yaml @@ -1,2 +1,3 @@ --- output: "\n\n\t\n" +output1: ''