Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add append option to zabbix_maintenance module #1438

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 @@
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(

Check warning on line 473 in plugins/modules/zabbix_maintenance.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/zabbix_maintenance.py#L473

Added line #L473 was not covered by tests
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
Loading