| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2013-2014, Epic Games, Inc. |
| 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_host |
| 14 |
short_description: Create/update/delete Zabbix hosts |
| 15 |
description: |
| 16 |
- This module allows you to create, modify and delete Zabbix host entries and associated group and template data. |
| 17 |
author: |
| 18 |
- "Cove (@cove)" |
| 19 |
- Tony Minfei Ding (!UNKNOWN) |
| 20 |
- Harrison Gu (@harrisongu) |
| 21 |
- Werner Dijkerman (@dj-wasabi) |
| 22 |
- Eike Frost (@eikef) |
| 23 |
requirements: |
| 24 |
- "python >= 3.9" |
| 25 |
options: |
| 26 |
host_name: |
| 27 |
description: |
| 28 |
- Name of the host in Zabbix. |
| 29 |
- I(host_name) is the unique identifier used and cannot be updated using this module. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
visible_name: |
| 33 |
description: |
| 34 |
- Visible name of the host in Zabbix. |
| 35 |
type: str |
| 36 |
description: |
| 37 |
description: |
| 38 |
- Description of the host in Zabbix. |
| 39 |
type: str |
| 40 |
host_groups: |
| 41 |
description: |
| 42 |
- List of host groups the host is part of. |
| 43 |
- Make sure the Zabbix user used for Ansible can read these groups. |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
link_templates: |
| 47 |
description: |
| 48 |
- List of templates linked to the host. |
| 49 |
type: list |
| 50 |
elements: str |
| 51 |
inventory_mode: |
| 52 |
description: |
| 53 |
- Configure the inventory mode. |
| 54 |
choices: ["automatic", "manual", "disabled"] |
| 55 |
type: str |
| 56 |
inventory_zabbix: |
| 57 |
description: |
| 58 |
- Add Facts for a zabbix inventory (e.g. Tag) (see example below). |
| 59 |
- Please review the interface documentation for more information on the supported properties |
| 60 |
- U(https://www.zabbix.com/documentation/current/en/manual/api/reference/host/object#host-inventory) |
| 61 |
type: dict |
| 62 |
status: |
| 63 |
description: |
| 64 |
- Monitoring status of the host. |
| 65 |
choices: ["enabled", "disabled"] |
| 66 |
default: "enabled" |
| 67 |
type: str |
| 68 |
state: |
| 69 |
description: |
| 70 |
- State of the host. |
| 71 |
- On C(present), it will create if host does not exist or update the host if the associated data is different. |
| 72 |
- On C(absent) will remove a host if it exists. |
| 73 |
choices: ["present", "absent"] |
| 74 |
default: "present" |
| 75 |
type: str |
| 76 |
monitored_by: |
| 77 |
description: |
| 78 |
- Parameter introduced in Zabbix 7.0. |
| 79 |
- Source that is used to monitor the host. |
| 80 |
choices: ["zabbix_server", "proxy", "proxy_group"] |
| 81 |
type: str |
| 82 |
proxy: |
| 83 |
description: |
| 84 |
- The name of the Zabbix proxy to be used. |
| 85 |
- Required if C(monitored_by) is "proxy" |
| 86 |
type: str |
| 87 |
proxy_group: |
| 88 |
description: |
| 89 |
- Parameter introduced in Zabbix 7.0. |
| 90 |
- Proxy group that is used to monitor the host. |
| 91 |
- Required if C(monitored_by) is "proxy_group" |
| 92 |
type: str |
| 93 |
interfaces: |
| 94 |
type: list |
| 95 |
elements: dict |
| 96 |
description: |
| 97 |
- List of interfaces to be created for the host (see example below). |
| 98 |
- For more information, review host interface documentation at |
| 99 |
- U(https://www.zabbix.com/documentation/current/en/manual/api/reference/hostinterface/object#host-interface) |
| 100 |
default: [] |
| 101 |
suboptions: |
| 102 |
type: |
| 103 |
type: str |
| 104 |
description: |
| 105 |
- Interface type to add |
| 106 |
- Numerical values are also accepted for interface type |
| 107 |
- 1 = agent |
| 108 |
- 2 = snmp |
| 109 |
- 3 = ipmi |
| 110 |
- 4 = jmx |
| 111 |
choices: ["agent", "1", "snmp", "2", "ipmi", "3", "jmx", "4"] |
| 112 |
required: true |
| 113 |
main: |
| 114 |
type: int |
| 115 |
description: |
| 116 |
- Whether the interface is used as default. |
| 117 |
- If multiple interfaces with the same type are provided, only one can be default. |
| 118 |
- 0 (not default), 1 (default) |
| 119 |
default: 0 |
| 120 |
choices: [0, 1] |
| 121 |
useip: |
| 122 |
type: int |
| 123 |
description: |
| 124 |
- Connect to host interface with IP address instead of DNS name. |
| 125 |
- 0 (don't use ip), 1 (use ip) |
| 126 |
default: 0 |
| 127 |
choices: [0, 1] |
| 128 |
ip: |
| 129 |
type: str |
| 130 |
description: |
| 131 |
- IP address used by host interface. |
| 132 |
- Required if I(useip=1). |
| 133 |
dns: |
| 134 |
type: str |
| 135 |
description: |
| 136 |
- DNS name of the host interface. |
| 137 |
- Required if I(useip=0). |
| 138 |
port: |
| 139 |
type: str |
| 140 |
description: |
| 141 |
- Port used by host interface. |
| 142 |
- If not specified, default port for each type of interface is used |
| 143 |
- 10050 if I(type="agent") |
| 144 |
- 161 if I(type="snmp") |
| 145 |
- 623 if I(type="ipmi") |
| 146 |
- 12345 if I(type="jmx") |
| 147 |
details: |
| 148 |
type: dict |
| 149 |
description: |
| 150 |
- Additional details for SNMP host interfaces. |
| 151 |
- Required when I(type="snmp"). |
| 152 |
default: {} |
| 153 |
suboptions: |
| 154 |
version: |
| 155 |
type: int |
| 156 |
description: |
| 157 |
- SNMP version. |
| 158 |
- 1 (SNMPv1), 2 (SNMPv2c), 3 (SNMPv3) |
| 159 |
choices: [1, 2, 3] |
| 160 |
default: 2 |
| 161 |
bulk: |
| 162 |
type: int |
| 163 |
description: |
| 164 |
- Whether to use bulk SNMP requests. |
| 165 |
- 0 (don't use bulk requests), 1 (use bulk requests) |
| 166 |
choices: [0, 1] |
| 167 |
default: 1 |
| 168 |
community: |
| 169 |
type: str |
| 170 |
description: |
| 171 |
- SNMPv1 and SNMPv2 community string. |
| 172 |
- Required when I(version=1) or I(version=2). |
| 173 |
default: "" |
| 174 |
securityname: |
| 175 |
type: str |
| 176 |
description: |
| 177 |
- SNMPv3 security name. |
| 178 |
default: "" |
| 179 |
contextname: |
| 180 |
type: str |
| 181 |
description: |
| 182 |
- SNMPv3 context name. |
| 183 |
default: "" |
| 184 |
securitylevel: |
| 185 |
type: int |
| 186 |
description: |
| 187 |
- SNMPv3 security level. |
| 188 |
- 0 (noAuthNoPriv), 1 (authNoPriv), 2 (authPriv). |
| 189 |
choices: [0, 1, 2] |
| 190 |
default: 0 |
| 191 |
authprotocol: |
| 192 |
type: int |
| 193 |
description: |
| 194 |
- SNMPv3 authentication protocol. |
| 195 |
- Used when I(securitylevel=1)(authNoPriv) or I(securitylevel=2)(AuthPriv). |
| 196 |
- Variants 2,3,4,5 are supported only on Zabbix 5.4 or greater |
| 197 |
- 0 (MD5), 1 (SHA1), 2 (SHA224), 3 (SHA256), 4 (SHA384), 5 (SHA512) |
| 198 |
default: 0 |
| 199 |
choices: [0, 1, 2, 3, 4, 5] |
| 200 |
authpassphrase: |
| 201 |
type: str |
| 202 |
description: |
| 203 |
- SNMPv3 authentication passphrase. |
| 204 |
- Used when I(securitylevel=1)(authNoPriv) or I(securitylevel=2)(AuthPriv). |
| 205 |
default: "" |
| 206 |
privprotocol: |
| 207 |
type: int |
| 208 |
description: |
| 209 |
- SNMPv3 privacy protocol. |
| 210 |
- Used when I(securitylevel=2)(authPriv). |
| 211 |
- Variants 2,3,4,5 are supported only on Zabbix 5.4 or greater |
| 212 |
- 0 (DES), 1 (AES128), 2 (AES192), 3 (AES256), 4 (AES192C), 5 (AES256C) |
| 213 |
default: 0 |
| 214 |
choices: [0, 1, 2, 3, 4, 5] |
| 215 |
privpassphrase: |
| 216 |
type: str |
| 217 |
description: |
| 218 |
- SNMPv3 privacy passphrase. |
| 219 |
- Used when I(securitylevel=2)(AuthPriv). |
| 220 |
default: "" |
| 221 |
tls_connect: |
| 222 |
description: |
| 223 |
- Specifies what encryption to use for outgoing connections. |
| 224 |
- Possible values, 1 (no encryption), 2 (PSK), 4 (certificate). |
| 225 |
type: int |
| 226 |
tls_accept: |
| 227 |
description: |
| 228 |
- Specifies what types of connections are allowed for incoming connections. |
| 229 |
- The tls_accept parameter accepts values of 1 to 7 |
| 230 |
- Possible values, 1 (no encryption), 2 (PSK), 4 (certificate). |
| 231 |
- Values can be combined. |
| 232 |
type: int |
| 233 |
tls_psk_identity: |
| 234 |
description: |
| 235 |
- It is a unique name by which this specific PSK is referred to by Zabbix components |
| 236 |
- Do not put sensitive information in the PSK identity string, it is transmitted over the network unencrypted. |
| 237 |
- Works only with >= Zabbix 3.0 |
| 238 |
- Using this parameter with Zabbix >= 5.4 makes this module non-idempotent. |
| 239 |
type: str |
| 240 |
tls_psk: |
| 241 |
description: |
| 242 |
- PSK value is a hard to guess string of hexadecimal digits. |
| 243 |
- The preshared key, at least 32 hex digits. Required if either I(tls_connect) or I(tls_accept) has PSK enabled. |
| 244 |
- Using this parameter makes this module non-idempotent. |
| 245 |
type: str |
| 246 |
ca_cert: |
| 247 |
description: |
| 248 |
- Required certificate issuer. |
| 249 |
aliases: [ tls_issuer ] |
| 250 |
type: str |
| 251 |
tls_subject: |
| 252 |
description: |
| 253 |
- Required certificate subject. |
| 254 |
type: str |
| 255 |
ipmi_authtype: |
| 256 |
description: |
| 257 |
- IPMI authentication algorithm. |
| 258 |
- Please review the Host object documentation for more information on the supported properties |
| 259 |
- "https://www.zabbix.com/documentation/3.4/manual/api/reference/host/object" |
| 260 |
- Possible values are, C(0) (none), C(1) (MD2), C(2) (MD5), C(4) (straight), C(5) (OEM), C(6) (RMCP+), |
| 261 |
with -1 being the API default. |
| 262 |
- Please note that the Zabbix API will treat absent settings as default when updating |
| 263 |
any of the I(ipmi_)-options; this means that if you attempt to set any of the four |
| 264 |
options individually, the rest will be reset to default values. |
| 265 |
type: int |
| 266 |
ipmi_privilege: |
| 267 |
description: |
| 268 |
- IPMI privilege level. |
| 269 |
- Please review the Host object documentation for more information on the supported properties |
| 270 |
- "https://www.zabbix.com/documentation/3.4/manual/api/reference/host/object" |
| 271 |
- Possible values are C(1) (callback), C(2) (user), C(3) (operator), C(4) (admin), C(5) (OEM), with C(2) |
| 272 |
being the API default. |
| 273 |
- also see the last note in the I(ipmi_authtype) documentation |
| 274 |
type: int |
| 275 |
ipmi_username: |
| 276 |
description: |
| 277 |
- IPMI username. |
| 278 |
- also see the last note in the I(ipmi_authtype) documentation |
| 279 |
type: str |
| 280 |
ipmi_password: |
| 281 |
description: |
| 282 |
- IPMI password. |
| 283 |
- also see the last note in the I(ipmi_authtype) documentation |
| 284 |
type: str |
| 285 |
force: |
| 286 |
description: |
| 287 |
- Overwrite the host configuration, even if already present. |
| 288 |
type: bool |
| 289 |
default: "yes" |
| 290 |
macros: |
| 291 |
description: |
| 292 |
- List of user macros to assign to the zabbix host. |
| 293 |
- Providing I(macros=[]) with I(force=yes) will clean all of the existing user macros from the host. |
| 294 |
type: list |
| 295 |
elements: dict |
| 296 |
suboptions: |
| 297 |
macro: |
| 298 |
description: |
| 299 |
- Name of the user macro. |
| 300 |
- Can be in zabbix native format "{$MACRO}" or short format "MACRO". |
| 301 |
type: str |
| 302 |
required: true |
| 303 |
value: |
| 304 |
description: |
| 305 |
- Value of the user macro. |
| 306 |
type: str |
| 307 |
required: true |
| 308 |
description: |
| 309 |
description: |
| 310 |
- Description of the user macro. |
| 311 |
type: str |
| 312 |
required: false |
| 313 |
default: "" |
| 314 |
type: |
| 315 |
description: |
| 316 |
- Type of the macro. |
| 317 |
- Since value is not returned by API for secret macros, there is no reliable way to |
| 318 |
detect changes in the content of secret macro value. |
| 319 |
- To update secret macro value, please update description alongside it so it passes |
| 320 |
the check. |
| 321 |
choices: [text, secret, vault] |
| 322 |
type: str |
| 323 |
required: false |
| 324 |
default: text |
| 325 |
aliases: [ user_macros ] |
| 326 |
tags: |
| 327 |
description: |
| 328 |
- List of host tags to assign to the zabbix host. |
| 329 |
- Providing I(tags=[]) with I(force=yes) will clean all of the tags from the host. |
| 330 |
type: list |
| 331 |
elements: dict |
| 332 |
suboptions: |
| 333 |
tag: |
| 334 |
description: |
| 335 |
- Name of the host tag. |
| 336 |
type: str |
| 337 |
required: true |
| 338 |
value: |
| 339 |
description: |
| 340 |
- Value of the host tag. |
| 341 |
type: str |
| 342 |
default: "" |
| 343 |
aliases: [ host_tags ] |
| 344 |
|
| 345 |
extends_documentation_fragment: |
| 346 |
- community.zabbix.zabbix |
| 347 |
|
| 348 |
""" |
| 349 |
|
| 350 |
EXAMPLES = r""" |
| 351 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 352 |
- name: Set credentials to access Zabbix Server API |
| 353 |
ansible.builtin.set_fact: |
| 354 |
ansible_user: Admin |
| 355 |
ansible_httpapi_pass: zabbix |
| 356 |
|
| 357 |
# If you want to use API token to be authenticated by Zabbix Server |
| 358 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 359 |
- name: Set API token |
| 360 |
ansible.builtin.set_fact: |
| 361 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 362 |
|
| 363 |
- name: Create a new host or rewrite an existing host's info (Zabbix <= 7.0) |
| 364 |
# Set task level following variables for Zabbix Server host in task |
| 365 |
vars: |
| 366 |
ansible_network_os: community.zabbix.zabbix |
| 367 |
ansible_connection: httpapi |
| 368 |
ansible_httpapi_port: 443 |
| 369 |
ansible_httpapi_use_ssl: true |
| 370 |
ansible_httpapi_validate_certs: false |
| 371 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 372 |
become: false |
| 373 |
delegate_to: zabbix-example-fqdn.org# you can use delegate_to or task level ansible_host like next example |
| 374 |
community.zabbix.zabbix_host: |
| 375 |
host_name: ExampleHost |
| 376 |
visible_name: ExampleName |
| 377 |
description: My ExampleHost Description |
| 378 |
host_groups: |
| 379 |
- Example group1 |
| 380 |
- Example group2 |
| 381 |
link_templates: |
| 382 |
- Example template1 |
| 383 |
- Example template2 |
| 384 |
status: enabled |
| 385 |
state: present |
| 386 |
inventory_mode: manual |
| 387 |
inventory_zabbix: |
| 388 |
tag: "{{ your_tag }}" |
| 389 |
alias: "{{ your_alias }}" |
| 390 |
notes: "Special Informations: {{ your_informations | default('None') }}" |
| 391 |
location: "{{ your_location }}" |
| 392 |
site_rack: "{{ your_site_rack }}" |
| 393 |
os: "{{ your_os }}" |
| 394 |
hardware: "{{ your_hardware }}" |
| 395 |
ipmi_authtype: 2 |
| 396 |
ipmi_privilege: 4 |
| 397 |
ipmi_username: username |
| 398 |
ipmi_password: password |
| 399 |
interfaces: |
| 400 |
- type: 1 |
| 401 |
main: 1 |
| 402 |
useip: 1 |
| 403 |
ip: 10.xx.xx.xx |
| 404 |
dns: "" |
| 405 |
port: "10050" |
| 406 |
- type: 4 |
| 407 |
main: 1 |
| 408 |
useip: 1 |
| 409 |
ip: 10.xx.xx.xx |
| 410 |
dns: "" |
| 411 |
port: "12345" |
| 412 |
proxy: a.zabbix.proxy |
| 413 |
macros: |
| 414 |
- macro: "{$EXAMPLEMACRO}" |
| 415 |
value: ExampleMacroValue |
| 416 |
- macro: EXAMPLEMACRO2 |
| 417 |
value: ExampleMacroValue2 |
| 418 |
description: Example desc that work only with Zabbix 4.4 and higher |
| 419 |
tags: |
| 420 |
- tag: ExampleHostsTag |
| 421 |
- tag: ExampleHostsTag2 |
| 422 |
value: ExampleTagValue |
| 423 |
|
| 424 |
- name: Create a new host or update it - monitored by Zabbix Proxy (Zabbix >= 7.0) |
| 425 |
# Set task level following variables for Zabbix Server host in task |
| 426 |
vars: |
| 427 |
ansible_network_os: community.zabbix.zabbix |
| 428 |
ansible_connection: httpapi |
| 429 |
ansible_httpapi_port: 443 |
| 430 |
ansible_httpapi_use_ssl: true |
| 431 |
ansible_httpapi_validate_certs: false |
| 432 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 433 |
become: false |
| 434 |
delegate_to: zabbix-example-fqdn.org# you can use delegate_to or task level ansible_host like next example |
| 435 |
community.zabbix.zabbix_host: |
| 436 |
host_name: ExampleHost |
| 437 |
host_groups: |
| 438 |
- Example group1 |
| 439 |
status: enabled |
| 440 |
state: present |
| 441 |
monitored_by: proxy |
| 442 |
proxy: a.zabbix.proxy |
| 443 |
|
| 444 |
- name: Create a new host or update it - monitored by Zabbix Proxy Group (Zabbix >= 7.0) |
| 445 |
# Set task level following variables for Zabbix Server host in task |
| 446 |
vars: |
| 447 |
ansible_network_os: community.zabbix.zabbix |
| 448 |
ansible_connection: httpapi |
| 449 |
ansible_httpapi_port: 443 |
| 450 |
ansible_httpapi_use_ssl: true |
| 451 |
ansible_httpapi_validate_certs: false |
| 452 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 453 |
become: false |
| 454 |
delegate_to: zabbix-example-fqdn.org# you can use delegate_to or task level ansible_host like next example |
| 455 |
community.zabbix.zabbix_host: |
| 456 |
host_name: ExampleHost |
| 457 |
host_groups: |
| 458 |
- Example group1 |
| 459 |
status: enabled |
| 460 |
state: present |
| 461 |
monitored_by: proxy_group |
| 462 |
proxy: a.zabbix.proxy.group |
| 463 |
|
| 464 |
- name: Update an existing host's TLS settings |
| 465 |
# Set current task level variables for Zabbix Server host in task |
| 466 |
vars: |
| 467 |
ansible_network_os: community.zabbix.zabbix |
| 468 |
ansible_connection: httpapi |
| 469 |
ansible_httpapi_port: 443 |
| 470 |
ansible_httpapi_use_ssl: true |
| 471 |
ansible_httpapi_validate_certs: false |
| 472 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 473 |
ansible_host: zabbix-example-fqdn.org # you can use task level ansible_host or delegate_to like in previous example |
| 474 |
become: false |
| 475 |
community.zabbix.zabbix_host: |
| 476 |
host_name: ExampleHost |
| 477 |
visible_name: ExampleName |
| 478 |
host_groups: |
| 479 |
- Example group1 |
| 480 |
tls_psk_identity: test |
| 481 |
tls_connect: 2 |
| 482 |
tls_psk: 123456789abcdef123456789abcdef12 |
| 483 |
force: false |
| 484 |
""" |
| 485 |
|
| 486 |
|
| 487 |
import copy |
| 488 |
|
| 489 |
from ansible.module_utils.basic import AnsibleModule |
| 490 |
|
| 491 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 492 |
|
| 493 |
from ansible.module_utils.compat.version import LooseVersion |
| 494 |
|
| 495 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 496 |
|
| 497 |
|
| 498 |
class Host(ZabbixBase): |
| 499 |
# exist host |
| 500 |
def is_host_exist(self, host_name): |
| 501 |
result = self._zapi.host.get({"filter": {"host": host_name}}) |
| 502 |
return result |
| 503 |
|
| 504 |
# check if host group exists |
| 505 |
def check_host_group_exist(self, group_names): |
| 506 |
for group_name in group_names: |
| 507 |
result = self._zapi.hostgroup.get({"filter": {"name": group_name}}) |
| 508 |
if not result: |
| 509 |
self._module.fail_json(msg="Hostgroup not found: %s" % group_name) |
| 510 |
return True |
| 511 |
|
| 512 |
def get_template_ids(self, template_list): |
| 513 |
template_ids = [] |
| 514 |
if template_list is None or len(template_list) == 0: |
| 515 |
return template_ids |
| 516 |
for template in template_list: |
| 517 |
template_list = self._zapi.template.get({"output": ["templateid"], "filter": {"host": template}}) |
| 518 |
if len(template_list) < 1: |
| 519 |
self._module.fail_json(msg="Template not found: %s" % template) |
| 520 |
else: |
| 521 |
template_id = template_list[0]["templateid"] |
| 522 |
template_ids.append(template_id) |
| 523 |
return template_ids |
| 524 |
|
| 525 |
def add_host(self, host_name, group_ids, status, interfaces, proxy_id, visible_name, description, tls_connect, |
| 526 |
tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject, ipmi_authtype, ipmi_privilege, |
| 527 |
ipmi_username, ipmi_password, macros, tags, monitored_by, proxy_group_id): |
| 528 |
try: |
| 529 |
if self._module.check_mode: |
| 530 |
self._module.exit_json(changed=True) |
| 531 |
parameters = {"host": host_name, "interfaces": interfaces, "groups": group_ids, "status": status} |
| 532 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 533 |
if proxy_id: |
| 534 |
parameters["proxy_hostid"] = proxy_id |
| 535 |
else: |
| 536 |
if monitored_by == 1: |
| 537 |
if proxy_id: |
| 538 |
parameters["monitored_by"] = 1 # By single proxy |
| 539 |
parameters["proxyid"] = proxy_id |
| 540 |
elif monitored_by == 2: |
| 541 |
if proxy_group_id: |
| 542 |
parameters["monitored_by"] = 2 # By single proxy |
| 543 |
parameters["proxy_groupid"] = proxy_group_id |
| 544 |
if visible_name: |
| 545 |
parameters["name"] = visible_name |
| 546 |
if tls_connect: |
| 547 |
parameters["tls_connect"] = tls_connect |
| 548 |
if tls_accept: |
| 549 |
parameters["tls_accept"] = tls_accept |
| 550 |
if tls_psk_identity is not None: |
| 551 |
parameters["tls_psk_identity"] = tls_psk_identity |
| 552 |
if tls_psk is not None: |
| 553 |
parameters["tls_psk"] = tls_psk |
| 554 |
if tls_issuer is not None: |
| 555 |
parameters["tls_issuer"] = tls_issuer |
| 556 |
if tls_subject is not None: |
| 557 |
parameters["tls_subject"] = tls_subject |
| 558 |
if description: |
| 559 |
parameters["description"] = description |
| 560 |
if ipmi_authtype is not None: |
| 561 |
parameters["ipmi_authtype"] = ipmi_authtype |
| 562 |
if ipmi_privilege is not None: |
| 563 |
parameters["ipmi_privilege"] = ipmi_privilege |
| 564 |
if ipmi_username is not None: |
| 565 |
parameters["ipmi_username"] = ipmi_username |
| 566 |
if ipmi_password is not None: |
| 567 |
parameters["ipmi_password"] = ipmi_password |
| 568 |
if macros is not None: |
| 569 |
parameters["macros"] = macros |
| 570 |
if tags is not None: |
| 571 |
parameters["tags"] = tags |
| 572 |
|
| 573 |
host_list = self._zapi.host.create(parameters) |
| 574 |
if len(host_list) >= 1: |
| 575 |
return host_list["hostids"][0] |
| 576 |
except Exception as e: |
| 577 |
self._module.fail_json(msg="Failed to create host %s: %s" % (host_name, e)) |
| 578 |
|
| 579 |
def update_host(self, host_name, group_ids, status, host_id, interfaces, exist_interface_list, proxy_id, |
| 580 |
visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, |
| 581 |
tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags, |
| 582 |
discovered_host, zabbix_host_obj, monitored_by, proxy_group_id): |
| 583 |
try: |
| 584 |
if self._module.check_mode: |
| 585 |
self._module.exit_json(changed=True) |
| 586 |
if discovered_host: |
| 587 |
# The host was discovered via Discovery Rule |
| 588 |
parameters = {"hostid": host_id, "status": status} |
| 589 |
else: |
| 590 |
# A "plain" host |
| 591 |
parameters = {"hostid": host_id, "groups": group_ids, "status": status} |
| 592 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 593 |
if (proxy_id >= 0 and proxy_id != zabbix_host_obj["proxy_hostid"]): |
| 594 |
parameters["proxy_hostid"] = proxy_id |
| 595 |
else: |
| 596 |
if monitored_by == 1: |
| 597 |
if (proxy_id >= 0 and proxy_id != zabbix_host_obj["proxyid"]): |
| 598 |
parameters["proxyid"] = proxy_id |
| 599 |
elif monitored_by == 2: |
| 600 |
if (proxy_group_id >= 0 and proxy_group_id != zabbix_host_obj["proxy_groupid"]): |
| 601 |
parameters["proxy_groupid"] = proxy_group_id |
| 602 |
parameters["monitored_by"] = monitored_by |
| 603 |
if (visible_name is not None and visible_name != zabbix_host_obj["name"]): |
| 604 |
parameters["name"] = visible_name |
| 605 |
if (tls_connect is not None and tls_connect != zabbix_host_obj["tls_connect"]): |
| 606 |
parameters["tls_connect"] = tls_connect |
| 607 |
if (tls_accept is not None and tls_accept != zabbix_host_obj["tls_accept"]): |
| 608 |
parameters["tls_accept"] = tls_accept |
| 609 |
if tls_psk_identity: |
| 610 |
parameters["tls_psk_identity"] = tls_psk_identity |
| 611 |
if tls_psk: |
| 612 |
parameters["tls_psk"] = tls_psk |
| 613 |
if (tls_issuer is not None and tls_issuer != zabbix_host_obj["tls_issuer"]): |
| 614 |
parameters["tls_issuer"] = tls_issuer |
| 615 |
if (tls_subject is not None and tls_subject != zabbix_host_obj["tls_subject"]): |
| 616 |
parameters["tls_subject"] = tls_subject |
| 617 |
if (description is not None and description != zabbix_host_obj["description"]): |
| 618 |
parameters["description"] = description |
| 619 |
if (ipmi_authtype is not None and ipmi_authtype != zabbix_host_obj["ipmi_authtype"]): |
| 620 |
parameters["ipmi_authtype"] = ipmi_authtype |
| 621 |
if (ipmi_privilege is not None and ipmi_privilege != zabbix_host_obj["ipmi_privilege"]): |
| 622 |
parameters["ipmi_privilege"] = ipmi_privilege |
| 623 |
if (ipmi_username is not None and ipmi_username != zabbix_host_obj["ipmi_username"]): |
| 624 |
parameters["ipmi_username"] = ipmi_username |
| 625 |
if (ipmi_password is not None and ipmi_password != zabbix_host_obj["ipmi_password"]): |
| 626 |
parameters["ipmi_password"] = ipmi_password |
| 627 |
if interfaces: |
| 628 |
parameters["interfaces"] = interfaces |
| 629 |
if macros is not None: |
| 630 |
parameters["macros"] = macros |
| 631 |
if tags is not None: |
| 632 |
parameters["tags"] = tags |
| 633 |
|
| 634 |
self._zapi.host.update(parameters) |
| 635 |
except Exception as e: |
| 636 |
self._module.fail_json(msg="Failed to update host %s: %s" % (host_name, e)) |
| 637 |
|
| 638 |
def delete_host(self, host_id, host_name): |
| 639 |
try: |
| 640 |
if self._module.check_mode: |
| 641 |
self._module.exit_json(changed=True) |
| 642 |
self._zapi.host.delete([host_id]) |
| 643 |
except Exception as e: |
| 644 |
self._module.fail_json(msg="Failed to delete host %s: %s" % (host_name, e)) |
| 645 |
|
| 646 |
# get host by host name |
| 647 |
def get_host_by_host_name(self, host_name): |
| 648 |
params = { |
| 649 |
"output": "extend", |
| 650 |
"selectInventory": "extend", |
| 651 |
"selectMacros": "extend", |
| 652 |
"selectTags": ["tag", "value"], |
| 653 |
"filter": { |
| 654 |
"host": [host_name] |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
host_list = self._zapi.host.get(params) |
| 659 |
if len(host_list) < 1: |
| 660 |
self._module.fail_json(msg="Host not found: %s" % host_name) |
| 661 |
else: |
| 662 |
return host_list[0] |
| 663 |
|
| 664 |
# get proxyid by proxy name |
| 665 |
def get_proxyid_by_proxy_name(self, proxy_name): |
| 666 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 667 |
proxy_list = self._zapi.proxy.get({"output": "extend", "filter": {"host": [proxy_name]}}) |
| 668 |
else: |
| 669 |
proxy_list = self._zapi.proxy.get({"output": "extend", "filter": {"name": [proxy_name]}}) |
| 670 |
if len(proxy_list) < 1: |
| 671 |
self._module.fail_json(msg="Proxy not found: %s" % proxy_name) |
| 672 |
else: |
| 673 |
return int(proxy_list[0]["proxyid"]) |
| 674 |
|
| 675 |
# get proxy_group_id by proxy group name |
| 676 |
def get_proxy_group_id_by_name(self, proxy_group_name): |
| 677 |
proxy_group_list = self._zapi.proxygroup.get({"output": "extend", "filter": {"name": proxy_group_name}}) |
| 678 |
if len(proxy_group_list) < 1: |
| 679 |
self._module.fail_json(msg="Proxy group not found: %s" % proxy_group_name) |
| 680 |
else: |
| 681 |
return int(proxy_group_list[0]["proxy_groupid"]) |
| 682 |
|
| 683 |
# get group ids by group names |
| 684 |
def get_group_ids_by_group_names(self, group_names): |
| 685 |
if self.check_host_group_exist(group_names): |
| 686 |
return self._zapi.hostgroup.get({"output": ["groupid"], "filter": {"name": group_names}}) |
| 687 |
|
| 688 |
# get host groups ids by host id |
| 689 |
def get_group_ids_by_host_id(self, host_id): |
| 690 |
return self._zapi.hostgroup.get({"output": ["groupid"], "hostids": host_id}) |
| 691 |
|
| 692 |
# get host templates by host id |
| 693 |
def get_host_templates_by_host_id(self, host_id): |
| 694 |
template_ids = [] |
| 695 |
template_list = self._zapi.template.get({"output": "extend", "hostids": host_id}) |
| 696 |
for template in template_list: |
| 697 |
template_ids.append(template["templateid"]) |
| 698 |
return template_ids |
| 699 |
|
| 700 |
def construct_host_interfaces(self, interfaces): |
| 701 |
"""Ensures interfaces object is properly formatted before submitting it to API. |
| 702 |
|
| 703 |
Args: |
| 704 |
interfaces (list): list of dictionaries for each interface present on the host. |
| 705 |
|
| 706 |
Returns: |
| 707 |
(interfaces, ip) - where interfaces is original list reformated into a valid format |
| 708 |
and ip is any IP address found on interface of type agent (printing purposes only). |
| 709 |
""" |
| 710 |
ip = "" |
| 711 |
interface_types = {"agent": 1, "snmp": 2, "ipmi": 3, "jmx": 4} |
| 712 |
type_to_port = {1: "10050", 2: "161", 3: "623", 4: "12345"} |
| 713 |
|
| 714 |
for interface in interfaces: |
| 715 |
if interface["type"] in list(interface_types.keys()): |
| 716 |
interface["type"] = interface_types[interface["type"]] |
| 717 |
else: |
| 718 |
interface["type"] = int(interface["type"]) |
| 719 |
|
| 720 |
if interface["type"] == 1: |
| 721 |
ip = interface.get("ip", "") |
| 722 |
|
| 723 |
for key in ["ip", "dns"]: |
| 724 |
if key not in interface or interface[key] is None: |
| 725 |
interface[key] = "" |
| 726 |
|
| 727 |
if "port" not in interface or interface["port"] is None: |
| 728 |
interface["port"] = type_to_port.get(interface["type"], "") |
| 729 |
|
| 730 |
if "bulk" in interface: |
| 731 |
del interface["bulk"] |
| 732 |
|
| 733 |
# Not handled in argument_spec with required_if since only SNMP interfaces are using details |
| 734 |
if interface["type"] == 2: |
| 735 |
if not interface["details"]: |
| 736 |
self._module.fail_json(msg="Option 'details' required for SNMP interface {0}".format(interface)) |
| 737 |
|
| 738 |
i_details = interface["details"] |
| 739 |
if i_details["version"] < 3 and not i_details.get("community", False): |
| 740 |
self._module.fail_json( |
| 741 |
msg="Option 'community' is required in 'details' for SNMP interface {0}".format(interface)) |
| 742 |
|
| 743 |
else: |
| 744 |
interface["details"] = {} |
| 745 |
|
| 746 |
return (interfaces, ip) |
| 747 |
|
| 748 |
# check the exist_interfaces whether it equals the interfaces or not |
| 749 |
def check_interface_properties(self, exist_interfaces, interfaces): |
| 750 |
# Find already configured interfaces in requested interfaces |
| 751 |
if len(exist_interfaces) != len(interfaces): |
| 752 |
return True |
| 753 |
|
| 754 |
for iface in interfaces: |
| 755 |
found = False |
| 756 |
for e_int in exist_interfaces: |
| 757 |
diff_dict = {} |
| 758 |
zabbix_utils.helper_cleanup_data(zabbix_utils.helper_compare_dictionaries(iface, e_int, diff_dict)) |
| 759 |
if diff_dict == {}: |
| 760 |
found = True |
| 761 |
break |
| 762 |
|
| 763 |
if interfaces and not found: |
| 764 |
return True |
| 765 |
|
| 766 |
return False |
| 767 |
|
| 768 |
# get the status of host by host |
| 769 |
def get_host_status_by_host(self, host): |
| 770 |
return host["status"] |
| 771 |
|
| 772 |
# check all the properties before link or clear template |
| 773 |
def check_all_properties(self, host_id, group_ids, status, interfaces, template_ids, |
| 774 |
exist_interfaces, host, proxy_id, visible_name, description, host_name, |
| 775 |
inventory_mode, inventory_zabbix, tls_accept, tls_psk_identity, tls_psk, |
| 776 |
tls_issuer, tls_subject, tls_connect, ipmi_authtype, ipmi_privilege, |
| 777 |
ipmi_username, ipmi_password, macros, tags, monitored_by, proxy_group_id): |
| 778 |
# get the existing host's groups |
| 779 |
exist_host_groups = sorted(self.get_group_ids_by_host_id(host_id), key=lambda k: k["groupid"]) |
| 780 |
if sorted(group_ids, key=lambda k: k["groupid"]) != exist_host_groups: |
| 781 |
return True |
| 782 |
|
| 783 |
# get the existing status |
| 784 |
exist_status = self.get_host_status_by_host(host) |
| 785 |
if int(status) != int(exist_status): |
| 786 |
return True |
| 787 |
|
| 788 |
# check the exist_interfaces whether it equals the interfaces or not |
| 789 |
if self.check_interface_properties(exist_interfaces, interfaces): |
| 790 |
return True |
| 791 |
|
| 792 |
# get the existing templates |
| 793 |
exist_template_ids = self.get_host_templates_by_host_id(host_id) |
| 794 |
if set(list(template_ids)) != set(exist_template_ids): |
| 795 |
return True |
| 796 |
|
| 797 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 798 |
if int(host["proxy_hostid"]) != int(proxy_id): |
| 799 |
return True |
| 800 |
else: |
| 801 |
if int(host["monitored_by"]) != monitored_by: |
| 802 |
return True |
| 803 |
if int(host["proxyid"]) != int(proxy_id): |
| 804 |
return True |
| 805 |
if int(host["proxy_groupid"]) != int(proxy_group_id): |
| 806 |
return True |
| 807 |
|
| 808 |
# Check whether the visible_name has changed; Zabbix defaults to the technical hostname if not set. |
| 809 |
if visible_name: |
| 810 |
if host["name"] != visible_name: |
| 811 |
return True |
| 812 |
|
| 813 |
# Only compare description if it is given as a module parameter |
| 814 |
if description: |
| 815 |
if host["description"] != description: |
| 816 |
return True |
| 817 |
|
| 818 |
if inventory_mode: |
| 819 |
if int(host["inventory_mode"]) != self.inventory_mode_numeric(inventory_mode): |
| 820 |
return True |
| 821 |
|
| 822 |
if inventory_zabbix: |
| 823 |
proposed_inventory = copy.deepcopy(host["inventory"]) |
| 824 |
proposed_inventory.update(inventory_zabbix) |
| 825 |
if proposed_inventory != host["inventory"]: |
| 826 |
return True |
| 827 |
|
| 828 |
if tls_accept is not None and "tls_accept" in host: |
| 829 |
if int(host["tls_accept"]) != tls_accept: |
| 830 |
return True |
| 831 |
|
| 832 |
# in Zabbix >= 5.4 these parameters are write-only and are not returned in host.get response |
| 833 |
if tls_psk_identity is not None or tls_psk is not None: |
| 834 |
return True |
| 835 |
|
| 836 |
if tls_issuer is not None and "tls_issuer" in host: |
| 837 |
if host["tls_issuer"] != tls_issuer: |
| 838 |
return True |
| 839 |
|
| 840 |
if tls_subject is not None and "tls_subject" in host: |
| 841 |
if host["tls_subject"] != tls_subject: |
| 842 |
return True |
| 843 |
|
| 844 |
if tls_connect is not None and "tls_connect" in host: |
| 845 |
if int(host["tls_connect"]) != tls_connect: |
| 846 |
return True |
| 847 |
if ipmi_authtype is not None: |
| 848 |
if int(host["ipmi_authtype"]) != ipmi_authtype: |
| 849 |
return True |
| 850 |
if ipmi_privilege is not None: |
| 851 |
if int(host["ipmi_privilege"]) != ipmi_privilege: |
| 852 |
return True |
| 853 |
if ipmi_username is not None: |
| 854 |
if host["ipmi_username"] != ipmi_username: |
| 855 |
return True |
| 856 |
if ipmi_password is not None: |
| 857 |
if host["ipmi_password"] != ipmi_password: |
| 858 |
return True |
| 859 |
|
| 860 |
# hostmacroid and hostid are present in every item of host["macros"] and need to be removed |
| 861 |
if macros is not None and "macros" in host: |
| 862 |
t_macros = copy.deepcopy(macros) # make copy to prevent change in original data |
| 863 |
for macro in host["macros"]: |
| 864 |
macro.pop("hostid", False) |
| 865 |
macro.pop("hostmacroid", False) |
| 866 |
|
| 867 |
diff = [] |
| 868 |
zabbix_utils.helper_compare_lists(t_macros, host["macros"], diff) |
| 869 |
if diff != []: |
| 870 |
return True |
| 871 |
|
| 872 |
if tags is not None and "tags" in host: |
| 873 |
if sorted(tags, key=lambda k: (k["tag"], k.get("value", ""))) != sorted(host["tags"], key=lambda k: (k["tag"], k.get("value", ""))): |
| 874 |
return True |
| 875 |
|
| 876 |
return False |
| 877 |
|
| 878 |
# link or clear template of the host |
| 879 |
def link_or_clear_template(self, host_id, template_id_list): |
| 880 |
# get host's exist template ids |
| 881 |
exist_template_id_list = self.get_host_templates_by_host_id(host_id) |
| 882 |
|
| 883 |
exist_template_ids = set(exist_template_id_list) |
| 884 |
template_ids = set(template_id_list) |
| 885 |
template_id_list = list(template_ids) |
| 886 |
template_id_list_ = [] |
| 887 |
for t in template_id_list: |
| 888 |
template_id_list_.append({"templateid": t}) |
| 889 |
|
| 890 |
# get unlink and clear templates |
| 891 |
templates_clear = exist_template_ids.difference(template_ids) |
| 892 |
templates_clear_list = list(templates_clear) |
| 893 |
templates_clear_list_ = [] |
| 894 |
for t in templates_clear_list: |
| 895 |
templates_clear_list_.append({"templateid": t}) |
| 896 |
|
| 897 |
request_str = {"hostid": host_id, "templates": template_id_list_, "templates_clear": templates_clear_list_} |
| 898 |
try: |
| 899 |
if self._module.check_mode: |
| 900 |
self._module.exit_json(changed=True) |
| 901 |
self._zapi.host.update(request_str) |
| 902 |
except Exception as e: |
| 903 |
self._module.fail_json(msg="Failed to link template to host: %s" % e) |
| 904 |
|
| 905 |
def inventory_mode_numeric(self, inventory_mode): |
| 906 |
if inventory_mode == "automatic": |
| 907 |
return int(1) |
| 908 |
elif inventory_mode == "manual": |
| 909 |
return int(0) |
| 910 |
elif inventory_mode == "disabled": |
| 911 |
return int(-1) |
| 912 |
return inventory_mode |
| 913 |
|
| 914 |
# Update the host inventory_mode |
| 915 |
def update_inventory_mode(self, host_id, inventory_mode): |
| 916 |
|
| 917 |
# nothing was set, do nothing |
| 918 |
if not inventory_mode: |
| 919 |
return |
| 920 |
|
| 921 |
inventory_mode = self.inventory_mode_numeric(inventory_mode) |
| 922 |
|
| 923 |
# watch for - https://support.zabbix.com/browse/ZBX-6033 |
| 924 |
request_str = {"hostid": host_id, "inventory_mode": inventory_mode} |
| 925 |
try: |
| 926 |
if self._module.check_mode: |
| 927 |
self._module.exit_json(changed=True) |
| 928 |
self._zapi.host.update(request_str) |
| 929 |
except Exception as e: |
| 930 |
self._module.fail_json(msg="Failed to set inventory_mode to host: %s" % e) |
| 931 |
|
| 932 |
def update_inventory_zabbix(self, host_id, inventory): |
| 933 |
|
| 934 |
if not inventory: |
| 935 |
return |
| 936 |
|
| 937 |
request_str = {"hostid": host_id, "inventory": inventory} |
| 938 |
try: |
| 939 |
if self._module.check_mode: |
| 940 |
self._module.exit_json(changed=True) |
| 941 |
self._zapi.host.update(request_str) |
| 942 |
except Exception as e: |
| 943 |
self._module.fail_json(msg="Failed to set inventory to host: %s" % e) |
| 944 |
|
| 945 |
|
| 946 |
# Add all default values to all missing parameters for existing interfaces |
| 947 |
def update_exist_interfaces_with_defaults(exist_interfaces): |
| 948 |
|
| 949 |
new_exist_interfaces = [] |
| 950 |
default_interface = { |
| 951 |
"main": "0", |
| 952 |
"useip": "0", |
| 953 |
"ip": "", |
| 954 |
"dns": "", |
| 955 |
"port": "" |
| 956 |
} |
| 957 |
default_interface_details = { |
| 958 |
"version": 2, |
| 959 |
"bulk": 1, |
| 960 |
"community": "", |
| 961 |
"securityname": "", |
| 962 |
"contextname": "", |
| 963 |
"securitylevel": 0, |
| 964 |
"authprotocol": 0, |
| 965 |
"authpassphrase": "", |
| 966 |
"privprotocol": 0, |
| 967 |
"privpassphrase": "" |
| 968 |
} |
| 969 |
allowed_fields = [ |
| 970 |
'hostid', |
| 971 |
'type', |
| 972 |
'ip', |
| 973 |
'dns', |
| 974 |
'port', |
| 975 |
'useip', |
| 976 |
'main', |
| 977 |
'details', |
| 978 |
'interface_ref', |
| 979 |
'items', |
| 980 |
'interfaceid' |
| 981 |
] |
| 982 |
for interface in exist_interfaces: |
| 983 |
normalized_interface = interface |
| 984 |
denied_fields = list(set(interface.keys()) - set(allowed_fields)) |
| 985 |
normalized_interface = zabbix_utils.helper_normalize_data( |
| 986 |
interface, del_keys=denied_fields |
| 987 |
)[0] |
| 988 |
|
| 989 |
new_interface = default_interface.copy() |
| 990 |
new_interface.update(normalized_interface) |
| 991 |
new_interface["details"] = default_interface_details.copy() |
| 992 |
if "details" in interface: |
| 993 |
new_interface["details"].update(interface["details"]) |
| 994 |
new_exist_interfaces.append(new_interface) |
| 995 |
|
| 996 |
return new_exist_interfaces |
| 997 |
|
| 998 |
|
| 999 |
def normalize_macro_name(macro_name): |
| 1000 |
# Zabbix handles macro names in upper case characters |
| 1001 |
if ":" in macro_name: |
| 1002 |
macro_name = ":".join([macro_name.split(":")[0].upper(), ":".join(macro_name.split(":")[1:])]) |
| 1003 |
else: |
| 1004 |
macro_name = macro_name.upper() |
| 1005 |
|
| 1006 |
# Valid format for macro is {$MACRO} |
| 1007 |
if not macro_name.startswith("{$"): |
| 1008 |
macro_name = "{$" + macro_name |
| 1009 |
if not macro_name.endswith("}"): |
| 1010 |
macro_name = macro_name + "}" |
| 1011 |
|
| 1012 |
return macro_name |
| 1013 |
|
| 1014 |
|
| 1015 |
def main(): |
| 1016 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 1017 |
argument_spec.update(dict( |
| 1018 |
host_name=dict(type="str", required=True), |
| 1019 |
host_groups=dict(type="list", required=False, elements="str"), |
| 1020 |
link_templates=dict(type="list", required=False, elements="str"), |
| 1021 |
status=dict(type="str", default="enabled", choices=["enabled", "disabled"]), |
| 1022 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 1023 |
inventory_mode=dict(type="str", required=False, choices=["automatic", "manual", "disabled"]), |
| 1024 |
ipmi_authtype=dict(type="int", default=None), |
| 1025 |
ipmi_privilege=dict(type="int", default=None), |
| 1026 |
ipmi_username=dict(type="str", required=False, default=None), |
| 1027 |
ipmi_password=dict(type="str", required=False, default=None, no_log=True), |
| 1028 |
tls_connect=dict(type="int", required=False), |
| 1029 |
tls_accept=dict(type="int", required=False), |
| 1030 |
tls_psk_identity=dict(type="str", required=False), |
| 1031 |
tls_psk=dict(type="str", required=False, no_log=True), |
| 1032 |
ca_cert=dict(type="str", required=False, aliases=["tls_issuer"]), |
| 1033 |
tls_subject=dict(type="str", required=False), |
| 1034 |
inventory_zabbix=dict(type="dict", required=False), |
| 1035 |
interfaces=dict( |
| 1036 |
type="list", |
| 1037 |
elements="dict", |
| 1038 |
default=[], |
| 1039 |
options=dict( |
| 1040 |
type=dict(type="str", required=True, choices=["agent", "1", "snmp", "2", "ipmi", "3", "jmx", "4"]), |
| 1041 |
main=dict(type="int", choices=[0, 1], default=0), |
| 1042 |
useip=dict(type="int", choices=[0, 1], default=0), |
| 1043 |
ip=dict(type="str"), |
| 1044 |
dns=dict(type="str"), |
| 1045 |
port=dict(type="str"), |
| 1046 |
details=dict( |
| 1047 |
type="dict", |
| 1048 |
default={}, |
| 1049 |
options=dict( |
| 1050 |
version=dict(type="int", choices=[1, 2, 3], default=2), |
| 1051 |
bulk=dict(type="int", choices=[0, 1], default=1), |
| 1052 |
community=dict(type="str", default=""), |
| 1053 |
securityname=dict(type="str", default=""), |
| 1054 |
contextname=dict(type="str", default=""), |
| 1055 |
securitylevel=dict(type="int", choices=[0, 1, 2], default=0), |
| 1056 |
authprotocol=dict(type="int", choices=[0, 1, 2, 3, 4, 5], default=0), |
| 1057 |
authpassphrase=dict(type="str", default="", no_log=True), |
| 1058 |
privprotocol=dict(type="int", choices=[0, 1, 2, 3, 4, 5], default=0), |
| 1059 |
privpassphrase=dict(type="str", default="", no_log=True) |
| 1060 |
) |
| 1061 |
) |
| 1062 |
), |
| 1063 |
required_if=[ |
| 1064 |
["useip", 0, ["dns"]], |
| 1065 |
["useip", 1, ["ip"]] |
| 1066 |
] |
| 1067 |
), |
| 1068 |
force=dict(type="bool", default=True), |
| 1069 |
monitored_by=dict(type="str", required=False, choices=["zabbix_server", "proxy", "proxy_group"], default=None), |
| 1070 |
proxy=dict(type="str", required=False), |
| 1071 |
proxy_group=dict(type="str", required=False), |
| 1072 |
visible_name=dict(type="str", required=False), |
| 1073 |
description=dict(type="str", required=False), |
| 1074 |
macros=dict( |
| 1075 |
type="list", |
| 1076 |
elements="dict", |
| 1077 |
aliases=["user_macros"], |
| 1078 |
options=dict( |
| 1079 |
macro=dict(type="str", required=True), |
| 1080 |
value=dict(type="str", required=True), |
| 1081 |
description=dict(type="str", default=""), |
| 1082 |
type=dict(type="str", default="text", choices=["text", "secret", "vault"]) |
| 1083 |
) |
| 1084 |
), |
| 1085 |
tags=dict( |
| 1086 |
type="list", |
| 1087 |
elements="dict", |
| 1088 |
aliases=["host_tags"], |
| 1089 |
options=dict( |
| 1090 |
tag=dict(type="str", required=True), |
| 1091 |
value=dict(type="str", default="") |
| 1092 |
) |
| 1093 |
) |
| 1094 |
)) |
| 1095 |
|
| 1096 |
module = AnsibleModule( |
| 1097 |
argument_spec=argument_spec, |
| 1098 |
required_if=([ |
| 1099 |
["monitored_by", "proxy", ("proxy",)], |
| 1100 |
["monitored_by", "proxy_group", ("proxy_group",)] |
| 1101 |
]), |
| 1102 |
supports_check_mode=True |
| 1103 |
) |
| 1104 |
|
| 1105 |
host_name = module.params["host_name"] |
| 1106 |
visible_name = module.params["visible_name"] |
| 1107 |
description = module.params["description"] |
| 1108 |
host_groups = module.params["host_groups"] |
| 1109 |
link_templates = module.params["link_templates"] |
| 1110 |
inventory_mode = module.params["inventory_mode"] |
| 1111 |
ipmi_authtype = module.params["ipmi_authtype"] |
| 1112 |
ipmi_privilege = module.params["ipmi_privilege"] |
| 1113 |
ipmi_username = module.params["ipmi_username"] |
| 1114 |
ipmi_password = module.params["ipmi_password"] |
| 1115 |
tls_connect = module.params["tls_connect"] |
| 1116 |
tls_accept = module.params["tls_accept"] |
| 1117 |
tls_psk_identity = module.params["tls_psk_identity"] |
| 1118 |
tls_psk = module.params["tls_psk"] |
| 1119 |
tls_issuer = module.params["ca_cert"] |
| 1120 |
tls_subject = module.params["tls_subject"] |
| 1121 |
inventory_zabbix = module.params["inventory_zabbix"] |
| 1122 |
status = module.params["status"] |
| 1123 |
state = module.params["state"] |
| 1124 |
interfaces = module.params["interfaces"] |
| 1125 |
force = module.params["force"] |
| 1126 |
proxy = module.params["proxy"] |
| 1127 |
macros = module.params["macros"] |
| 1128 |
tags = module.params["tags"] |
| 1129 |
monitored_by = module.params["monitored_by"] |
| 1130 |
proxy_group = module.params["proxy_group"] |
| 1131 |
|
| 1132 |
# convert enabled to 0; disabled to 1 |
| 1133 |
status = 1 if status == "disabled" else 0 |
| 1134 |
|
| 1135 |
# convert monitored_by to int |
| 1136 |
if monitored_by == "zabbix_server": |
| 1137 |
monitored_by = 0 |
| 1138 |
elif monitored_by == "proxy": |
| 1139 |
monitored_by = 1 |
| 1140 |
elif monitored_by == "proxy_group": |
| 1141 |
monitored_by = 2 |
| 1142 |
|
| 1143 |
host = Host(module) |
| 1144 |
|
| 1145 |
template_ids = [] |
| 1146 |
if link_templates: |
| 1147 |
template_ids = host.get_template_ids(link_templates) |
| 1148 |
|
| 1149 |
group_ids = [] |
| 1150 |
|
| 1151 |
if host_groups is not None: |
| 1152 |
if len(host_groups) >= 1: |
| 1153 |
group_ids = host.get_group_ids_by_group_names(host_groups) |
| 1154 |
else: |
| 1155 |
module.fail_json(msg="host_groups must be not empty list.") |
| 1156 |
|
| 1157 |
interfaces, ip = host.construct_host_interfaces(interfaces) |
| 1158 |
|
| 1159 |
if macros: |
| 1160 |
# convert macros to zabbix native format - {$MACRO} |
| 1161 |
for macro in macros: |
| 1162 |
macro["macro"] = normalize_macro_name(macro["macro"]) |
| 1163 |
|
| 1164 |
if "type" in macro: |
| 1165 |
if macro["type"] == "text": |
| 1166 |
macro["type"] = "0" |
| 1167 |
elif macro["type"] == "secret": |
| 1168 |
macro["type"] = "1" |
| 1169 |
elif macro["type"] == "vault": |
| 1170 |
macro["type"] = "2" |
| 1171 |
|
| 1172 |
# Use proxy specified, or set to 0 |
| 1173 |
if proxy: |
| 1174 |
proxy_id = host.get_proxyid_by_proxy_name(proxy) |
| 1175 |
else: |
| 1176 |
proxy_id = 0 |
| 1177 |
|
| 1178 |
if proxy_group: |
| 1179 |
proxy_group_id = host.get_proxy_group_id_by_name(proxy_group) |
| 1180 |
else: |
| 1181 |
proxy_group_id = 0 |
| 1182 |
|
| 1183 |
# check if host exist |
| 1184 |
is_host_exist = host.is_host_exist(host_name) |
| 1185 |
|
| 1186 |
if is_host_exist: |
| 1187 |
# get host id by host name |
| 1188 |
zabbix_host_obj = host.get_host_by_host_name(host_name) |
| 1189 |
host_id = zabbix_host_obj["hostid"] |
| 1190 |
discovered_host = zabbix_host_obj["flags"] == '4' |
| 1191 |
|
| 1192 |
# If proxy is not specified as a module parameter, use the existing setting |
| 1193 |
if proxy is None: |
| 1194 |
if LooseVersion(host._zbx_api_version) < LooseVersion("7.0"): |
| 1195 |
proxy_id = int(zabbix_host_obj["proxy_hostid"]) |
| 1196 |
else: |
| 1197 |
proxy_id = int(zabbix_host_obj["proxyid"]) |
| 1198 |
|
| 1199 |
if LooseVersion(host._zbx_api_version) >= LooseVersion("7.0"): |
| 1200 |
# If monitored_by and proxy_group are not specified as a module parameters, use the existing setting |
| 1201 |
if monitored_by is None: |
| 1202 |
monitored_by = int(zabbix_host_obj["monitored_by"]) |
| 1203 |
if proxy_group is None: |
| 1204 |
proxy_group_id = int(zabbix_host_obj["proxy_groupid"]) |
| 1205 |
|
| 1206 |
if state == "absent": |
| 1207 |
# remove host |
| 1208 |
host.delete_host(host_id, host_name) |
| 1209 |
module.exit_json(changed=True, result="Successfully delete host %s" % host_name) |
| 1210 |
else: |
| 1211 |
if not host_groups: |
| 1212 |
# if host_groups have not been specified when updating an existing host, just |
| 1213 |
# get the group_ids from the existing host without updating them. |
| 1214 |
group_ids = host.get_group_ids_by_host_id(host_id) |
| 1215 |
|
| 1216 |
# get existing host's interfaces |
| 1217 |
exist_interfaces = host._zapi.hostinterface.get({"output": "extend", "hostids": host_id}) |
| 1218 |
exist_interfaces.sort(key=lambda x: int(x["interfaceid"])) |
| 1219 |
exist_interfaces = update_exist_interfaces_with_defaults(exist_interfaces) |
| 1220 |
|
| 1221 |
# Convert integer parameters from strings to ints |
| 1222 |
for idx, interface in enumerate(copy.deepcopy(exist_interfaces)): |
| 1223 |
for key in tuple(interface.keys()): |
| 1224 |
# fix values for properties |
| 1225 |
if key in ["useip", "main", "type", "bulk"]: |
| 1226 |
exist_interfaces[idx][key] = int(interface[key]) |
| 1227 |
elif key == "details": |
| 1228 |
if not interface[key]: |
| 1229 |
exist_interfaces[idx][key] = {} |
| 1230 |
else: |
| 1231 |
for d_key in interface[key].keys(): |
| 1232 |
if d_key in ["version", "bulk", "securitylevel", "authprotocol", "privprotocol"]: |
| 1233 |
exist_interfaces[idx][key][d_key] = int(interface[key][d_key]) |
| 1234 |
|
| 1235 |
interfaces_copy = copy.deepcopy(interfaces) |
| 1236 |
found_in_interfaces = [] |
| 1237 |
for idx, interface in enumerate(copy.deepcopy(exist_interfaces)): |
| 1238 |
interfaceid = interface["interfaceid"] |
| 1239 |
hostid = interface["hostid"] |
| 1240 |
|
| 1241 |
if not interfaces_copy: |
| 1242 |
# Whe no interfaces specified, copy existing interfaces |
| 1243 |
interfaces.append(interface) |
| 1244 |
continue |
| 1245 |
|
| 1246 |
# Find already configured interfaces in requested interfaces and compile final list of |
| 1247 |
# interfaces in "interfaces" variable. Every element of the list defines one interface. |
| 1248 |
# If an element has "interfaceid" field then Zabbix will update existing interface otherwise |
| 1249 |
# a new interface will be added. |
| 1250 |
found = False |
| 1251 |
for idx1, iface in enumerate(interfaces_copy): |
| 1252 |
diff_dict = {} |
| 1253 |
zabbix_utils.helper_cleanup_data(zabbix_utils.helper_compare_dictionaries(iface, interface, diff_dict)) |
| 1254 |
if diff_dict == {}: |
| 1255 |
found = True |
| 1256 |
found_in_interfaces.append(iface) |
| 1257 |
interfaces[idx1]["interfaceid"] = interfaceid |
| 1258 |
interfaces[idx1]["hostid"] = hostid |
| 1259 |
break |
| 1260 |
|
| 1261 |
if not found: |
| 1262 |
if not force: |
| 1263 |
interfaces.append(interface) |
| 1264 |
else: |
| 1265 |
# if force == True overwrite existing interfaces with provided interfaces with the same type |
| 1266 |
for idx1, iface in enumerate(interfaces_copy): |
| 1267 |
if interface["type"] == iface["type"] and iface not in found_in_interfaces: |
| 1268 |
found_in_interfaces.append(iface) |
| 1269 |
interfaces[idx1]["interfaceid"] = interfaceid |
| 1270 |
interfaces[idx1]["hostid"] = hostid |
| 1271 |
break |
| 1272 |
|
| 1273 |
if not force or link_templates is None: |
| 1274 |
template_ids = list(set(template_ids + host.get_host_templates_by_host_id(host_id))) |
| 1275 |
|
| 1276 |
if not force: |
| 1277 |
for group_id in host.get_group_ids_by_host_id(host_id): |
| 1278 |
if group_id not in group_ids: |
| 1279 |
group_ids.append(group_id) |
| 1280 |
|
| 1281 |
# Macros not present in host.update will be removed if we dont copy them when force=no |
| 1282 |
if macros is not None and "macros" in zabbix_host_obj.keys(): |
| 1283 |
existing_macros = zabbix_host_obj["macros"] |
| 1284 |
for macro in existing_macros: |
| 1285 |
macro.pop("hostmacroid", None) |
| 1286 |
macro.pop("hostid", None) |
| 1287 |
macro.pop("automatic", None) |
| 1288 |
found = False |
| 1289 |
for idx1, prov_macro in enumerate(macros): |
| 1290 |
diff_dict = {} |
| 1291 |
zabbix_utils.helper_compare_dictionaries(prov_macro, macro, diff_dict) |
| 1292 |
if diff_dict == {}: |
| 1293 |
found = True |
| 1294 |
break |
| 1295 |
if found: |
| 1296 |
macros[idx1] = macro |
| 1297 |
else: |
| 1298 |
macros.append(macro) |
| 1299 |
|
| 1300 |
# Tags not present in host.update will be removed if we dont copy them when force=no |
| 1301 |
if tags is not None and "tags" in zabbix_host_obj.keys(): |
| 1302 |
provided_tags = [t["tag"] for t in tags] |
| 1303 |
existing_tags = zabbix_host_obj["tags"] |
| 1304 |
for tag in existing_tags: |
| 1305 |
if tag["tag"] not in provided_tags: |
| 1306 |
tags.append(tag) |
| 1307 |
|
| 1308 |
# update host |
| 1309 |
if host.check_all_properties( |
| 1310 |
host_id, group_ids, status, interfaces, template_ids, exist_interfaces, zabbix_host_obj, proxy_id, |
| 1311 |
visible_name, description, host_name, inventory_mode, inventory_zabbix, tls_accept, tls_psk_identity, tls_psk, |
| 1312 |
tls_issuer, tls_subject, tls_connect, ipmi_authtype, ipmi_privilege, |
| 1313 |
ipmi_username, ipmi_password, macros, tags, monitored_by, proxy_group_id): |
| 1314 |
|
| 1315 |
host.update_host( |
| 1316 |
host_name, group_ids, status, host_id, interfaces, exist_interfaces, proxy_id, visible_name, |
| 1317 |
description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject, |
| 1318 |
ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags, discovered_host, zabbix_host_obj, |
| 1319 |
monitored_by, proxy_group_id) |
| 1320 |
|
| 1321 |
host.link_or_clear_template(host_id, template_ids) |
| 1322 |
|
| 1323 |
host.update_inventory_mode(host_id, inventory_mode) |
| 1324 |
host.update_inventory_zabbix(host_id, inventory_zabbix) |
| 1325 |
|
| 1326 |
module.exit_json(changed=True, |
| 1327 |
result="Successfully updated host %s (%s) and linked with template '%s'" |
| 1328 |
% (host_name, ip, link_templates)) |
| 1329 |
else: |
| 1330 |
module.exit_json(changed=False) |
| 1331 |
|
| 1332 |
else: |
| 1333 |
discovered_host = False |
| 1334 |
|
| 1335 |
if state == "absent": |
| 1336 |
# the host is already deleted. |
| 1337 |
module.exit_json(changed=False) |
| 1338 |
|
| 1339 |
if not group_ids: |
| 1340 |
module.fail_json(msg="Specify at least one group for creating host '%s'." % host_name) |
| 1341 |
|
| 1342 |
# create host |
| 1343 |
host_id = host.add_host( |
| 1344 |
host_name, group_ids, status, interfaces, proxy_id, visible_name, description, tls_connect, tls_accept, |
| 1345 |
tls_psk_identity, tls_psk, tls_issuer, tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, |
| 1346 |
ipmi_password, macros, tags, monitored_by, proxy_group_id) |
| 1347 |
|
| 1348 |
host.link_or_clear_template(host_id, template_ids) |
| 1349 |
|
| 1350 |
host.update_inventory_mode(host_id, inventory_mode) |
| 1351 |
host.update_inventory_zabbix(host_id, inventory_zabbix) |
| 1352 |
|
| 1353 |
module.exit_json(changed=True, result="Successfully added host %s (%s) and linked with template '%s'" % ( |
| 1354 |
host_name, ip, link_templates)) |
| 1355 |
|
| 1356 |
|
| 1357 |
if __name__ == "__main__": |
| 1358 |
main() |