mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 13:50:22 -07:00
zabbix_template: fixed idempotency issues (#49188)
This commit is contained in:
parent
a914f494a8
commit
a9aa1053a8
3 changed files with 38 additions and 20 deletions
4
changelogs/fragments/48730-zabbix_hostmacro-fixes.yaml
Normal file
4
changelogs/fragments/48730-zabbix_hostmacro-fixes.yaml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- zabbix_hostmacro - Added missing validate_certs logic for running module against Zabbix servers with untrused SSL certificates (https://github.com/ansible/ansible/issues/47611)
|
||||||
|
- zabbix_hostmacro - Fixed support for user macros with context (https://github.com/ansible/ansible/issues/46953)
|
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- zabbix_template - Fixed idempotency of the module when using ``link_templates``, ``macros`` or ``template_json`` options (https://github.com/ansible/ansible/issues/48337)
|
|
@ -1,21 +1,9 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# (c) 2017, sookido
|
# (c) 2017, sookido
|
||||||
#
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
# This file is part of Ansible
|
|
||||||
#
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
@ -26,7 +14,7 @@ ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
module: zabbix_template
|
module: zabbix_template
|
||||||
short_description: create/delete/dump zabbix template
|
short_description: create/delete/dump zabbix template
|
||||||
description:
|
description:
|
||||||
|
@ -174,6 +162,7 @@ EXAMPLES = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
|
---
|
||||||
template_json:
|
template_json:
|
||||||
description: The JSON dump of the template
|
description: The JSON dump of the template
|
||||||
returned: when state is dump
|
returned: when state is dump
|
||||||
|
@ -350,6 +339,18 @@ class Template(object):
|
||||||
unwanted_keys = set(template_json['zabbix_export']) - keep_keys
|
unwanted_keys = set(template_json['zabbix_export']) - keep_keys
|
||||||
for unwanted_key in unwanted_keys:
|
for unwanted_key in unwanted_keys:
|
||||||
del template_json['zabbix_export'][unwanted_key]
|
del template_json['zabbix_export'][unwanted_key]
|
||||||
|
|
||||||
|
# Versions older than 2.4 do not support description field within template
|
||||||
|
desc_not_supported = False
|
||||||
|
if LooseVersion(self._zapi.api_version()).version[:2] < LooseVersion('2.4').version:
|
||||||
|
desc_not_supported = True
|
||||||
|
|
||||||
|
# Filter empty attributes from template object to allow accurate comparison
|
||||||
|
for template in template_json['zabbix_export']['templates']:
|
||||||
|
for key in template.keys():
|
||||||
|
if not template[key] or (key == 'description' and desc_not_supported):
|
||||||
|
template.pop(key)
|
||||||
|
|
||||||
return template_json
|
return template_json
|
||||||
|
|
||||||
def load_json_template(self, template_json):
|
def load_json_template(self, template_json):
|
||||||
|
@ -516,7 +517,12 @@ def main():
|
||||||
elif state == "present":
|
elif state == "present":
|
||||||
child_template_ids = None
|
child_template_ids = None
|
||||||
if link_templates is not None:
|
if link_templates is not None:
|
||||||
child_template_ids = template.get_template_ids(link_templates)
|
existing_child_templates = None
|
||||||
|
if existing_template_json:
|
||||||
|
existing_child_templates = set(list(
|
||||||
|
tmpl['name'] for tmpl in existing_template_json['zabbix_export']['templates'][0]['templates']))
|
||||||
|
if not existing_template_json or set(link_templates) != existing_child_templates:
|
||||||
|
child_template_ids = template.get_template_ids(link_templates)
|
||||||
|
|
||||||
clear_template_ids = []
|
clear_template_ids = []
|
||||||
if clear_templates is not None:
|
if clear_templates is not None:
|
||||||
|
@ -534,9 +540,14 @@ def main():
|
||||||
macros = None
|
macros = None
|
||||||
if template_macros is not None:
|
if template_macros is not None:
|
||||||
existing_macros = None
|
existing_macros = None
|
||||||
|
# zabbix configuration.export does not differentiate python types (numbers are returned as strings)
|
||||||
|
for macroitem in template_macros:
|
||||||
|
for key in macroitem:
|
||||||
|
macroitem[key] = str(macroitem[key])
|
||||||
|
|
||||||
if existing_template_json:
|
if existing_template_json:
|
||||||
existing_macros = set(existing_template_json['zabbix_export']['templates'][0]['macros'])
|
existing_macros = existing_template_json['zabbix_export']['templates'][0]['macros']
|
||||||
if not existing_macros or set(template_macros) != existing_macros:
|
if not existing_macros or template_macros != existing_macros:
|
||||||
macros = template_macros
|
macros = template_macros
|
||||||
|
|
||||||
if not template_ids:
|
if not template_ids:
|
||||||
|
@ -551,7 +562,7 @@ def main():
|
||||||
clear_template_ids, macros,
|
clear_template_ids, macros,
|
||||||
existing_template_json)
|
existing_template_json)
|
||||||
module.exit_json(changed=changed,
|
module.exit_json(changed=changed,
|
||||||
result="Successfully updateed template: %s" %
|
result="Successfully updated template: %s" %
|
||||||
template_name)
|
template_name)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue