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):