From 2242c385b25cc6fdcaaa53560c7f56f819bafe23 Mon Sep 17 00:00:00 2001 From: sky-joker Date: Tue, 26 Nov 2019 20:18:55 +0900 Subject: [PATCH] Zabbix: zabbix_template: fix #63907 and add test case (#65236) * Zabbix: zabbix_template: fix https://github.com/ansible/ansible/issues/63907 and add test case * update test case --- .../monitoring/zabbix/zabbix_template.py | 3 + .../targets/zabbix_template/aliases | 5 + .../targets/zabbix_template/defaults/main.yml | 5 + .../targets/zabbix_template/meta/main.yml | 2 + .../targets/zabbix_template/tasks/main.yml | 159 ++++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 test/integration/targets/zabbix_template/aliases create mode 100644 test/integration/targets/zabbix_template/defaults/main.yml create mode 100644 test/integration/targets/zabbix_template/meta/main.yml create mode 100644 test/integration/targets/zabbix_template/tasks/main.yml diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py index 386b19e91e2..703073a5c34 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py @@ -91,6 +91,7 @@ options: dump_format: description: - Format to use when dumping template with C(state=dump). + - This option is deprecated and will eventually be removed in 2.14. required: false choices: [json, xml] default: "json" @@ -101,6 +102,7 @@ options: - On C(state=present) template will be created/imported or updated depending if it is already present. - On C(state=dump) template content will get dumped into required format specified in I(dump_format). - On C(state=absent) template will be deleted. + - The C(state=dump) is deprecated and will eventually be removed in 2.14. The M(zabbix_template_info) module should be used instead. required: false choices: [present, absent, dump] default: "present" @@ -681,6 +683,7 @@ def main(): module.exit_json(changed=True, result="Successfully deleted template %s" % template_name) elif state == "dump": + module.deprecate("The 'dump' state has been deprecated and will be removed, use 'zabbix_template_info' module instead.", version='2.14') if not template_ids: module.fail_json(msg='Template not found: %s' % template_name) diff --git a/test/integration/targets/zabbix_template/aliases b/test/integration/targets/zabbix_template/aliases new file mode 100644 index 00000000000..3d0091e7a94 --- /dev/null +++ b/test/integration/targets/zabbix_template/aliases @@ -0,0 +1,5 @@ +destructive +shippable/posix/group1 +skip/osx +skip/freebsd +skip/rhel diff --git a/test/integration/targets/zabbix_template/defaults/main.yml b/test/integration/targets/zabbix_template/defaults/main.yml new file mode 100644 index 00000000000..5482107368d --- /dev/null +++ b/test/integration/targets/zabbix_template/defaults/main.yml @@ -0,0 +1,5 @@ +--- + +zabbix_server_url: http://127.0.0.1/zabbix/ +zabbix_login_user: Admin +zabbix_login_password: zabbix diff --git a/test/integration/targets/zabbix_template/meta/main.yml b/test/integration/targets/zabbix_template/meta/main.yml new file mode 100644 index 00000000000..d30074784c9 --- /dev/null +++ b/test/integration/targets/zabbix_template/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_zabbix diff --git a/test/integration/targets/zabbix_template/tasks/main.yml b/test/integration/targets/zabbix_template/tasks/main.yml new file mode 100644 index 00000000000..00f2a0333f4 --- /dev/null +++ b/test/integration/targets/zabbix_template/tasks/main.yml @@ -0,0 +1,159 @@ +--- +- when: + - ansible_distribution == 'Ubuntu' + block: + - name: Create a new Zabbix template. + zabbix_template: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + template_groups: + - 'Linux servers' + - 'Templates' + state: present + register: create_zabbix_template_result + + - assert: + that: + - create_zabbix_template_result.changed is sameas true + + - name: Gather Zabbix template infomation. + zabbix_template_info: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + format: json + register: gather_template_result + + - assert: + that: + - gather_template_result.template_json.zabbix_export.groups.0.name == 'Linux servers' + - gather_template_result.template_json.zabbix_export.groups.1.name == 'Templates' + + - name: Add link_templates to Zabbix template. + zabbix_template: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + template_groups: + - 'Linux servers' + - 'Templates' + link_templates: + - 'Template App Zabbix Proxy' + - 'Template App Zabbix Agent' + state: present + register: update_zabbix_template_result + + - assert: + that: + - create_zabbix_template_result.changed is sameas true + + - name: Gather Zabbix template infomation. + zabbix_template_info: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + format: json + register: gather_template_result + + - assert: + that: + - gather_template_result.template_json.zabbix_export.groups.0.name == 'Linux servers' + - gather_template_result.template_json.zabbix_export.groups.1.name == 'Templates' + - gather_template_result.template_json.zabbix_export.templates.0.templates.0.name == 'Template App Zabbix Agent' + - gather_template_result.template_json.zabbix_export.templates.0.templates.1.name == 'Template App Zabbix Proxy' + + - name: Add macros to Zabbix template. + zabbix_template: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + template_groups: + - 'Linux servers' + - 'Templates' + link_templates: + - 'Template App Zabbix Proxy' + - 'Template App Zabbix Agent' + macros: + - macro: '{$EXAMPLE_MACRO1}' + value: 1000 + - macro: '{$EXAMPLE_MACRO2}' + value: 'text' + state: present + register: update_zabbix_template_result + + - assert: + that: + - create_zabbix_template_result.changed is sameas true + + - name: Gather Zabbix template infomation. + zabbix_template_info: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + format: json + register: gather_template_result + + - assert: + that: + - gather_template_result.template_json.zabbix_export.groups.0.name == 'Linux servers' + - gather_template_result.template_json.zabbix_export.groups.1.name == 'Templates' + - gather_template_result.template_json.zabbix_export.templates.0.templates.0.name == 'Template App Zabbix Agent' + - gather_template_result.template_json.zabbix_export.templates.0.templates.1.name == 'Template App Zabbix Proxy' + - gather_template_result.template_json.zabbix_export.templates.0.macros.0.macro == '{$EXAMPLE_MACRO1}' + - gather_template_result.template_json.zabbix_export.templates.0.macros.0.value == '1000' + - gather_template_result.template_json.zabbix_export.templates.0.macros.1.macro == '{$EXAMPLE_MACRO2}' + - gather_template_result.template_json.zabbix_export.templates.0.macros.1.value == 'text' + + - name: Dump Zabbix template to XML format. + zabbix_template: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + dump_format: xml + state: dump + register: template_dump_result + + - debug: var=template_dump_result + + - assert: + that: + - template_dump_result.deprecations is defined + - template_dump_result.deprecations.0.version == '2.14' + + - name: Dump Zabbix template to JSON format. + zabbix_template: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + dump_format: json + state: dump + register: template_dump_result + + - debug: var=template_dump_result + + - assert: + that: + - template_dump_result.deprecations is defined + - template_dump_result.deprecations.0.version == '2.14' + + - name: Delete Zabbix template. + zabbix_template: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + template_name: ExampleHost + state: absent + register: delete_zabbix_template_result + + - assert: + that: + - delete_zabbix_template_result.changed is sameas true