Skip to content

Commit 0c97fb6

Browse files
authored
Add Support for URL Script Type (#1609)
1 parent 540c29e commit 0c97fb6

File tree

3 files changed

+178
-60
lines changed

3 files changed

+178
-60
lines changed

changelogs/fragments/1590.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- zabbix_script - Added support for type 'url'

plugins/modules/zabbix_script.py

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,22 @@
2727
script_type:
2828
description:
2929
- Script type. Required when state is 'present'.
30+
- A value of 'url' is only available in 7.0 or later
3031
type: str
3132
required: false
32-
choices: ["script", "ipmi", "ssh", "telnet", "webhook"]
33+
choices: ["script", "ipmi", "ssh", "telnet", "webhook", "url"]
34+
url:
35+
description:
36+
- The URL for quick access
37+
- Required if script_type is C(url)
38+
- Only available if script_type is C(url)
39+
type: str
40+
new_window:
41+
description:
42+
- Should URL be opened in a new window?
43+
- Only available if script_type is C(url)
44+
type: bool
45+
default: true
3346
command:
3447
description:
3548
- Command to run. Required when state is 'present'
@@ -203,6 +216,7 @@
203216

204217
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
205218
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
219+
from ansible.module_utils.compat.version import LooseVersion
206220

207221

208222
class Script(ZabbixBase):
@@ -214,21 +228,23 @@ def get_script_ids(self, script_name):
214228
return script_ids
215229

216230
def create_script(self, name, script_type, command, scope, execute_on, menu_path, authtype, username, password,
217-
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout, parameters, description):
231+
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout,
232+
parameters, description, url, new_window):
218233
if self._module.check_mode:
219234
self._module.exit_json(changed=True)
220235

221236
self._zapi.script.create(self.generate_script_config(name, script_type, command, scope, execute_on, menu_path,
222-
authtype, username, password, publickey, privatekey, port, host_group, user_group, host_access, confirmation,
223-
script_timeout, parameters, description))
237+
authtype, username, password, publickey, privatekey, port, host_group, user_group,
238+
host_access, confirmation, script_timeout, parameters, description, url, new_window))
224239

225240
def delete_script(self, script_ids):
226241
if self._module.check_mode:
227242
self._module.exit_json(changed=True)
228243
self._zapi.script.delete(script_ids)
229244

230245
def generate_script_config(self, name, script_type, command, scope, execute_on, menu_path, authtype, username, password,
231-
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout, parameters, description):
246+
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout,
247+
parameters, description, url, new_window):
232248
if host_group == "all":
233249
groupid = "0"
234250
else:
@@ -253,8 +269,8 @@ def generate_script_config(self, name, script_type, command, scope, execute_on,
253269
"ssh",
254270
"telnet",
255271
"",
256-
"webhook"], script_type)),
257-
"command": command,
272+
"webhook",
273+
"url"], script_type)),
258274
"scope": str(zabbix_utils.helper_to_numeric_value([
259275
"",
260276
"action_operation",
@@ -264,6 +280,9 @@ def generate_script_config(self, name, script_type, command, scope, execute_on,
264280
"groupid": groupid
265281
}
266282

283+
if command:
284+
request["command"] = command
285+
267286
if description is not None:
268287
request["description"] = description
269288

@@ -291,6 +310,13 @@ def generate_script_config(self, name, script_type, command, scope, execute_on,
291310
else:
292311
request["confirmation"] = confirmation
293312

313+
if script_type == "url":
314+
request["url"] = url
315+
if new_window:
316+
request["new_window"] = "1"
317+
else:
318+
request["new_window"] = "0"
319+
294320
if script_type == "ssh":
295321
request["authtype"] = str(zabbix_utils.helper_to_numeric_value([
296322
"password",
@@ -317,10 +343,11 @@ def generate_script_config(self, name, script_type, command, scope, execute_on,
317343
return request
318344

319345
def update_script(self, script_id, name, script_type, command, scope, execute_on, menu_path, authtype, username, password,
320-
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout, parameters, description):
346+
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout, parameters,
347+
description, url, new_window):
321348
generated_config = self.generate_script_config(name, script_type, command, scope, execute_on, menu_path, authtype, username,
322349
password, publickey, privatekey, port, host_group, user_group, host_access,
323-
confirmation, script_timeout, parameters, description)
350+
confirmation, script_timeout, parameters, description, url, new_window)
324351
live_config = self._zapi.script.get({"filter": {"name": name}})[0]
325352

326353
change_parameters = {}
@@ -342,8 +369,10 @@ def main():
342369
name=dict(type="str", required=True),
343370
script_type=dict(
344371
type="str",
345-
choices=["script", "ipmi", "ssh", "telnet", "webhook"]),
372+
choices=["script", "ipmi", "ssh", "telnet", "webhook", "url"]),
346373
command=dict(type="str"),
374+
url=dict(type="str"),
375+
new_window=dict(type="bool", default=True),
347376
scope=dict(
348377
type="str",
349378
choices=["action_operation", "manual_host_action", "manual_event_action"],
@@ -385,11 +414,15 @@ def main():
385414
))
386415

387416
required_if = [
388-
("state", "present", ("script_type", "command",)),
389-
("script_type", "ssh", ("authtype", "username",)),
417+
("state", "present", ("script_type",)),
418+
("script_type", "ssh", ("authtype", "username", "command",)),
419+
("script_type", "url", ("new_window", "url",)),
390420
("authtype", "password", ("password",)),
391421
("authtype", "public_key", ("publickey", "privatekey",)),
392-
("script_type", "telnet", ("username", "password")),
422+
("script_type", "telnet", ("username", "password", "command",)),
423+
("script_type", "script", ("command",)),
424+
("script_type", "ipmi", ("command",)),
425+
("script_type", "webhook", ("command",))
393426
]
394427

395428
module = AnsibleModule(
@@ -418,6 +451,8 @@ def main():
418451
parameters = module.params["parameters"]
419452
description = module.params["description"]
420453
state = module.params["state"]
454+
url = module.params["url"]
455+
new_window = module.params["new_window"]
421456

422457
script = Script(module)
423458
script_ids = script.get_script_ids(name)
@@ -430,14 +465,24 @@ def main():
430465
module.exit_json(changed=True, result="Successfully deleted script(s) %s" % name)
431466

432467
elif state == "present":
468+
if script_type == "url":
469+
if LooseVersion(script._zbx_api_version) < LooseVersion('7.0'):
470+
module.fail_json(changed=False, msg="A type of 'url' is only available for Zabbix >= 7.0")
471+
if scope not in ["manual_host_action", "manual_event_action"]:
472+
module.fail_json(changed=False, msg="A scope of '%s' is not valid for type of 'url'" % scope)
473+
else:
474+
if url:
475+
module.fail_json(changed=False, msg="A url can only be set for a type of 'url'")
476+
433477
if not script_ids:
434478
script.create_script(name, script_type, command, scope, execute_on, menu_path, authtype, username, password,
435-
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout, parameters, description)
479+
publickey, privatekey, port, host_group, user_group, host_access, confirmation, script_timeout,
480+
parameters, description, url, new_window)
436481
module.exit_json(changed=True, msg="Script %s created" % name)
437482
else:
438483
script.update_script(script_ids[0], name, script_type, command, scope, execute_on, menu_path, authtype, username,
439484
password, publickey, privatekey, port, host_group, user_group, host_access, confirmation,
440-
script_timeout, parameters, description)
485+
script_timeout, parameters, description, url, new_window)
441486

442487

443488
if __name__ == "__main__":

0 commit comments

Comments
 (0)