| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# (c) 2017, Alen Komic |
| 4 |
# |
| 5 |
# This file is part of Ansible |
| 6 |
# |
| 7 |
# Ansible is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Ansible is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
|
| 21 |
from __future__ import (absolute_import, division, print_function) |
| 22 |
__metaclass__ = type |
| 23 |
|
| 24 |
|
| 25 |
DOCUMENTATION = r""" |
| 26 |
--- |
| 27 |
module: zabbix_proxy |
| 28 |
short_description: Create/delete/get/update Zabbix proxies |
| 29 |
description: |
| 30 |
- This module allows you to create, modify, get and delete Zabbix proxy entries. |
| 31 |
author: |
| 32 |
- "Alen Komic (@akomic)" |
| 33 |
requirements: |
| 34 |
- "python >= 3.9" |
| 35 |
options: |
| 36 |
proxy_name: |
| 37 |
description: |
| 38 |
- Name of the proxy in Zabbix. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
proxy_address: |
| 42 |
description: |
| 43 |
- Deprecated for Zabbix version >= 7.0. |
| 44 |
- Comma-delimited list of IP/CIDR addresses or DNS names to accept active proxy requests from. |
| 45 |
- Requires I(status=active). |
| 46 |
required: false |
| 47 |
type: str |
| 48 |
description: |
| 49 |
description: |
| 50 |
- Description of the proxy. |
| 51 |
required: false |
| 52 |
type: str |
| 53 |
status: |
| 54 |
description: |
| 55 |
- Deprecated for Zabbix version >= 7.0. |
| 56 |
- Type of proxy. (4 - active, 5 - passive) |
| 57 |
required: false |
| 58 |
choices: ["active", "passive"] |
| 59 |
default: "active" |
| 60 |
type: str |
| 61 |
tls_connect: |
| 62 |
description: |
| 63 |
- Connections to proxy. |
| 64 |
required: false |
| 65 |
choices: ["no_encryption","PSK","certificate"] |
| 66 |
default: "no_encryption" |
| 67 |
type: str |
| 68 |
tls_accept: |
| 69 |
description: |
| 70 |
- Connections from proxy. |
| 71 |
required: false |
| 72 |
choices: ["no_encryption","PSK","certificate"] |
| 73 |
default: "no_encryption" |
| 74 |
type: str |
| 75 |
ca_cert: |
| 76 |
description: |
| 77 |
- Certificate issuer. |
| 78 |
required: false |
| 79 |
aliases: [ tls_issuer ] |
| 80 |
type: str |
| 81 |
tls_subject: |
| 82 |
description: |
| 83 |
- Certificate subject. |
| 84 |
required: false |
| 85 |
type: str |
| 86 |
tls_psk_identity: |
| 87 |
description: |
| 88 |
- PSK identity. Required if either I(tls_connect) or I(tls_accept) has PSK enabled. |
| 89 |
required: false |
| 90 |
type: str |
| 91 |
tls_psk: |
| 92 |
description: |
| 93 |
- The preshared key, at least 32 hex digits. Required if either I(tls_connect) or I(tls_accept) has PSK enabled. |
| 94 |
required: false |
| 95 |
type: str |
| 96 |
state: |
| 97 |
description: |
| 98 |
- State of the proxy. |
| 99 |
- On C(present), it will create if proxy does not exist or update the proxy if the associated data is different. |
| 100 |
- On C(absent) will remove a proxy if it exists. |
| 101 |
required: false |
| 102 |
choices: ["present", "absent"] |
| 103 |
default: "present" |
| 104 |
type: str |
| 105 |
interface: |
| 106 |
description: |
| 107 |
- Deprecated for Zabbix version >= 7.0. |
| 108 |
- Dictionary with params for the interface when proxy is in passive mode. |
| 109 |
- For more information, review proxy interface documentation at |
| 110 |
- U(https://www.zabbix.com/documentation/current/en/manual/api/reference/proxy/object#proxy-interface). |
| 111 |
required: false |
| 112 |
suboptions: |
| 113 |
useip: |
| 114 |
type: int |
| 115 |
description: |
| 116 |
- Connect to proxy interface with IP address instead of DNS name. |
| 117 |
- 0 (don't use ip), 1 (use ip). |
| 118 |
default: 0 |
| 119 |
choices: [0, 1] |
| 120 |
ip: |
| 121 |
type: str |
| 122 |
description: |
| 123 |
- IP address used by proxy interface. |
| 124 |
- Required if I(useip=1). |
| 125 |
default: "" |
| 126 |
dns: |
| 127 |
type: str |
| 128 |
description: |
| 129 |
- DNS name of the proxy interface. |
| 130 |
- Required if I(useip=0). |
| 131 |
default: "" |
| 132 |
port: |
| 133 |
type: str |
| 134 |
description: |
| 135 |
- Port used by proxy interface. |
| 136 |
default: "10051" |
| 137 |
default: {} |
| 138 |
type: dict |
| 139 |
address: |
| 140 |
description: |
| 141 |
- Parameter introduced in Zabbix 7.0. |
| 142 |
- IP address or DNS name to connect to. |
| 143 |
- Required if the Zabbix proxy operating mode is passive |
| 144 |
required: false |
| 145 |
type: str |
| 146 |
port: |
| 147 |
description: |
| 148 |
- Parameter introduced in Zabbix 7.0. |
| 149 |
- Port number to connect to. |
| 150 |
- supported if the Zabbix proxy operating mode is passive. |
| 151 |
required: false |
| 152 |
type: str |
| 153 |
default: "10051" |
| 154 |
proxy_group: |
| 155 |
description: |
| 156 |
- Parameter introduced in Zabbix 7.0. |
| 157 |
- Proxy group name. |
| 158 |
required: false |
| 159 |
type: str |
| 160 |
local_address: |
| 161 |
description: |
| 162 |
- Parameter introduced in Zabbix 7.0. |
| 163 |
- Address for active agents. IP address or DNS name to connect to. |
| 164 |
- Required if proxy_groupid is not 0 |
| 165 |
required: false |
| 166 |
type: str |
| 167 |
local_port: |
| 168 |
description: |
| 169 |
- Parameter introduced in Zabbix 7.0. |
| 170 |
- Local proxy port number to connect to. |
| 171 |
- Supported if proxy_groupid is not 0 |
| 172 |
required: false |
| 173 |
type: str |
| 174 |
default: "10051" |
| 175 |
allowed_addresses: |
| 176 |
description: |
| 177 |
- Parameter introduced in Zabbix 7.0. |
| 178 |
- Comma-delimited IP addresses or DNS names of active Zabbix proxy. |
| 179 |
required: false |
| 180 |
type: str |
| 181 |
operating_mode: |
| 182 |
description: |
| 183 |
- Parameter introduced in Zabbix 7.0. |
| 184 |
- Type of proxy. |
| 185 |
required: false |
| 186 |
choices: ["active", "passive"] |
| 187 |
default: "active" |
| 188 |
type: str |
| 189 |
custom_timeouts: |
| 190 |
description: |
| 191 |
- Parameter introduced in Zabbix 7.0. |
| 192 |
- Whether to override global item timeouts on the proxy level. |
| 193 |
- 0 - use global settings; 1 - override timeouts. |
| 194 |
required: false |
| 195 |
type: int |
| 196 |
default: 0 |
| 197 |
choices: [0, 1] |
| 198 |
timeout_zabbix_agent: |
| 199 |
description: |
| 200 |
- Parameter introduced in Zabbix 7.0. |
| 201 |
- Spend no more than this number of seconds on Zabbix agent checks processing. |
| 202 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 203 |
- "Possible values range: 1-600s." |
| 204 |
- Rired if if C(custom_timeouts) is set to 1. |
| 205 |
required: false |
| 206 |
type: str |
| 207 |
timeout_simple_check: |
| 208 |
description: |
| 209 |
- Parameter introduced in Zabbix 7.0. |
| 210 |
- Spend no more than this number of seconds on simple checks processing. |
| 211 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 212 |
- "Possible values range: 1-600s." |
| 213 |
- Required if if C(custom_timeouts) is set to 1. |
| 214 |
required: false |
| 215 |
type: str |
| 216 |
timeout_snmp_agent: |
| 217 |
description: |
| 218 |
- Parameter introduced in Zabbix 7.0. |
| 219 |
- Spend no more than this number of seconds on SNMP agent checks processing. |
| 220 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 221 |
- "Possible values range: 1-600s." |
| 222 |
- Required if if C(custom_timeouts) is set to 1. |
| 223 |
required: false |
| 224 |
type: str |
| 225 |
timeout_external_check: |
| 226 |
description: |
| 227 |
- Parameter introduced in Zabbix 7.0. |
| 228 |
- Spend no more than this number of seconds on external checks processing. |
| 229 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 230 |
- "Possible values range: 1-600s." |
| 231 |
- Required if if C(custom_timeouts) is set to 1. |
| 232 |
required: false |
| 233 |
type: str |
| 234 |
timeout_db_monitor: |
| 235 |
description: |
| 236 |
- Parameter introduced in Zabbix 7.0. |
| 237 |
- Spend no more than this number of seconds on DB checks processing. |
| 238 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 239 |
- "Possible values range: 1-600s." |
| 240 |
- Required if if C(custom_timeouts) is set to 1. |
| 241 |
required: false |
| 242 |
type: str |
| 243 |
timeout_http_agent: |
| 244 |
description: |
| 245 |
- Parameter introduced in Zabbix 7.0. |
| 246 |
- Spend no more than this number of seconds on HTTPagent checks processing. |
| 247 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 248 |
- "Possible values range: 1-600s." |
| 249 |
- Required if if C(custom_timeouts) is set to 1. |
| 250 |
required: false |
| 251 |
type: str |
| 252 |
timeout_ssh_agent: |
| 253 |
description: |
| 254 |
- Parameter introduced in Zabbix 7.0. |
| 255 |
- Spend no more than this number of seconds on SSH checks processing. |
| 256 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 257 |
- "Possible values range: 1-600s." |
| 258 |
- Required if if C(custom_timeouts) is set to 1. |
| 259 |
required: false |
| 260 |
type: str |
| 261 |
timeout_telnet_agent: |
| 262 |
description: |
| 263 |
- Parameter introduced in Zabbix 7.0. |
| 264 |
- Spend no more than this number of seconds on Telnet checks processing. |
| 265 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 266 |
- "Possible values range: 1-600s." |
| 267 |
- Required if if C(custom_timeouts) is set to 1. |
| 268 |
required: false |
| 269 |
type: str |
| 270 |
timeout_script: |
| 271 |
description: |
| 272 |
- Parameter introduced in Zabbix 7.0. |
| 273 |
- Spend no more than this number of seconds on script type checks processing. |
| 274 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 275 |
- "Possible values range: 1-600s." |
| 276 |
- Required if if C(custom_timeouts) is set to 1. |
| 277 |
required: false |
| 278 |
type: str |
| 279 |
timeout_browser: |
| 280 |
description: |
| 281 |
- Parameter introduced in Zabbix 7.0. |
| 282 |
- Spend no more than this number of seconds on browser type checks processing. |
| 283 |
- Accepts seconds or time unit with suffix (e.g., 30s, 1m). |
| 284 |
- "Possible values range: 1-600s." |
| 285 |
- Required if if C(custom_timeouts) is set to 1. |
| 286 |
required: false |
| 287 |
type: str |
| 288 |
|
| 289 |
extends_documentation_fragment: |
| 290 |
- community.zabbix.zabbix |
| 291 |
|
| 292 |
""" |
| 293 |
|
| 294 |
EXAMPLES = r""" |
| 295 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 296 |
- name: Set credentials to access Zabbix Server API |
| 297 |
ansible.builtin.set_fact: |
| 298 |
ansible_user: Admin |
| 299 |
ansible_httpapi_pass: zabbix |
| 300 |
|
| 301 |
# If you want to use API token to be authenticated by Zabbix Server |
| 302 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 303 |
- name: Set API token |
| 304 |
ansible.builtin.set_fact: |
| 305 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 306 |
|
| 307 |
- name: Create or update a proxy with proxy type active (Zabbix version < 7.0) |
| 308 |
# set task level variables as we change ansible_connection plugin here |
| 309 |
vars: |
| 310 |
ansible_network_os: community.zabbix.zabbix |
| 311 |
ansible_connection: httpapi |
| 312 |
ansible_httpapi_port: 443 |
| 313 |
ansible_httpapi_use_ssl: true |
| 314 |
ansible_httpapi_validate_certs: false |
| 315 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 316 |
ansible_host: zabbix-example-fqdn.org |
| 317 |
community.zabbix.zabbix_proxy: |
| 318 |
proxy_name: ExampleProxy |
| 319 |
description: ExampleProxy |
| 320 |
status: active |
| 321 |
state: present |
| 322 |
proxy_address: ExampleProxy.local |
| 323 |
|
| 324 |
- name: Create a new passive proxy using only its IP (Zabbix version < 7.0) |
| 325 |
# set task level variables as we change ansible_connection plugin here |
| 326 |
vars: |
| 327 |
ansible_network_os: community.zabbix.zabbix |
| 328 |
ansible_connection: httpapi |
| 329 |
ansible_httpapi_port: 443 |
| 330 |
ansible_httpapi_use_ssl: true |
| 331 |
ansible_httpapi_validate_certs: false |
| 332 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 333 |
ansible_host: zabbix-example-fqdn.org |
| 334 |
community.zabbix.zabbix_proxy: |
| 335 |
proxy_name: ExampleProxy |
| 336 |
description: ExampleProxy |
| 337 |
status: passive |
| 338 |
state: present |
| 339 |
interface: |
| 340 |
useip: 1 |
| 341 |
ip: 10.1.1.2 |
| 342 |
port: 10051 |
| 343 |
|
| 344 |
- name: Create a new passive proxy using only its DNS (Zabbix version < 7.0) |
| 345 |
# set task level variables as we change ansible_connection plugin here |
| 346 |
vars: |
| 347 |
ansible_network_os: community.zabbix.zabbix |
| 348 |
ansible_connection: httpapi |
| 349 |
ansible_httpapi_port: 443 |
| 350 |
ansible_httpapi_use_ssl: true |
| 351 |
ansible_httpapi_validate_certs: false |
| 352 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 353 |
ansible_host: zabbix-example-fqdn.org |
| 354 |
community.zabbix.zabbix_proxy: |
| 355 |
proxy_name: ExampleProxy |
| 356 |
description: ExampleProxy |
| 357 |
status: passive |
| 358 |
state: present |
| 359 |
interface: |
| 360 |
dns: proxy.example.com |
| 361 |
port: 10051 |
| 362 |
|
| 363 |
- name: Create or update a proxy with proxy type active (Zabbix version >= 7.0) |
| 364 |
# set task level variables as we change ansible_connection plugin here |
| 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 |
ansible_host: zabbix-example-fqdn.org |
| 373 |
community.zabbix.zabbix_proxy: |
| 374 |
proxy_name: ExampleProxy |
| 375 |
description: ExampleProxy |
| 376 |
operating_mode: active |
| 377 |
state: present |
| 378 |
allowed_addresses: ExampleProxy.local |
| 379 |
|
| 380 |
- name: Create a new passive proxy using only its IP (Zabbix version >= 7.0) |
| 381 |
# set task level variables as we change ansible_connection plugin here |
| 382 |
vars: |
| 383 |
ansible_network_os: community.zabbix.zabbix |
| 384 |
ansible_connection: httpapi |
| 385 |
ansible_httpapi_port: 443 |
| 386 |
ansible_httpapi_use_ssl: true |
| 387 |
ansible_httpapi_validate_certs: false |
| 388 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 389 |
ansible_host: zabbix-example-fqdn.org |
| 390 |
community.zabbix.zabbix_proxy: |
| 391 |
proxy_name: ExampleProxy |
| 392 |
description: ExampleProxy |
| 393 |
operating_mode: passive |
| 394 |
state: present |
| 395 |
address: 10.1.1.2 |
| 396 |
port: 10051 |
| 397 |
|
| 398 |
- name: Create a new passive proxy using only its DNS (Zabbix version >= 7.0) |
| 399 |
# set task level variables as we change ansible_connection plugin here |
| 400 |
vars: |
| 401 |
ansible_network_os: community.zabbix.zabbix |
| 402 |
ansible_connection: httpapi |
| 403 |
ansible_httpapi_port: 443 |
| 404 |
ansible_httpapi_use_ssl: true |
| 405 |
ansible_httpapi_validate_certs: false |
| 406 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 407 |
ansible_host: zabbix-example-fqdn.org |
| 408 |
community.zabbix.zabbix_proxy: |
| 409 |
proxy_name: ExampleProxy |
| 410 |
description: ExampleProxy |
| 411 |
operating_mode: passive |
| 412 |
state: present |
| 413 |
address: proxy.example.com |
| 414 |
port: 10051 |
| 415 |
""" |
| 416 |
|
| 417 |
RETURN = r""" # """ |
| 418 |
|
| 419 |
|
| 420 |
from ansible.module_utils.basic import AnsibleModule |
| 421 |
|
| 422 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 423 |
|
| 424 |
from ansible.module_utils.compat.version import LooseVersion |
| 425 |
|
| 426 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 427 |
|
| 428 |
|
| 429 |
class Proxy(ZabbixBase): |
| 430 |
def __init__(self, module, zbx=None, zapi_wrapper=None): |
| 431 |
super(Proxy, self).__init__(module, zbx, zapi_wrapper) |
| 432 |
self.existing_data = None |
| 433 |
|
| 434 |
def proxy_exists(self, proxy_name): |
| 435 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 436 |
result = self._zapi.proxy.get({"output": "extend", |
| 437 |
"selectInterface": "extend", |
| 438 |
"filter": {"host": proxy_name}}) |
| 439 |
else: |
| 440 |
result = self._zapi.proxy.get({"output": "extend", |
| 441 |
"filter": {"name": proxy_name}}) |
| 442 |
|
| 443 |
if len(result) > 0 and "proxyid" in result[0]: |
| 444 |
self.existing_data = result[0] |
| 445 |
return result[0]["proxyid"] |
| 446 |
else: |
| 447 |
return result |
| 448 |
|
| 449 |
def add_proxy(self, data): |
| 450 |
try: |
| 451 |
if self._module.check_mode: |
| 452 |
self._module.exit_json(changed=True) |
| 453 |
|
| 454 |
parameters = {} |
| 455 |
for item in data: |
| 456 |
if data[item]: |
| 457 |
parameters[item] = data[item] |
| 458 |
|
| 459 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 460 |
if "proxy_address" in data and data["status"] != "5": |
| 461 |
parameters.pop("proxy_address", False) |
| 462 |
else: |
| 463 |
if "allowed_addresses" in data and data["operating_mode"] != "0": |
| 464 |
parameters.pop("allowed_addresses", False) |
| 465 |
|
| 466 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 467 |
if "interface" in data and data["status"] != "6": |
| 468 |
parameters.pop("interface", False) |
| 469 |
else: |
| 470 |
if "interface" in data and data["operating_mode"] != "1": |
| 471 |
parameters.pop("interface", False) |
| 472 |
|
| 473 |
if data["proxy_group"]: |
| 474 |
proxy_group = data["proxy_group"] |
| 475 |
result = self._zapi.proxygroup.get({"output": "extend", "filter": {"name": proxy_group}}) |
| 476 |
|
| 477 |
if len(result) == 0: |
| 478 |
self._module.fail_json(msg="Failed to find proxy group %s" % (proxy_group)) |
| 479 |
|
| 480 |
proxy_group_id = result[0]["proxy_groupid"] |
| 481 |
parameters.pop("proxy_group", False) |
| 482 |
parameters["proxy_groupid"] = proxy_group_id |
| 483 |
|
| 484 |
proxy_ids_list = self._zapi.proxy.create(parameters) |
| 485 |
|
| 486 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 487 |
self._module.exit_json(changed=True, |
| 488 |
result="Successfully added proxy %s (%s)" % (data["host"], data["status"])) |
| 489 |
else: |
| 490 |
self._module.exit_json(changed=True, |
| 491 |
result="Successfully added proxy %s (%s)" % (data["name"], data["operating_mode"])) |
| 492 |
if len(proxy_ids_list) >= 1: |
| 493 |
return proxy_ids_list["proxyids"][0] |
| 494 |
except Exception as e: |
| 495 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 496 |
self._module.fail_json(msg="Failed to create proxy %s: %s" % (data["host"], e)) |
| 497 |
else: |
| 498 |
self._module.fail_json(msg="Failed to create proxy %s: %s" % (data["name"], e)) |
| 499 |
|
| 500 |
def delete_proxy(self, proxy_id, proxy_name): |
| 501 |
try: |
| 502 |
if self._module.check_mode: |
| 503 |
self._module.exit_json(changed=True) |
| 504 |
self._zapi.proxy.delete([proxy_id]) |
| 505 |
self._module.exit_json(changed=True, result="Successfully deleted proxy %s" % proxy_name) |
| 506 |
except Exception as e: |
| 507 |
self._module.fail_json(msg="Failed to delete proxy %s: %s" % (proxy_name, str(e))) |
| 508 |
|
| 509 |
def update_proxy(self, proxy_id, data): |
| 510 |
try: |
| 511 |
if self._module.check_mode: |
| 512 |
self._module.exit_json(changed=True) |
| 513 |
|
| 514 |
parameters = {} |
| 515 |
for key in data: |
| 516 |
if data[key]: |
| 517 |
parameters[key] = data[key] |
| 518 |
|
| 519 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 520 |
status_or_operating_mode_name = "status" |
| 521 |
status_or_operating_mode_value = "5" |
| 522 |
else: |
| 523 |
status_or_operating_mode_name = "operating_mode" |
| 524 |
status_or_operating_mode_value = "0" |
| 525 |
if "custom_timeouts" in parameters: |
| 526 |
parameters["custom_timeouts"] = str(parameters["custom_timeouts"]) |
| 527 |
|
| 528 |
if data["proxy_group"]: |
| 529 |
proxy_group = data["proxy_group"] |
| 530 |
result = self._zapi.proxygroup.get({"output": "extend", "filter": {"name": proxy_group}}) |
| 531 |
|
| 532 |
if len(result) == 0: |
| 533 |
self._module.fail_json(msg="Failed to find proxy group %s" % (proxy_group)) |
| 534 |
|
| 535 |
proxy_group_id = result[0]["proxy_groupid"] |
| 536 |
parameters.pop("proxy_group", False) |
| 537 |
parameters["proxy_groupid"] = proxy_group_id |
| 538 |
|
| 539 |
if "interface" in parameters: |
| 540 |
if parameters[status_or_operating_mode_name] == status_or_operating_mode_value: |
| 541 |
# Active proxy |
| 542 |
parameters.pop("interface", False) |
| 543 |
else: |
| 544 |
# Passive proxy |
| 545 |
parameters["interface"]["useip"] = str(parameters["interface"]["useip"]) |
| 546 |
|
| 547 |
if parameters[status_or_operating_mode_name] == status_or_operating_mode_value: |
| 548 |
# Active proxy |
| 549 |
parameters.pop("tls_connect", False) |
| 550 |
else: |
| 551 |
# Passive proxy |
| 552 |
parameters.pop("tls_accept", False) |
| 553 |
|
| 554 |
parameters["proxyid"] = proxy_id |
| 555 |
|
| 556 |
change_parameters = {} |
| 557 |
difference = zabbix_utils.helper_cleanup_data(zabbix_utils.helper_compare_dictionaries(parameters, self.existing_data, change_parameters)) |
| 558 |
|
| 559 |
if difference == {}: |
| 560 |
self._module.exit_json(changed=False) |
| 561 |
else: |
| 562 |
difference["proxyid"] = proxy_id |
| 563 |
self._zapi.proxy.update(parameters) |
| 564 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 565 |
self._module.exit_json( |
| 566 |
changed=True, |
| 567 |
result="Successfully updated proxy %s (%s)" % (data["host"], proxy_id) |
| 568 |
) |
| 569 |
else: |
| 570 |
self._module.exit_json( |
| 571 |
changed=True, |
| 572 |
result="Successfully updated proxy %s (%s)" % (data["name"], proxy_id) |
| 573 |
) |
| 574 |
except Exception as e: |
| 575 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 576 |
self._module.fail_json(msg="Failed to update proxy %s: %s" % |
| 577 |
(data["host"], e)) |
| 578 |
else: |
| 579 |
self._module.fail_json(msg="Failed to update proxy %s: %s" % |
| 580 |
(data["name"], e)) |
| 581 |
|
| 582 |
|
| 583 |
def main(): |
| 584 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 585 |
argument_spec.update(dict( |
| 586 |
proxy_name=dict(type="str", required=True), |
| 587 |
proxy_address=dict(type="str", required=False), |
| 588 |
status=dict(type="str", default="active", choices=["active", "passive"]), |
| 589 |
allowed_addresses=dict(type="str", required=False, default=None), |
| 590 |
operating_mode=dict(type="str", default="active", choices=["active", "passive"]), |
| 591 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 592 |
description=dict(type="str", required=False), |
| 593 |
tls_connect=dict(type="str", default="no_encryption", choices=["no_encryption", "PSK", "certificate"]), |
| 594 |
tls_accept=dict(type="str", default="no_encryption", choices=["no_encryption", "PSK", "certificate"]), |
| 595 |
ca_cert=dict(type="str", required=False, default=None, aliases=["tls_issuer"]), |
| 596 |
tls_subject=dict(type="str", required=False, default=None), |
| 597 |
tls_psk_identity=dict(type="str", required=False, default=None), |
| 598 |
tls_psk=dict(type="str", required=False, default=None, no_log=True), |
| 599 |
interface=dict( |
| 600 |
type="dict", |
| 601 |
required=False, |
| 602 |
default={}, |
| 603 |
options=dict( |
| 604 |
useip=dict(type="int", choices=[0, 1], default=0), |
| 605 |
ip=dict(type="str", default=""), |
| 606 |
dns=dict(type="str", default=""), |
| 607 |
port=dict(type="str", default="10051") |
| 608 |
), |
| 609 |
), |
| 610 |
address=dict(type="str", required=False, default=None), |
| 611 |
port=dict(type="str", required=False, default="10051"), |
| 612 |
proxy_group=dict(type="str", required=False, default=None), |
| 613 |
local_address=dict(type="str", required=False, default=None), |
| 614 |
local_port=dict(type="str", required=False, default="10051"), |
| 615 |
custom_timeouts=dict(type="int", required=False, default=0, choices=[0, 1]), |
| 616 |
timeout_zabbix_agent=dict(type="str", required=False, default=None), |
| 617 |
timeout_simple_check=dict(type="str", required=False, default=None), |
| 618 |
timeout_snmp_agent=dict(type="str", required=False, default=None), |
| 619 |
timeout_external_check=dict(type="str", required=False, default=None), |
| 620 |
timeout_db_monitor=dict(type="str", required=False, default=None), |
| 621 |
timeout_http_agent=dict(type="str", required=False, default=None), |
| 622 |
timeout_ssh_agent=dict(type="str", required=False, default=None), |
| 623 |
timeout_telnet_agent=dict(type="str", required=False, default=None), |
| 624 |
timeout_script=dict(type="str", required=False, default=None), |
| 625 |
timeout_browser=dict(type="str", required=False, default=None) |
| 626 |
)) |
| 627 |
|
| 628 |
# Create temporary proxy object to be able to pull Zabbix version to resolve parameters dependencies |
| 629 |
module = AnsibleModule(argument_spec=argument_spec) |
| 630 |
proxy = Proxy(module) |
| 631 |
|
| 632 |
if LooseVersion(proxy._zbx_api_version) < LooseVersion("7.0"): |
| 633 |
required = [ |
| 634 |
["tls_connect", "PSK", ("tls_psk_identity", "tls_psk"), False], |
| 635 |
["tls_accept", "PSK", ("tls_psk_identity", "tls_psk"), False], |
| 636 |
["status", "passive", ("interface",)] |
| 637 |
] |
| 638 |
else: |
| 639 |
required = [ |
| 640 |
["operating_mode", "passive", ("address",)], |
| 641 |
["custom_timeouts", 1, ( |
| 642 |
"timeout_zabbix_agent", "timeout_simple_check", "timeout_snmp_agent", |
| 643 |
"timeout_external_check", "timeout_db_monitor", "timeout_http_agent", |
| 644 |
"timeout_ssh_agent", "timeout_telnet_agent", "timeout_script", |
| 645 |
"timeout_browser"), False] |
| 646 |
] |
| 647 |
|
| 648 |
module = AnsibleModule( |
| 649 |
argument_spec=argument_spec, |
| 650 |
required_if=required, |
| 651 |
supports_check_mode=True |
| 652 |
) |
| 653 |
|
| 654 |
proxy_name = module.params["proxy_name"] |
| 655 |
description = module.params["description"] |
| 656 |
tls_connect = module.params["tls_connect"] |
| 657 |
tls_accept = module.params["tls_accept"] |
| 658 |
tls_issuer = module.params["ca_cert"] |
| 659 |
tls_subject = module.params["tls_subject"] |
| 660 |
tls_psk_identity = module.params["tls_psk_identity"] |
| 661 |
tls_psk = module.params["tls_psk"] |
| 662 |
state = module.params["state"] |
| 663 |
|
| 664 |
proxy = Proxy(module) |
| 665 |
|
| 666 |
# convert enabled / disabled to integer |
| 667 |
if LooseVersion(proxy._zbx_api_version) < LooseVersion("7.0"): |
| 668 |
proxy_address = "" |
| 669 |
if "proxy_address" in module.params: |
| 670 |
proxy_address = module.params["proxy_address"] |
| 671 |
if "interface" in module.params: |
| 672 |
interface = module.params["interface"] |
| 673 |
status = 6 if module.params["status"] == "passive" else 5 |
| 674 |
else: |
| 675 |
allowed_addresses = module.params["allowed_addresses"] |
| 676 |
operating_mode = 1 if module.params["operating_mode"] == "passive" else 0 |
| 677 |
address = '127.0.0.1' if operating_mode == 0 else module.params["address"] |
| 678 |
port = module.params["port"] |
| 679 |
proxy_group = module.params["proxy_group"] |
| 680 |
local_address = module.params["local_address"] |
| 681 |
local_port = module.params["local_port"] |
| 682 |
allowed_addresses = module.params["allowed_addresses"] |
| 683 |
custom_timeouts = module.params["custom_timeouts"] |
| 684 |
timeout_zabbix_agent = module.params["timeout_zabbix_agent"] |
| 685 |
timeout_simple_check = module.params["timeout_simple_check"] |
| 686 |
timeout_snmp_agent = module.params["timeout_snmp_agent"] |
| 687 |
timeout_external_check = module.params["timeout_external_check"] |
| 688 |
timeout_db_monitor = module.params["timeout_db_monitor"] |
| 689 |
timeout_http_agent = module.params["timeout_http_agent"] |
| 690 |
timeout_ssh_agent = module.params["timeout_ssh_agent"] |
| 691 |
timeout_telnet_agent = module.params["timeout_telnet_agent"] |
| 692 |
timeout_script = module.params["timeout_script"] |
| 693 |
timeout_browser = module.params["timeout_browser"] |
| 694 |
if proxy_group: |
| 695 |
if local_address is None: |
| 696 |
module.fail_json(msg="local_address parameter is required when proxy_group is specified.") |
| 697 |
|
| 698 |
if tls_connect == "certificate": |
| 699 |
tls_connect = 4 |
| 700 |
elif tls_connect == "PSK": |
| 701 |
tls_connect = 2 |
| 702 |
else: |
| 703 |
tls_connect = 1 |
| 704 |
|
| 705 |
if tls_accept == "certificate": |
| 706 |
tls_accept = 4 |
| 707 |
elif tls_accept == "PSK": |
| 708 |
tls_accept = 2 |
| 709 |
else: |
| 710 |
tls_accept = 1 |
| 711 |
|
| 712 |
# check if proxy already exists |
| 713 |
proxy_id = proxy.proxy_exists(proxy_name) |
| 714 |
|
| 715 |
if proxy_id: |
| 716 |
if state == "absent": |
| 717 |
# remove proxy |
| 718 |
proxy.delete_proxy(proxy_id, proxy_name) |
| 719 |
else: |
| 720 |
if LooseVersion(proxy._zbx_api_version) < LooseVersion("7.0"): |
| 721 |
proxy.update_proxy(proxy_id, { |
| 722 |
"host": proxy_name, |
| 723 |
"description": description, |
| 724 |
"status": str(status), |
| 725 |
"tls_connect": str(tls_connect), |
| 726 |
"tls_accept": str(tls_accept), |
| 727 |
"tls_issuer": tls_issuer, |
| 728 |
"tls_subject": tls_subject, |
| 729 |
"tls_psk_identity": tls_psk_identity, |
| 730 |
"tls_psk": tls_psk, |
| 731 |
"interface": interface, |
| 732 |
"proxy_address": proxy_address |
| 733 |
}) |
| 734 |
else: |
| 735 |
proxy.update_proxy(proxy_id, { |
| 736 |
"name": proxy_name, |
| 737 |
"description": description, |
| 738 |
"operating_mode": str(operating_mode), |
| 739 |
"tls_connect": str(tls_connect), |
| 740 |
"tls_accept": str(tls_accept), |
| 741 |
"tls_issuer": tls_issuer, |
| 742 |
"tls_subject": tls_subject, |
| 743 |
"tls_psk_identity": tls_psk_identity, |
| 744 |
"tls_psk": tls_psk, |
| 745 |
"allowed_addresses": allowed_addresses, |
| 746 |
"address": address, |
| 747 |
"port": port, |
| 748 |
"proxy_group": proxy_group, |
| 749 |
"local_address": local_address, |
| 750 |
"local_port": local_port, |
| 751 |
"custom_timeouts": custom_timeouts, |
| 752 |
"timeout_zabbix_agent": timeout_zabbix_agent, |
| 753 |
"timeout_simple_check": timeout_simple_check, |
| 754 |
"timeout_snmp_agent": timeout_snmp_agent, |
| 755 |
"timeout_external_check": timeout_external_check, |
| 756 |
"timeout_db_monitor": timeout_db_monitor, |
| 757 |
"timeout_http_agent": timeout_http_agent, |
| 758 |
"timeout_ssh_agent": timeout_ssh_agent, |
| 759 |
"timeout_telnet_agent": timeout_telnet_agent, |
| 760 |
"timeout_script": timeout_script, |
| 761 |
"timeout_browser": timeout_browser |
| 762 |
}) |
| 763 |
else: |
| 764 |
if state == "absent": |
| 765 |
# the proxy is already deleted. |
| 766 |
module.exit_json(changed=False) |
| 767 |
|
| 768 |
if LooseVersion(proxy._zbx_api_version) < LooseVersion("7.0"): |
| 769 |
proxy_id = proxy.add_proxy(data={ |
| 770 |
"host": proxy_name, |
| 771 |
"description": description, |
| 772 |
"status": str(status), |
| 773 |
"tls_connect": str(tls_connect), |
| 774 |
"tls_accept": str(tls_accept), |
| 775 |
"tls_issuer": tls_issuer, |
| 776 |
"tls_subject": tls_subject, |
| 777 |
"tls_psk_identity": tls_psk_identity, |
| 778 |
"tls_psk": tls_psk, |
| 779 |
"interface": interface, |
| 780 |
"proxy_address": proxy_address |
| 781 |
}) |
| 782 |
else: |
| 783 |
proxy_id = proxy.add_proxy(data={ |
| 784 |
"name": proxy_name, |
| 785 |
"description": description, |
| 786 |
"operating_mode": str(operating_mode), |
| 787 |
"tls_connect": str(tls_connect), |
| 788 |
"tls_accept": str(tls_accept), |
| 789 |
"tls_issuer": tls_issuer, |
| 790 |
"tls_subject": tls_subject, |
| 791 |
"tls_psk_identity": tls_psk_identity, |
| 792 |
"tls_psk": tls_psk, |
| 793 |
"allowed_addresses": allowed_addresses, |
| 794 |
"address": address, |
| 795 |
"port": port, |
| 796 |
"proxy_group": proxy_group, |
| 797 |
"local_address": local_address, |
| 798 |
"local_port": local_port, |
| 799 |
"custom_timeouts": custom_timeouts, |
| 800 |
"timeout_zabbix_agent": timeout_zabbix_agent, |
| 801 |
"timeout_simple_check": timeout_simple_check, |
| 802 |
"timeout_snmp_agent": timeout_snmp_agent, |
| 803 |
"timeout_external_check": timeout_external_check, |
| 804 |
"timeout_db_monitor": timeout_db_monitor, |
| 805 |
"timeout_http_agent": timeout_http_agent, |
| 806 |
"timeout_ssh_agent": timeout_ssh_agent, |
| 807 |
"timeout_telnet_agent": timeout_telnet_agent, |
| 808 |
"timeout_script": timeout_script, |
| 809 |
"timeout_browser": timeout_browser |
| 810 |
}) |
| 811 |
|
| 812 |
|
| 813 |
if __name__ == "__main__": |
| 814 |
main() |