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 <D3DeFi@users.noreply.github.com>

* Update changelogs/fragments/66463-zabbix_template-fix-error-linktemplate-and-importdump.yml

Co-Authored-By: Dusan Matejka <D3DeFi@users.noreply.github.com>

Co-authored-by: Dusan Matejka <D3DeFi@users.noreply.github.com>
This commit is contained in:
Markus Fischbacher 2020-01-23 14:22:19 +01:00 committed by ansibot
parent eb3d081c11
commit e646bd08e1
2 changed files with 15 additions and 1 deletions

View file

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

View file

@ -406,11 +406,17 @@ class Template(object):
if set(template_groups) != set(existing_groups): if set(template_groups) != set(existing_groups):
changed = True 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 # 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']] exist_child_templates = [t['name'] for t in existing_template['zabbix_export']['templates'][0]['templates']]
if link_templates is not None: if link_templates is not None:
if set(link_templates) != set(exist_child_templates): if set(link_templates) != set(exist_child_templates):
changed = True 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 # Mark that there will be changes when at least one existing template will be unlinked
if clear_templates is not None: if clear_templates is not None:
@ -433,6 +439,8 @@ class Template(object):
if link_template_ids is not None: if link_template_ids is not None:
template_changes.update({'templates': link_template_ids}) template_changes.update({'templates': link_template_ids})
else:
template_changes.update({'templates': []})
if clear_template_ids is not None: if clear_template_ids is not None:
template_changes.update({'templates_clear': clear_template_ids}) template_changes.update({'templates_clear': clear_template_ids})
@ -522,7 +530,7 @@ class Template(object):
template.remove(element) template.remove(element)
# Filter new lines and indentation # 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) return ''.join(xml_root_text)
def load_json_template(self, template_json): def load_json_template(self, template_json):