From e646bd08e1f77b0e2c535c1b3d577ef49df7a41a Mon Sep 17 00:00:00 2001 From: Markus Fischbacher <453368+rockaut@users.noreply.github.com> Date: Thu, 23 Jan 2020 14:22:19 +0100 Subject: [PATCH] zabbix_template - fixing errors linked templates and dump/imports (#66463) * Fixing errors on empty linked templates fixes #66417 Line 409 fixes KeyError if existing template has no linked templates - as Zabbix API isn't returning an empty array either. Line 417 is needed to mark as changed if new/modified template has no linked but existing one has. Line 442 is needed to actually update even if link_templates was None. * decode parsed xml root fixes #66466 Line 533: without .decode() the ET.tostring on python3 returns byte-like object * Create 66463-zabbix_template-fix-error-linktemplate-and-importdump.yml * Update changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml Co-Authored-By: Dusan Matejka * Update changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml Co-Authored-By: Dusan Matejka Co-authored-by: Dusan Matejka --- ..._template-fix-error-linktemplate-and-importdump.yml | 6 ++++++ .../modules/monitoring/zabbix/zabbix_template.py | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml diff --git a/changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml b/changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml new file mode 100644 index 00000000000..187feee4c3c --- /dev/null +++ b/changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml @@ -0,0 +1,6 @@ +--- + +bugfixes: + - zabbix_template - fixed error when providing empty ``link_templates`` to the module (see https://github.com/ansible/ansible/issues/66417) + - zabbix_template - fixed invalid (non-importable) output provided by exporting XML (see https://github.com/ansible/ansible/issues/66466) + diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py index ae60cfa82f4..59446455076 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py @@ -406,11 +406,17 @@ class Template(object): if set(template_groups) != set(existing_groups): changed = True + if 'templates' not in existing_template['zabbix_export']['templates'][0]: + existing_template['zabbix_export']['templates'][0]['templates'] = [] + # Check if any new templates would be linked or any existing would be unlinked exist_child_templates = [t['name'] for t in existing_template['zabbix_export']['templates'][0]['templates']] if link_templates is not None: if set(link_templates) != set(exist_child_templates): changed = True + else: + if set([]) != set(exist_child_templates): + changed = True # Mark that there will be changes when at least one existing template will be unlinked if clear_templates is not None: @@ -433,6 +439,8 @@ class Template(object): if link_template_ids is not None: template_changes.update({'templates': link_template_ids}) + else: + template_changes.update({'templates': []}) if clear_template_ids is not None: template_changes.update({'templates_clear': clear_template_ids}) @@ -522,7 +530,7 @@ class Template(object): template.remove(element) # Filter new lines and indentation - xml_root_text = list(line.strip() for line in ET.tostring(parsed_xml_root).split('\n')) + xml_root_text = list(line.strip() for line in ET.tostring(parsed_xml_root, encoding='utf8', method='xml').decode().split('\n')) return ''.join(xml_root_text) def load_json_template(self, template_json):