| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
# SPDX-FileCopyrightText: 2026 Christoph Fiehe <[email protected]> |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: icinga2_downtime |
| 11 |
short_description: Manages Icinga 2 downtimes |
| 12 |
version_added: "12.4.0" |
| 13 |
description: |
| 14 |
- Manages downtimes in Icinga 2 through its REST API. |
| 15 |
- Options as described at U(https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#schedule-downtime). |
| 16 |
author: |
| 17 |
- Christoph Fiehe (@cfiehe) |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
details: |
| 22 |
- In case of a complex filter expression, it may become very complex to decide |
| 23 |
whether downtime creation or removal will succeed and trigger a change. |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
all_services: |
| 28 |
description: |
| 29 |
- Whether downtimes should be created for all services of the matched host objects. |
| 30 |
- If omitted, Icinga 2 does not create downtimes for all services of the matched host objects by default. |
| 31 |
type: bool |
| 32 |
author: |
| 33 |
description: |
| 34 |
- Name of the author. |
| 35 |
type: str |
| 36 |
default: "Ansible" |
| 37 |
comment: |
| 38 |
description: |
| 39 |
- A descriptive comment. |
| 40 |
type: str |
| 41 |
default: Downtime scheduled by Ansible |
| 42 |
child_options: |
| 43 |
description: |
| 44 |
- Schedule child downtimes. |
| 45 |
type: str |
| 46 |
choices: ["DowntimeNoChildren", "DowntimeTriggeredChildren", "DowntimeNonTriggeredChildren"] |
| 47 |
duration: |
| 48 |
description: |
| 49 |
- Duration of the downtime. |
| 50 |
- Required in case of a flexible downtime. |
| 51 |
type: int |
| 52 |
end_time: |
| 53 |
description: |
| 54 |
- End time of the downtime as UNIX timestamp. |
| 55 |
type: int |
| 56 |
filter_vars: |
| 57 |
description: |
| 58 |
- Variable names and values used in the filter expression. |
| 59 |
type: dict |
| 60 |
filter: |
| 61 |
description: |
| 62 |
- Filter expression limiting the objects to operate on. |
| 63 |
type: str |
| 64 |
fixed: |
| 65 |
description: |
| 66 |
- Whether the downtime is fixed or flexible. |
| 67 |
- If omitted, Icinga 2 creates a fixed downtime by default. |
| 68 |
type: bool |
| 69 |
name: |
| 70 |
description: |
| 71 |
- Name of the downtime object. |
| 72 |
- This option has no effect for states other than V(absent). |
| 73 |
type: str |
| 74 |
object_type: |
| 75 |
description: |
| 76 |
- Use V(Host) for a host downtime and V(Service) for a service downtime. |
| 77 |
- Use V(Downtime) and give the name of the downtime object you want to remove. |
| 78 |
type: str |
| 79 |
choices: ["Service", "Host", "Downtime"] |
| 80 |
default: Host |
| 81 |
start_time: |
| 82 |
description: |
| 83 |
- Start time of the downtime as UNIX timestamp. |
| 84 |
type: int |
| 85 |
state: |
| 86 |
description: |
| 87 |
- State of the downtime. |
| 88 |
type: str |
| 89 |
choices: ["present", "absent"] |
| 90 |
default: present |
| 91 |
trigger_name: |
| 92 |
description: |
| 93 |
- Name of the downtime trigger. |
| 94 |
type: str |
| 95 |
extends_documentation_fragment: |
| 96 |
- community.general._icinga2_api |
| 97 |
- community.general._attributes |
| 98 |
- ansible.builtin.url |
| 99 |
""" |
| 100 |
|
| 101 |
EXAMPLES = r""" |
| 102 |
- name: Schedule a host downtime |
| 103 |
community.general.icinga2_downtime: |
| 104 |
url: "https://icinga2.example.com:5665" |
| 105 |
url_username: icingadmin |
| 106 |
url_password: secret |
| 107 |
state: present |
| 108 |
author: Ansible |
| 109 |
comment: Scheduled downtime for test purposes. |
| 110 |
all_services: true |
| 111 |
start_time: "{{ downtime_start_time }}" |
| 112 |
end_time: "{{ downtime_end_time }}" |
| 113 |
duration: "{{ downtime_duration }}" |
| 114 |
fixed: true |
| 115 |
object_type: Host |
| 116 |
filter: host.name=="host.example.com" |
| 117 |
delegate_to: localhost |
| 118 |
register: icinga2_downtime_response |
| 119 |
vars: |
| 120 |
downtime_start_time: "{{ ansible_date_time['epoch'] | int }}" |
| 121 |
downtime_end_time: "{{ downtime_start_time | int + 3600 }}" |
| 122 |
downtime_duration: "{{ downtime_end_time | int - downtime_start_time | int }}" |
| 123 |
|
| 124 |
- name: Remove scheduled host downtime |
| 125 |
community.general.icinga2_downtime: |
| 126 |
url: "https://icinga2.example.com:5665" |
| 127 |
url_username: icingadmin |
| 128 |
url_password: secret |
| 129 |
state: absent |
| 130 |
author: Ansible |
| 131 |
object_type: Downtime |
| 132 |
name: "{{ icinga2_downtime_response.results[0].name }}" |
| 133 |
delegate_to: localhost |
| 134 |
when: icinga2_downtime_response.results | default([]) | length > 0 |
| 135 |
""" |
| 136 |
|
| 137 |
RETURN = r""" |
| 138 |
# Returns the results of downtime scheduling as a list of JSON dictionaries from the Icinga 2 API under the C(results) key. |
| 139 |
# Refer to https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#schedule-downtime for more details. |
| 140 |
results: |
| 141 |
description: Results of downtime scheduling or removal |
| 142 |
type: list |
| 143 |
returned: success |
| 144 |
elements: dict |
| 145 |
contains: |
| 146 |
code: |
| 147 |
description: Success or error code of downtime scheduling. |
| 148 |
returned: always |
| 149 |
type: int |
| 150 |
sample: 200 |
| 151 |
legacy_id: |
| 152 |
description: Legacy id of the downtime object. |
| 153 |
returned: if a downtime was scheduled successfully |
| 154 |
type: int |
| 155 |
sample: 28911 |
| 156 |
name: |
| 157 |
description: Name of the downtime object. |
| 158 |
returned: if a downtime was scheduled successfully |
| 159 |
type: str |
| 160 |
sample: host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51 |
| 161 |
status: |
| 162 |
description: Human-readable message describing the result of downtime scheduling. |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
sample: Successfully scheduled downtime 'host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51' for object 'host.example.com'. |
| 166 |
sample: |
| 167 |
[ |
| 168 |
{ |
| 169 |
"code": 200, |
| 170 |
"legacy_id": 28911, |
| 171 |
"name": "host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51", |
| 172 |
"status": "Successfully scheduled downtime 'host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51' for object 'host.example.com'.", |
| 173 |
} |
| 174 |
] |
| 175 |
error: |
| 176 |
description: Error message as JSON dictionary returned from the Icinga 2 API. |
| 177 |
type: dict |
| 178 |
returned: if downtime scheduling or removal did not succeed |
| 179 |
sample: |
| 180 |
{ |
| 181 |
"error": 404, |
| 182 |
"status": "No objects found." |
| 183 |
} |
| 184 |
""" |
| 185 |
|
| 186 |
import json |
| 187 |
from contextlib import suppress |
| 188 |
|
| 189 |
from ansible_collections.community.general.plugins.module_utils._icinga2 import ( |
| 190 |
Icinga2Client, |
| 191 |
icinga2_argument_spec, |
| 192 |
) |
| 193 |
from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper |
| 194 |
|
| 195 |
|
| 196 |
class Icinga2Downtime(StateModuleHelper): |
| 197 |
argument_spec = icinga2_argument_spec() |
| 198 |
argument_spec.update( |
| 199 |
all_services=dict(type="bool"), |
| 200 |
author=dict(type="str", default="Ansible"), |
| 201 |
comment=dict(type="str", default="Downtime scheduled by Ansible"), |
| 202 |
child_options=dict( |
| 203 |
type="str", |
| 204 |
choices=[ |
| 205 |
"DowntimeNoChildren", |
| 206 |
"DowntimeTriggeredChildren", |
| 207 |
"DowntimeNonTriggeredChildren", |
| 208 |
], |
| 209 |
), |
| 210 |
duration=dict(type="int"), |
| 211 |
end_time=dict(type="int"), |
| 212 |
filter_vars=dict(type="dict"), |
| 213 |
filter=dict(type="str"), |
| 214 |
fixed=dict(type="bool"), |
| 215 |
name=dict(type="str"), |
| 216 |
object_type=dict(type="str", choices=["Service", "Host", "Downtime"], default="Host"), |
| 217 |
start_time=dict(type="int"), |
| 218 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 219 |
trigger_name=dict(type="str"), |
| 220 |
) |
| 221 |
module = dict( |
| 222 |
argument_spec=argument_spec, |
| 223 |
supports_check_mode=False, |
| 224 |
required_if=( |
| 225 |
( |
| 226 |
"state", |
| 227 |
"present", |
| 228 |
["comment", "start_time", "end_time", "filter"], |
| 229 |
), |
| 230 |
("fixed", False, ["duration"]), |
| 231 |
), |
| 232 |
required_one_of=[["filter", "name"]], |
| 233 |
) |
| 234 |
|
| 235 |
def __init_module__(self) -> None: |
| 236 |
self.client = Icinga2Client( |
| 237 |
module=self.module, # type:ignore[arg-type] |
| 238 |
url=self.vars.url, |
| 239 |
ca_path=self.vars.ca_path, |
| 240 |
timeout=self.vars.timeout, |
| 241 |
) |
| 242 |
|
| 243 |
def state_present(self) -> None: |
| 244 |
duration = self.vars.duration |
| 245 |
end_time = self.vars.end_time |
| 246 |
start_time = self.vars.start_time |
| 247 |
|
| 248 |
if end_time <= start_time: |
| 249 |
self.do_raise(msg="The end time must be later than the start time.") |
| 250 |
|
| 251 |
if duration is None: |
| 252 |
duration = end_time - start_time |
| 253 |
|
| 254 |
response, info = self.client.actions.schedule_downtime( |
| 255 |
all_services=self.vars.all_services, |
| 256 |
author=self.vars.author, |
| 257 |
child_options=self.vars.child_options, |
| 258 |
comment=self.vars.comment, |
| 259 |
duration=duration, |
| 260 |
end_time=end_time, |
| 261 |
filter_vars=self.vars.filter_vars, |
| 262 |
filter=self.vars.filter, |
| 263 |
fixed=self.vars.fixed, |
| 264 |
object_type=self.vars.object_type, |
| 265 |
start_time=start_time, |
| 266 |
trigger_name=self.vars.trigger_name, |
| 267 |
) |
| 268 |
|
| 269 |
status_code = info["status"] |
| 270 |
|
| 271 |
if 200 <= status_code <= 299: |
| 272 |
self.vars.set("results", json.loads(response.read())["results"], output=True) |
| 273 |
self.vars.msg = "Successfully scheduled downtime." |
| 274 |
self.changed = True |
| 275 |
elif status_code >= 400: |
| 276 |
with suppress(KeyError, ValueError): |
| 277 |
self.vars.set("error", json.loads(info["body"])) # type:ignore[arg-type] |
| 278 |
|
| 279 |
self.do_raise(msg="Unable to schedule downtime.") |
| 280 |
|
| 281 |
def state_absent(self) -> None: |
| 282 |
response, info = self.client.actions.remove_downtime( |
| 283 |
filter_vars=self.vars.filter_vars, |
| 284 |
filter=self.vars.filter, |
| 285 |
name=self.vars.name, |
| 286 |
object_type=self.vars.object_type, |
| 287 |
) |
| 288 |
|
| 289 |
status_code = info["status"] |
| 290 |
|
| 291 |
if 200 <= status_code <= 299: |
| 292 |
self.vars.set("results", json.loads(response.read())["results"], output=True) |
| 293 |
self.vars.msg = "Successfully removed downtime." |
| 294 |
self.changed = True |
| 295 |
elif status_code == 404: |
| 296 |
self.vars.msg = "No matching downtime object found." |
| 297 |
elif status_code >= 400: |
| 298 |
with suppress(KeyError, ValueError): |
| 299 |
self.vars.set("error", json.loads(info["body"])) # type:ignore[arg-type] |
| 300 |
|
| 301 |
self.do_raise(msg="Unable to remove downtime.") |
| 302 |
|
| 303 |
|
| 304 |
def main(): |
| 305 |
Icinga2Downtime.execute() |
| 306 |
|
| 307 |
|
| 308 |
if __name__ == "__main__": |
| 309 |
main() |