Skip to content

Commit

Permalink
Add append option to zabbix_maintenance module (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
notok authored Dec 31, 2024
1 parent 470ead0 commit f97701e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/pr_1438.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- zabbix_maintenance - Added ability to append host or host groups to existing maintenance.
32 changes: 22 additions & 10 deletions plugins/modules/zabbix_maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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"]
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/targets/test_zabbix_maintenance/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f97701e

Please sign in to comment.