| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2017, sookido |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: zabbix_template |
| 14 |
short_description: Create/update/delete Zabbix template |
| 15 |
description: |
| 16 |
- This module allows you to create, modify and delete Zabbix templates. |
| 17 |
- Multiple templates can be created or modified at once if passing JSON or XML to module. |
| 18 |
author: |
| 19 |
- "sookido (@sookido)" |
| 20 |
- "Logan Vig (@logan2211)" |
| 21 |
- "Dusan Matejka (@D3DeFi)" |
| 22 |
requirements: |
| 23 |
- "python >= 3.9" |
| 24 |
options: |
| 25 |
template_name: |
| 26 |
description: |
| 27 |
- Name of Zabbix template. |
| 28 |
- Required when I(template_json) or I(template_xml) are not used. |
| 29 |
- Mutually exclusive with I(template_json) and I(template_xml). |
| 30 |
required: false |
| 31 |
type: str |
| 32 |
template_json: |
| 33 |
description: |
| 34 |
- JSON dump of templates to import. |
| 35 |
- Multiple templates can be imported this way. |
| 36 |
- Mutually exclusive with I(template_name) and I(template_xml) and I(template_yaml). |
| 37 |
required: false |
| 38 |
type: json |
| 39 |
template_xml: |
| 40 |
description: |
| 41 |
- XML dump of templates to import. |
| 42 |
- Multiple templates can be imported this way. |
| 43 |
- Mutually exclusive with I(template_name) and I(template_json) and I(template_yaml). |
| 44 |
required: false |
| 45 |
type: str |
| 46 |
template_yaml: |
| 47 |
description: |
| 48 |
- Context of exported templates file to import. |
| 49 |
- Multiple templates can be imported this way. |
| 50 |
- Mutually exclusive with I(template_name) and I(template_json) and I(template_xml). |
| 51 |
required: false |
| 52 |
type: str |
| 53 |
template_groups: |
| 54 |
description: |
| 55 |
- List of template groups to add template to when template is created. |
| 56 |
- Replaces the current template groups the template belongs to if the template is already present. |
| 57 |
- Required when creating a new template with C(state=present) and I(template_name) is used. |
| 58 |
Not required when updating an existing template. |
| 59 |
required: false |
| 60 |
type: list |
| 61 |
elements: str |
| 62 |
link_templates: |
| 63 |
description: |
| 64 |
- List of template names to be linked to the template. |
| 65 |
- Templates that are not specified and are linked to the existing template will be only unlinked and not |
| 66 |
cleared from the template. |
| 67 |
required: false |
| 68 |
type: list |
| 69 |
elements: str |
| 70 |
clear_templates: |
| 71 |
description: |
| 72 |
- List of template names to be unlinked and cleared from the template. |
| 73 |
- This option is ignored if template is being created for the first time. |
| 74 |
required: false |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
macros: |
| 78 |
description: |
| 79 |
- List of user macros to create for the template. |
| 80 |
- Macros that are not specified and are present on the existing template will be replaced. |
| 81 |
- See examples on how to pass macros. |
| 82 |
required: false |
| 83 |
type: list |
| 84 |
elements: dict |
| 85 |
suboptions: |
| 86 |
macro: |
| 87 |
description: |
| 88 |
- Name of the macro. |
| 89 |
- Must be specified in {$NAME} format. |
| 90 |
type: str |
| 91 |
required: true |
| 92 |
value: |
| 93 |
description: |
| 94 |
- Value of the macro. |
| 95 |
type: str |
| 96 |
required: true |
| 97 |
tags: |
| 98 |
description: |
| 99 |
- List of tags to assign to the template. |
| 100 |
- Providing I(tags=[]) with I(force=yes) will clean all of the tags from the template. |
| 101 |
required: false |
| 102 |
type: list |
| 103 |
elements: dict |
| 104 |
suboptions: |
| 105 |
tag: |
| 106 |
description: |
| 107 |
- Name of the template tag. |
| 108 |
type: str |
| 109 |
required: true |
| 110 |
value: |
| 111 |
description: |
| 112 |
- Value of the template tag. |
| 113 |
type: str |
| 114 |
default: "" |
| 115 |
state: |
| 116 |
description: |
| 117 |
- Required state of the template. |
| 118 |
- On C(state=present) template will be created/imported or updated depending if it is already present. |
| 119 |
- On C(state=absent) template will be deleted. |
| 120 |
required: false |
| 121 |
choices: [present, absent] |
| 122 |
default: "present" |
| 123 |
type: str |
| 124 |
|
| 125 |
extends_documentation_fragment: |
| 126 |
- community.zabbix.zabbix |
| 127 |
""" |
| 128 |
|
| 129 |
|
| 130 |
EXAMPLES = r""" |
| 131 |
--- |
| 132 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 133 |
- name: Set credentials to access Zabbix Server API |
| 134 |
ansible.builtin.set_fact: |
| 135 |
ansible_user: Admin |
| 136 |
ansible_httpapi_pass: zabbix |
| 137 |
|
| 138 |
# If you want to use API token to be authenticated by Zabbix Server |
| 139 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 140 |
- name: Set API token |
| 141 |
ansible.builtin.set_fact: |
| 142 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 143 |
|
| 144 |
- name: Create a new Zabbix template linked to groups, macros and templates |
| 145 |
# set task level variables as we change ansible_connection plugin here |
| 146 |
vars: |
| 147 |
ansible_network_os: community.zabbix.zabbix |
| 148 |
ansible_connection: httpapi |
| 149 |
ansible_httpapi_port: 443 |
| 150 |
ansible_httpapi_use_ssl: true |
| 151 |
ansible_httpapi_validate_certs: false |
| 152 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 153 |
ansible_host: zabbix-example-fqdn.org |
| 154 |
community.zabbix.zabbix_template: |
| 155 |
template_name: ExampleHost |
| 156 |
template_groups: |
| 157 |
- Role |
| 158 |
- Role2 |
| 159 |
link_templates: |
| 160 |
- Example template1 |
| 161 |
- Example template2 |
| 162 |
macros: |
| 163 |
- macro: "{$EXAMPLE_MACRO1}" |
| 164 |
value: 30000 |
| 165 |
- macro: "{$EXAMPLE_MACRO2}" |
| 166 |
value: 3 |
| 167 |
- macro: "{$EXAMPLE_MACRO3}" |
| 168 |
value: "Example" |
| 169 |
state: present |
| 170 |
|
| 171 |
- name: Unlink and clear templates from the existing Zabbix template |
| 172 |
# set task level variables as we change ansible_connection plugin here |
| 173 |
vars: |
| 174 |
ansible_network_os: community.zabbix.zabbix |
| 175 |
ansible_connection: httpapi |
| 176 |
ansible_httpapi_port: 443 |
| 177 |
ansible_httpapi_use_ssl: true |
| 178 |
ansible_httpapi_validate_certs: false |
| 179 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 180 |
ansible_host: zabbix-example-fqdn.org |
| 181 |
community.zabbix.zabbix_template: |
| 182 |
template_name: ExampleHost |
| 183 |
clear_templates: |
| 184 |
- Example template3 |
| 185 |
- Example template4 |
| 186 |
state: present |
| 187 |
|
| 188 |
- name: Import Zabbix templates from JSON |
| 189 |
# set task level variables as we change ansible_connection plugin here |
| 190 |
vars: |
| 191 |
ansible_network_os: community.zabbix.zabbix |
| 192 |
ansible_connection: httpapi |
| 193 |
ansible_httpapi_port: 443 |
| 194 |
ansible_httpapi_use_ssl: true |
| 195 |
ansible_httpapi_validate_certs: false |
| 196 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 197 |
ansible_host: zabbix-example-fqdn.org |
| 198 |
community.zabbix.zabbix_template: |
| 199 |
template_json: "{{ lookup('file', 'zabbix_apache2.json') }}" |
| 200 |
state: present |
| 201 |
|
| 202 |
- name: Import Zabbix templates from XML |
| 203 |
# set task level variables as we change ansible_connection plugin here |
| 204 |
vars: |
| 205 |
ansible_network_os: community.zabbix.zabbix |
| 206 |
ansible_connection: httpapi |
| 207 |
ansible_httpapi_port: 443 |
| 208 |
ansible_httpapi_use_ssl: true |
| 209 |
ansible_httpapi_validate_certs: false |
| 210 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 211 |
ansible_host: zabbix-example-fqdn.org |
| 212 |
community.zabbix.zabbix_template: |
| 213 |
template_xml: "{{ lookup('file', 'zabbix_apache2.xml') }}" |
| 214 |
state: present |
| 215 |
|
| 216 |
- name: Import Zabbix template from Ansible dict variable |
| 217 |
# set task level variables as we change ansible_connection plugin here |
| 218 |
vars: |
| 219 |
ansible_network_os: community.zabbix.zabbix |
| 220 |
ansible_connection: httpapi |
| 221 |
ansible_httpapi_port: 443 |
| 222 |
ansible_httpapi_use_ssl: true |
| 223 |
ansible_httpapi_validate_certs: false |
| 224 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 225 |
ansible_host: zabbix-example-fqdn.org |
| 226 |
community.zabbix.zabbix_template: |
| 227 |
template_json: |
| 228 |
zabbix_export: |
| 229 |
version: "3.2" |
| 230 |
templates: |
| 231 |
- name: Template for Testing |
| 232 |
description: "Testing template import" |
| 233 |
template: Test Template |
| 234 |
groups: |
| 235 |
- name: Templates |
| 236 |
state: present |
| 237 |
|
| 238 |
- name: Configure macros on the existing Zabbix template |
| 239 |
# set task level variables as we change ansible_connection plugin here |
| 240 |
vars: |
| 241 |
ansible_network_os: community.zabbix.zabbix |
| 242 |
ansible_connection: httpapi |
| 243 |
ansible_httpapi_port: 443 |
| 244 |
ansible_httpapi_use_ssl: true |
| 245 |
ansible_httpapi_validate_certs: false |
| 246 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 247 |
ansible_host: zabbix-example-fqdn.org |
| 248 |
community.zabbix.zabbix_template: |
| 249 |
template_name: Template |
| 250 |
macros: |
| 251 |
- macro: "{$TEST_MACRO}" |
| 252 |
value: "Example" |
| 253 |
state: present |
| 254 |
|
| 255 |
- name: Add tags to the existing Zabbix template |
| 256 |
# set task level variables as we change ansible_connection plugin here |
| 257 |
vars: |
| 258 |
ansible_network_os: community.zabbix.zabbix |
| 259 |
ansible_connection: httpapi |
| 260 |
ansible_httpapi_port: 443 |
| 261 |
ansible_httpapi_use_ssl: true |
| 262 |
ansible_httpapi_validate_certs: false |
| 263 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 264 |
ansible_host: zabbix-example-fqdn.org |
| 265 |
community.zabbix.zabbix_template: |
| 266 |
template_name: Template |
| 267 |
tags: |
| 268 |
- tag: class |
| 269 |
value: application |
| 270 |
state: present |
| 271 |
|
| 272 |
- name: Delete Zabbix template |
| 273 |
# set task level variables as we change ansible_connection plugin here |
| 274 |
vars: |
| 275 |
ansible_network_os: community.zabbix.zabbix |
| 276 |
ansible_connection: httpapi |
| 277 |
ansible_httpapi_port: 443 |
| 278 |
ansible_httpapi_use_ssl: true |
| 279 |
ansible_httpapi_validate_certs: false |
| 280 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 281 |
ansible_host: zabbix-example-fqdn.org |
| 282 |
community.zabbix.zabbix_template: |
| 283 |
template_name: Template |
| 284 |
state: absent |
| 285 |
""" |
| 286 |
|
| 287 |
RETURN = r""" |
| 288 |
--- |
| 289 |
""" |
| 290 |
|
| 291 |
|
| 292 |
import json |
| 293 |
import traceback |
| 294 |
|
| 295 |
from ansible.module_utils.basic import AnsibleModule |
| 296 |
from ansible.module_utils._text import to_native |
| 297 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 298 |
from ansible.module_utils.compat.version import LooseVersion |
| 299 |
|
| 300 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 301 |
|
| 302 |
|
| 303 |
class Template(ZabbixBase): |
| 304 |
# get group ids by group names |
| 305 |
def get_group_ids_by_group_names(self, group_names): |
| 306 |
group_ids = [] |
| 307 |
for group_name in group_names: |
| 308 |
if LooseVersion(self._zbx_api_version) >= LooseVersion("7.0"): |
| 309 |
group = self._zapi.templategroup.get({"output": ["groupid"], "filter": {"name": group_name}}) |
| 310 |
else: |
| 311 |
group = self._zapi.hostgroup.get({"output": ["groupid"], "filter": {"name": group_name}}) |
| 312 |
if group: |
| 313 |
group_ids.append({"groupid": group[0]["groupid"]}) |
| 314 |
else: |
| 315 |
self._module.fail_json(msg="Template group not found: %s" % group_name) |
| 316 |
return group_ids |
| 317 |
|
| 318 |
def get_template_ids(self, template_list): |
| 319 |
template_ids = [] |
| 320 |
if template_list is None or len(template_list) == 0: |
| 321 |
return template_ids |
| 322 |
for template in template_list: |
| 323 |
template_list = self._zapi.template.get({"output": "extend", "filter": {"host": template}}) |
| 324 |
if len(template_list) < 1: |
| 325 |
continue |
| 326 |
else: |
| 327 |
template_id = template_list[0]["templateid"] |
| 328 |
template_ids.append({"templateid": template_id}) |
| 329 |
return template_ids |
| 330 |
|
| 331 |
def add_template(self, template_name, group_ids, link_template_ids, macros, tags): |
| 332 |
if self._module.check_mode: |
| 333 |
self._module.exit_json(changed=True) |
| 334 |
|
| 335 |
new_template = {"host": template_name, "groups": group_ids, "templates": link_template_ids, "macros": macros, "tags": tags} |
| 336 |
if macros is None: |
| 337 |
new_template.update({"macros": []}) |
| 338 |
if tags is None: |
| 339 |
new_template.update({"tags": []}) |
| 340 |
if link_template_ids is None: |
| 341 |
new_template.update({"templates": []}) |
| 342 |
|
| 343 |
self._zapi.template.create(new_template) |
| 344 |
|
| 345 |
def import_compare(self, template_content, template_type): |
| 346 |
"""template_content has same structure as Zabbix uses (e.g. it was optimally exported via Zabbix GUI or API)""" |
| 347 |
|
| 348 |
changed = False |
| 349 |
if template_content is not None: |
| 350 |
update_rules = { |
| 351 |
"discoveryRules": { |
| 352 |
"createMissing": True, |
| 353 |
"updateExisting": True, |
| 354 |
"deleteMissing": True |
| 355 |
}, |
| 356 |
"graphs": { |
| 357 |
"createMissing": True, |
| 358 |
"updateExisting": True, |
| 359 |
"deleteMissing": True |
| 360 |
}, |
| 361 |
"host_groups": { |
| 362 |
"createMissing": True |
| 363 |
}, |
| 364 |
"httptests": { |
| 365 |
"createMissing": True, |
| 366 |
"updateExisting": True, |
| 367 |
"deleteMissing": True |
| 368 |
}, |
| 369 |
"items": { |
| 370 |
"createMissing": True, |
| 371 |
"updateExisting": True, |
| 372 |
"deleteMissing": True |
| 373 |
}, |
| 374 |
"templates": { |
| 375 |
"createMissing": True, |
| 376 |
"updateExisting": True |
| 377 |
}, |
| 378 |
"template_groups": { |
| 379 |
"createMissing": True |
| 380 |
}, |
| 381 |
"templateLinkage": { |
| 382 |
"createMissing": True |
| 383 |
}, |
| 384 |
"templateScreens": { |
| 385 |
"createMissing": True, |
| 386 |
"updateExisting": True, |
| 387 |
"deleteMissing": True |
| 388 |
}, |
| 389 |
"triggers": { |
| 390 |
"createMissing": True, |
| 391 |
"updateExisting": True, |
| 392 |
"deleteMissing": True |
| 393 |
}, |
| 394 |
"valueMaps": { |
| 395 |
"createMissing": True, |
| 396 |
"updateExisting": True |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
try: |
| 401 |
update_rules["templateLinkage"]["deleteMissing"] = True |
| 402 |
update_rules["templateDashboards"] = update_rules.pop("templateScreens") |
| 403 |
|
| 404 |
# before Zabbix 6.2 host_groups and template_group are joined into groups parameter |
| 405 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 406 |
update_rules["groups"] = {"createMissing": True} |
| 407 |
update_rules.pop("host_groups", None) |
| 408 |
update_rules.pop("template_groups", None) |
| 409 |
importcompare = {"format": template_type, "source": template_content, "rules": update_rules} |
| 410 |
compare_result = self._zapi.configuration.importcompare(importcompare) |
| 411 |
if len(compare_result) != 0: |
| 412 |
changed = True |
| 413 |
return changed |
| 414 |
except Exception as e: |
| 415 |
self._module.fail_json(msg="Unable to compare template", details=to_native(e), |
| 416 |
exception=traceback.format_exc()) |
| 417 |
|
| 418 |
def check_template_changed(self, template_ids, template_groups, link_templates, clear_templates, |
| 419 |
template_macros, template_tags): |
| 420 |
"""Compare template with user provided all parameters via module options""" |
| 421 |
changed = False |
| 422 |
existing_template = self.dump_template(template_ids, template_type="json") |
| 423 |
if template_groups is not None: |
| 424 |
if LooseVersion(self._zbx_api_version) >= LooseVersion("7.0"): |
| 425 |
existing_groups = [g["name"] for g in existing_template["zabbix_export"]["template_groups"]] |
| 426 |
else: |
| 427 |
existing_groups = [g["name"] for g in existing_template["zabbix_export"]["groups"]] |
| 428 |
|
| 429 |
if set(template_groups) != set(existing_groups): |
| 430 |
changed = True |
| 431 |
|
| 432 |
if "templates" not in existing_template["zabbix_export"]["templates"][0]: |
| 433 |
existing_template["zabbix_export"]["templates"][0]["templates"] = [] |
| 434 |
|
| 435 |
# Check if any new templates would be linked or any existing would be unlinked |
| 436 |
exist_child_templates = [t["name"] for t in existing_template["zabbix_export"]["templates"][0]["templates"]] |
| 437 |
if link_templates is not None: |
| 438 |
if set(link_templates) != set(exist_child_templates): |
| 439 |
changed = True |
| 440 |
else: |
| 441 |
if set([]) != set(exist_child_templates): |
| 442 |
changed = True |
| 443 |
|
| 444 |
# Mark that there will be changes when at least one existing template will be unlinked |
| 445 |
if clear_templates is not None: |
| 446 |
for t in clear_templates: |
| 447 |
if t in exist_child_templates: |
| 448 |
changed = True |
| 449 |
break |
| 450 |
|
| 451 |
if "macros" not in existing_template["zabbix_export"]["templates"][0]: |
| 452 |
existing_template["zabbix_export"]["templates"][0]["macros"] = [] |
| 453 |
|
| 454 |
if template_macros is not None: |
| 455 |
existing_macros = existing_template["zabbix_export"]["templates"][0]["macros"] |
| 456 |
for item in existing_macros: |
| 457 |
if "value" not in item: |
| 458 |
item["value"] = "" |
| 459 |
if template_macros != existing_macros: |
| 460 |
changed = True |
| 461 |
|
| 462 |
if "tags" not in existing_template["zabbix_export"]["templates"][0]: |
| 463 |
existing_template["zabbix_export"]["templates"][0]["tags"] = [] |
| 464 |
if template_tags is not None: |
| 465 |
existing_tags = existing_template["zabbix_export"]["templates"][0]["tags"] |
| 466 |
for item in existing_tags: |
| 467 |
if "value" not in item: |
| 468 |
item["value"] = "" |
| 469 |
if template_tags != existing_tags: |
| 470 |
changed = True |
| 471 |
|
| 472 |
return changed |
| 473 |
|
| 474 |
def update_template(self, template_ids, group_ids, link_template_ids, clear_template_ids, template_macros, template_tags): |
| 475 |
template_changes = {} |
| 476 |
if group_ids is not None: |
| 477 |
template_changes.update({"groups": group_ids}) |
| 478 |
|
| 479 |
if link_template_ids is not None: |
| 480 |
template_changes.update({"templates": link_template_ids}) |
| 481 |
else: |
| 482 |
template_changes.update({"templates": []}) |
| 483 |
|
| 484 |
if clear_template_ids is not None: |
| 485 |
template_changes.update({"templates_clear": clear_template_ids}) |
| 486 |
|
| 487 |
if template_macros is not None: |
| 488 |
template_changes.update({"macros": template_macros}) |
| 489 |
else: |
| 490 |
template_changes.update({"macros": []}) |
| 491 |
|
| 492 |
if template_tags is not None: |
| 493 |
template_changes.update({"tags": template_tags}) |
| 494 |
else: |
| 495 |
template_changes.update({"tags": []}) |
| 496 |
|
| 497 |
if template_changes: |
| 498 |
# If we got here we know that only one template was provided via template_name |
| 499 |
template_changes.update(template_ids[0]) |
| 500 |
self._zapi.template.update(template_changes) |
| 501 |
|
| 502 |
def delete_template(self, templateids): |
| 503 |
if self._module.check_mode: |
| 504 |
self._module.exit_json(changed=True) |
| 505 |
|
| 506 |
templateids_list = [t.get("templateid") for t in templateids] |
| 507 |
self._zapi.template.delete(templateids_list) |
| 508 |
|
| 509 |
def dump_template(self, template_ids, template_type="json"): |
| 510 |
template_ids_list = [t.get("templateid") for t in template_ids] |
| 511 |
try: |
| 512 |
dump = self._zapi.configuration.export({"format": template_type, "options": {"templates": template_ids_list}}) |
| 513 |
return self.load_json_template(dump) |
| 514 |
|
| 515 |
except Exception as e: |
| 516 |
self._module.fail_json(msg="Unable to export template: %s" % e) |
| 517 |
|
| 518 |
def load_json_template(self, template_json): |
| 519 |
try: |
| 520 |
jsondoc = json.loads(template_json) |
| 521 |
return jsondoc |
| 522 |
except ValueError as e: |
| 523 |
self._module.fail_json(msg="Invalid JSON provided", details=to_native(e), exception=traceback.format_exc()) |
| 524 |
|
| 525 |
def import_template(self, template_content, template_type="json"): |
| 526 |
if self._module.check_mode: |
| 527 |
self._module.exit_json(changed=True) |
| 528 |
|
| 529 |
# rules schema latest version |
| 530 |
update_rules = { |
| 531 |
"discoveryRules": { |
| 532 |
"createMissing": True, |
| 533 |
"updateExisting": True, |
| 534 |
"deleteMissing": True |
| 535 |
}, |
| 536 |
"graphs": { |
| 537 |
"createMissing": True, |
| 538 |
"updateExisting": True, |
| 539 |
"deleteMissing": True |
| 540 |
}, |
| 541 |
"host_groups": { |
| 542 |
"createMissing": True |
| 543 |
}, |
| 544 |
"httptests": { |
| 545 |
"createMissing": True, |
| 546 |
"updateExisting": True, |
| 547 |
"deleteMissing": True |
| 548 |
}, |
| 549 |
"items": { |
| 550 |
"createMissing": True, |
| 551 |
"updateExisting": True, |
| 552 |
"deleteMissing": True |
| 553 |
}, |
| 554 |
"templates": { |
| 555 |
"createMissing": True, |
| 556 |
"updateExisting": True |
| 557 |
}, |
| 558 |
"template_groups": { |
| 559 |
"createMissing": True |
| 560 |
}, |
| 561 |
"templateLinkage": { |
| 562 |
"createMissing": True |
| 563 |
}, |
| 564 |
"templateScreens": { |
| 565 |
"createMissing": True, |
| 566 |
"updateExisting": True, |
| 567 |
"deleteMissing": True |
| 568 |
}, |
| 569 |
"triggers": { |
| 570 |
"createMissing": True, |
| 571 |
"updateExisting": True, |
| 572 |
"deleteMissing": True |
| 573 |
}, |
| 574 |
"valueMaps": { |
| 575 |
"createMissing": True, |
| 576 |
"updateExisting": True |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
try: |
| 581 |
update_rules["templateLinkage"]["deleteMissing"] = True |
| 582 |
update_rules["templateDashboards"] = update_rules.pop("templateScreens") |
| 583 |
|
| 584 |
# before Zabbix 6.2 host_groups and template_group are joined into groups parameter |
| 585 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 586 |
update_rules["groups"] = {"createMissing": True} |
| 587 |
update_rules.pop("host_groups", None) |
| 588 |
update_rules.pop("template_groups", None) |
| 589 |
|
| 590 |
import_data = {"format": template_type, "source": template_content, "rules": update_rules} |
| 591 |
self._zapi.configuration.import_(import_data) |
| 592 |
except Exception as e: |
| 593 |
self._module.fail_json(msg="Unable to import template", details=to_native(e), |
| 594 |
exception=traceback.format_exc()) |
| 595 |
|
| 596 |
|
| 597 |
def main(): |
| 598 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 599 |
argument_spec.update(dict( |
| 600 |
template_name=dict(type="str", required=False), |
| 601 |
template_json=dict(type="json", required=False), |
| 602 |
template_xml=dict(type="str", required=False), |
| 603 |
template_yaml=dict(type="str", required=False), |
| 604 |
template_groups=dict(type="list", required=False, elements="str"), |
| 605 |
link_templates=dict(type="list", required=False, elements="str"), |
| 606 |
clear_templates=dict(type="list", required=False, elements="str"), |
| 607 |
macros=dict( |
| 608 |
type="list", |
| 609 |
elements="dict", |
| 610 |
options=dict( |
| 611 |
macro=dict(type="str", required=True), |
| 612 |
value=dict(type="str", required=True) |
| 613 |
) |
| 614 |
), |
| 615 |
tags=dict( |
| 616 |
type="list", |
| 617 |
elements="dict", |
| 618 |
options=dict( |
| 619 |
tag=dict(type="str", required=True), |
| 620 |
value=dict(type="str", default="") |
| 621 |
) |
| 622 |
), |
| 623 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 624 |
)) |
| 625 |
module = AnsibleModule( |
| 626 |
argument_spec=argument_spec, |
| 627 |
required_one_of=[ |
| 628 |
["template_name", "template_json", "template_xml", "template_yaml"] |
| 629 |
], |
| 630 |
mutually_exclusive=[ |
| 631 |
["template_name", "template_json", "template_xml", "template_yaml"] |
| 632 |
], |
| 633 |
required_if=[ |
| 634 |
["state", "absent", ["template_name"]] |
| 635 |
], |
| 636 |
supports_check_mode=True |
| 637 |
) |
| 638 |
|
| 639 |
template_name = module.params["template_name"] |
| 640 |
template_json = module.params["template_json"] |
| 641 |
template_xml = module.params["template_xml"] |
| 642 |
template_yaml = module.params["template_yaml"] |
| 643 |
template_groups = module.params["template_groups"] |
| 644 |
link_templates = module.params["link_templates"] |
| 645 |
clear_templates = module.params["clear_templates"] |
| 646 |
template_macros = module.params["macros"] |
| 647 |
template_tags = module.params["tags"] |
| 648 |
state = module.params["state"] |
| 649 |
|
| 650 |
template = Template(module) |
| 651 |
|
| 652 |
# Identify template names for IDs retrieval |
| 653 |
# Template names are expected to reside in ["zabbix_export"]["templates"][*]["template"] for both data types |
| 654 |
template_content, template_type, template_ids = None, None, None |
| 655 |
|
| 656 |
if template_json is not None: |
| 657 |
template_type = "json" |
| 658 |
template_content = template_json |
| 659 |
|
| 660 |
elif template_xml is not None: |
| 661 |
template_type = "xml" |
| 662 |
template_content = template_xml |
| 663 |
|
| 664 |
elif template_yaml is not None: |
| 665 |
template_type = "yaml" |
| 666 |
template_content = template_yaml |
| 667 |
|
| 668 |
else: |
| 669 |
template_names = [template_name] |
| 670 |
template_ids = template.get_template_ids(template_names) |
| 671 |
|
| 672 |
if state == "absent": |
| 673 |
if not template_ids: |
| 674 |
module.exit_json(changed=False, msg="Template not found. No changed: %s" % template_name) |
| 675 |
|
| 676 |
template.delete_template(template_ids) |
| 677 |
module.exit_json(changed=True, result="Successfully deleted template %s" % template_name) |
| 678 |
|
| 679 |
elif state == "present": |
| 680 |
if template_content is not None: |
| 681 |
changed = template.import_compare(template_content, template_type) |
| 682 |
if not changed: |
| 683 |
module.exit_json(changed=changed, result="Template is up-to date") |
| 684 |
else: |
| 685 |
if module.check_mode: |
| 686 |
module.exit_json(changed=changed) |
| 687 |
template.import_template(template_content, template_type) |
| 688 |
module.exit_json(changed=changed, result="Template import successful") |
| 689 |
else: |
| 690 |
# Load all subelements for template that were provided by user |
| 691 |
group_ids = None |
| 692 |
if template_groups is not None: |
| 693 |
group_ids = template.get_group_ids_by_group_names(template_groups) |
| 694 |
|
| 695 |
link_template_ids = None |
| 696 |
if link_templates is not None: |
| 697 |
link_template_ids = template.get_template_ids(link_templates) |
| 698 |
|
| 699 |
clear_template_ids = None |
| 700 |
if clear_templates is not None: |
| 701 |
clear_template_ids = template.get_template_ids(clear_templates) |
| 702 |
|
| 703 |
if template_macros is not None: |
| 704 |
# Zabbix configuration.export does not differentiate python types (numbers are returned as strings) |
| 705 |
for macroitem in template_macros: |
| 706 |
for key in macroitem: |
| 707 |
macroitem[key] = str(macroitem[key]) |
| 708 |
|
| 709 |
if template_tags is not None: |
| 710 |
for tagitem in template_tags: |
| 711 |
for key in tagitem: |
| 712 |
tagitem[key] = str(tagitem[key]) |
| 713 |
|
| 714 |
if not template_ids: |
| 715 |
# Assume new templates are being added when no ID"s were found |
| 716 |
if group_ids is None: |
| 717 |
module.fail_json(msg="template_groups are required when creating a new Zabbix template") |
| 718 |
|
| 719 |
template.add_template(template_name, group_ids, link_template_ids, template_macros, template_tags) |
| 720 |
module.exit_json(changed=True, result="Successfully added template: %s" % template_name) |
| 721 |
|
| 722 |
else: |
| 723 |
changed = template.check_template_changed(template_ids, template_groups, link_templates, clear_templates, |
| 724 |
template_macros, template_tags) |
| 725 |
|
| 726 |
if module.check_mode: |
| 727 |
module.exit_json(changed=changed) |
| 728 |
|
| 729 |
if changed: |
| 730 |
if template_type is not None: |
| 731 |
template.import_template(template_content, template_type) |
| 732 |
else: |
| 733 |
template.update_template(template_ids, group_ids, link_template_ids, clear_template_ids, |
| 734 |
template_macros, template_tags) |
| 735 |
|
| 736 |
module.exit_json(changed=changed, result="Template successfully updated") |
| 737 |
|
| 738 |
|
| 739 |
if __name__ == "__main__": |
| 740 |
main() |