From a97618b486bd0d0c3990a36b1b130bb7bfd5583e Mon Sep 17 00:00:00 2001 From: MedicMomcilo Date: Fri, 13 Jul 2018 21:54:38 +0200 Subject: [PATCH] Add option to control CDATA tags stripping (#42689) * Add option to control CDATA tags stripping * Fix remaining issues before merging. * Add missing trailing dot. --- lib/ansible/modules/files/xml.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/files/xml.py b/lib/ansible/modules/files/xml.py index 464812234e8..9a066ec3340 100644 --- a/lib/ansible/modules/files/xml.py +++ b/lib/ansible/modules/files/xml.py @@ -106,6 +106,13 @@ options: the original file back if you somehow clobbered it incorrectly. type: bool default: 'no' + strip_cdata_tags: + description: + - Remove CDATA tags surrounding text values. + - Note that this might break your XML file if text values contain characters that could be interpreted as XML. + type: bool + default: 'no' + version_added: '2.7' requirements: - lxml >= 2.3.0 notes: @@ -732,6 +739,7 @@ def main(): content=dict(type='str', choices=['attribute', 'text']), input_type=dict(type='str', default='yaml', choices=['xml', 'yaml']), backup=dict(type='bool', default=False), + strip_cdata_tags=dict(type='bool', default=False), ), supports_check_mode=True, # TODO: Implement this as soon as #28662 (required_by functionality) is merged @@ -772,6 +780,7 @@ def main(): print_match = module.params['print_match'] count = module.params['count'] backup = module.params['backup'] + strip_cdata_tags = module.params['strip_cdata_tags'] # Check if we have lxml 2.3.0 or newer installed if not HAS_LXML: @@ -800,7 +809,7 @@ def main(): # Try to parse in the target XML file try: - parser = etree.XMLParser(remove_blank_text=pretty_print) + parser = etree.XMLParser(remove_blank_text=pretty_print, strip_cdata=strip_cdata_tags) doc = etree.parse(infile, parser) except etree.XMLSyntaxError as e: module.fail_json(msg="Error while parsing document: %s (%s)" % (xml_file or 'xml_string', e))