@ -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,6 +517,11 @@ 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 :
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 )
child_template_ids = template . get_template_ids ( link_templates )
clear_template_ids = [ ]
clear_template_ids = [ ]
@ -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 update e d template: %s " %
result = " Successfully update d template: %s " %
template_name )
template_name )