From cfe7d8f2f72fdec380de8f932ffe6628029b2f72 Mon Sep 17 00:00:00 2001 From: notok Date: Fri, 27 Dec 2024 12:07:46 +0900 Subject: [PATCH] Add append option to zabbix_maintenance module --- changelogs/fragments/pr_1438.yml | 2 ++ plugins/modules/zabbix_maintenance.py | 32 +++++++++++------ .../test_zabbix_maintenance/tasks/main.yml | 34 +++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/pr_1438.yml diff --git a/changelogs/fragments/pr_1438.yml b/changelogs/fragments/pr_1438.yml new file mode 100644 index 000000000..2c2359936 --- /dev/null +++ b/changelogs/fragments/pr_1438.yml @@ -0,0 +1,2 @@ +minor_changes: + - zabbix_maintenance - Added ability to append host or host groups to existing maintenance. diff --git a/plugins/modules/zabbix_maintenance.py b/plugins/modules/zabbix_maintenance.py index ce10815df..5a2df0c36 100644 --- a/plugins/modules/zabbix_maintenance.py +++ b/plugins/modules/zabbix_maintenance.py @@ -38,6 +38,11 @@ aliases: [ "host_group" ] type: list elements: str + append: + description: + - Whether to append hosts and host groups to the existing maintenance. + type: bool + default: false minutes: description: - Length of maintenance window in minutes. @@ -370,6 +375,7 @@ def main(): minutes=dict(type="int", required=False, default=10), host_groups=dict(type="list", required=False, default=None, aliases=["host_group"], elements="str"), + append=dict(type="bool", required=False, default=False), name=dict(type="str", required=True), desc=dict(type="str", required=False, default="Created by Ansible"), collect_data=dict(type="bool", required=False, default=True), @@ -396,6 +402,7 @@ def main(): host_names = module.params["host_names"] host_groups = module.params["host_groups"] + append = module.params["append"] state = module.params["state"] minutes = module.params["minutes"] name = module.params["name"] @@ -448,18 +455,23 @@ def main(): module.fail_json( msg="Failed to check maintenance %s existence: %s" % (name, error)) - if maintenance and maint.check_maint_properties(maintenance, group_ids, host_ids, maintenance_type, - start_time, period, desc, tags): - if module.check_mode: - changed = True - else: - (rc, data, error) = maint.update_maintenance( - maintenance["maintenanceid"], group_ids, host_ids, start_time, maintenance_type, period, desc, tags) - if rc == 0: + if maintenance: + if append: + group_ids = list(set(group_ids + maintenance["groupids"])) + host_ids = list(set(host_ids + maintenance["hostids"])) + + if maint.check_maint_properties(maintenance, group_ids, host_ids, maintenance_type, + start_time, period, desc, tags): + if module.check_mode: changed = True else: - module.fail_json( - msg="Failed to update maintenance: %s" % error) + (rc, data, error) = maint.update_maintenance( + maintenance["maintenanceid"], group_ids, host_ids, start_time, maintenance_type, period, desc, tags) + if rc == 0: + changed = True + else: + module.fail_json( + msg="Failed to update maintenance: %s" % error) if not maintenance: if module.check_mode: diff --git a/tests/integration/targets/test_zabbix_maintenance/tasks/main.yml b/tests/integration/targets/test_zabbix_maintenance/tasks/main.yml index 86d511d36..ebdc70eb0 100644 --- a/tests/integration/targets/test_zabbix_maintenance/tasks/main.yml +++ b/tests/integration/targets/test_zabbix_maintenance/tasks/main.yml @@ -323,6 +323,40 @@ that: - create_maintenance_active_till_again_result.changed is sameas false +- name: "test - Create maintenance with an append param" + community.zabbix.zabbix_maintenance: + name: maintenance + host_names: + - Zabbix server + host_groups: + - Linux servers + append: true + active_since: "1979-09-19 00:00" + active_till: "1979-09-19 23:59" + state: present + register: create_maintenance_append_result + +- ansible.builtin.assert: + that: + - create_maintenance_append_result.changed is sameas true + +- name: "test - Create maintenance with an append param(again - expectations: no change will occur)" + community.zabbix.zabbix_maintenance: + name: maintenance + host_names: + - Zabbix server + host_groups: + - Linux servers + append: true + active_since: "1979-09-19 00:00" + active_till: "1979-09-19 23:59" + state: present + register: create_maintenance_append_again_result + +- ansible.builtin.assert: + that: + - create_maintenance_append_again_result.changed is sameas false + - name: "test - Delete maintenance" community.zabbix.zabbix_maintenance: name: maintenance