From 2133f0821ab4a402fb6f2907e087cbb4415a4f70 Mon Sep 17 00:00:00 2001 From: Dusan Matejka Date: Sat, 26 Jan 2019 13:15:32 +0100 Subject: [PATCH] zabbix_template: Fixed interactions between options and data within JSON object (#51222) --- .../50834-50833-zabbix_template-json.yaml | 6 +++++ .../monitoring/zabbix/zabbix_template.py | 24 ++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/50834-50833-zabbix_template-json.yaml diff --git a/changelogs/fragments/50834-50833-zabbix_template-json.yaml b/changelogs/fragments/50834-50833-zabbix_template-json.yaml new file mode 100644 index 0000000000..dff2559ddf --- /dev/null +++ b/changelogs/fragments/50834-50833-zabbix_template-json.yaml @@ -0,0 +1,6 @@ +--- +minor_changes: + - zabbix_template - Module no longer requires ``template_name`` to be provided when importing with ``template_json`` option (https://github.com/ansible/ansible/issues/50833) +bugfixes: + - zabbix_template - Fixed cryptic error when ``template_groups`` option wasn't provided (https://github.com/ansible/ansible/issues/50834) + - zabbix_template - Failed template import will no longer leave empty templates configured on Zabbix server diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py index 6eb1bc8143..96d918d776 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py @@ -30,7 +30,8 @@ options: template_name: description: - Name of Zabbix template. - required: true + - Required when I(template_json) is not used. + required: false template_json: description: - JSON dump of template to import. @@ -38,6 +39,7 @@ options: template_groups: description: - List of template groups to create or delete. + - Required when I(template_name) is used and C(state=present). required: false link_templates: description: @@ -253,12 +255,14 @@ class Template(object): child_template_ids, macros): if self._module.check_mode: self._module.exit_json(changed=True) - self._zapi.template.create({'host': template_name, - 'groups': group_ids, - 'templates': child_template_ids, - 'macros': macros}) + if template_json: self.import_template(template_json, template_name) + else: + self._zapi.template.create({'host': template_name, + 'groups': group_ids, + 'templates': child_template_ids, + 'macros': macros}) def update_template(self, templateids, template_json, group_ids, child_template_ids, @@ -448,7 +452,7 @@ def main(): http_login_password=dict(type='str', required=False, default=None, no_log=True), validate_certs=dict(type='bool', required=False, default=True), - template_name=dict(type='str', required=True), + template_name=dict(type='str', required=False), template_json=dict(type='json', required=False), template_groups=dict(type='list', required=False), link_templates=dict(type='list', required=False), @@ -458,6 +462,7 @@ def main(): 'dump']), timeout=dict(type='int', default=10) ), + required_one_of=[['template_name', 'template_json']], supports_check_mode=True ) @@ -492,6 +497,13 @@ def main(): module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) template = Template(module, zbx) + if template_json and not template_name: + # Ensure template_name is not empty for safety check in import_template + template_loaded = template.load_json_template(template_json) + template_name = template_loaded['zabbix_export']['templates'][0]['template'] + elif template_name and not template_groups and state == 'present': + module.fail_json(msg="Option template_groups is required when template_json is not used") + template_ids = template.get_template_ids([template_name]) existing_template_json = None if template_ids: