Added support to disable xml declartion with full_document flag (#237)
parent
09362a2d98
commit
fca1229097
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
minor_changes:
|
||||
- >-
|
||||
to_xml - Added support to disable xml declartion with full_document flag.
|
|
@ -73,6 +73,27 @@ Parameters
|
|||
<div>Conversion library to use within the filter plugin.</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="parameter-"></div>
|
||||
<b>full_document</b>
|
||||
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
|
||||
<div style="font-size: small">
|
||||
<span style="color: purple">boolean</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<ul style="margin: 0; padding: 0"><b>Choices:</b>
|
||||
<li>no</li>
|
||||
<li><div style="color: blue"><b>yes</b> ←</div></li>
|
||||
</ul>
|
||||
</td>
|
||||
<td>
|
||||
</td>
|
||||
<td>
|
||||
<div>The option to disable xml declaration(defaults to True).</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="parameter-"></div>
|
||||
|
|
|
@ -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"""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) }}"
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
---
|
||||
output: "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<interface-configurations xmlns=\"http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg\">\n\t<interface-configuration></interface-configuration>\n</interface-configurations>"
|
||||
output1: '<interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>'
|
||||
|
|
Loading…
Reference in New Issue