| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, sky-joker |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: zabbix_user |
| 14 |
short_description: Create/update/delete Zabbix users |
| 15 |
author: |
| 16 |
- sky-joker (@sky-joker) |
| 17 |
description: |
| 18 |
- This module allows you to create, modify and delete Zabbix users. |
| 19 |
requirements: |
| 20 |
- "python >= 3.9" |
| 21 |
options: |
| 22 |
username: |
| 23 |
description: |
| 24 |
- Username. |
| 25 |
- username is the unique identifier used and cannot be updated using this module. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the user. |
| 31 |
type: str |
| 32 |
surname: |
| 33 |
description: |
| 34 |
- Surname of the user. |
| 35 |
type: str |
| 36 |
usrgrps: |
| 37 |
description: |
| 38 |
- User groups to add the user to. |
| 39 |
- Required when I(state=present). |
| 40 |
required: false |
| 41 |
type: list |
| 42 |
elements: str |
| 43 |
passwd: |
| 44 |
description: |
| 45 |
- User's password. |
| 46 |
- Required unless all of the I(usrgrps) are set to use LDAP as frontend access. |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
override_passwd: |
| 50 |
description: |
| 51 |
- Override password for the user. |
| 52 |
- Password will not be updated on subsequent runs without setting this value to yes. |
| 53 |
default: false |
| 54 |
type: bool |
| 55 |
current_passwd: |
| 56 |
description: |
| 57 |
- Current password for the user when overriding its password. |
| 58 |
- Required when overriding the logged in user's password. |
| 59 |
- https://www.zabbix.com/documentation/6.4/en/manual/api/reference/user/update |
| 60 |
required: false |
| 61 |
type: str |
| 62 |
lang: |
| 63 |
description: |
| 64 |
- Language code of the user's language. |
| 65 |
choices: |
| 66 |
- "en_GB" |
| 67 |
- "en_US" |
| 68 |
- "zh_CN" |
| 69 |
- "cs_CZ" |
| 70 |
- "fr_FR" |
| 71 |
- "he_IL" |
| 72 |
- "it_IT" |
| 73 |
- "ko_KR" |
| 74 |
- "ja_JP" |
| 75 |
- "nb_NO" |
| 76 |
- "pl_PL" |
| 77 |
- "pt_BR" |
| 78 |
- "pt_PT" |
| 79 |
- "ru_RU" |
| 80 |
- "sk_SK" |
| 81 |
- "tr_TR" |
| 82 |
- "uk_UA" |
| 83 |
- "default" |
| 84 |
type: str |
| 85 |
theme: |
| 86 |
description: |
| 87 |
- User's theme. |
| 88 |
choices: |
| 89 |
- "default" |
| 90 |
- "blue-theme" |
| 91 |
- "dark-theme" |
| 92 |
type: str |
| 93 |
autologin: |
| 94 |
description: |
| 95 |
- Whether to enable auto-login. |
| 96 |
- If enable autologin, cannot enable autologout. |
| 97 |
type: bool |
| 98 |
autologout: |
| 99 |
description: |
| 100 |
- User session life time in seconds. If set to 0, the session will never expire. |
| 101 |
- If enable autologout, cannot enable autologin. |
| 102 |
type: str |
| 103 |
refresh: |
| 104 |
description: |
| 105 |
- Automatic refresh period in seconds. |
| 106 |
type: str |
| 107 |
rows_per_page: |
| 108 |
description: |
| 109 |
- Amount of object rows to show per page. |
| 110 |
type: str |
| 111 |
after_login_url: |
| 112 |
description: |
| 113 |
- URL of the page to redirect the user to after logging in. |
| 114 |
type: str |
| 115 |
user_medias: |
| 116 |
description: |
| 117 |
- Set the user's media. |
| 118 |
- If not set, makes no changes to media. |
| 119 |
suboptions: |
| 120 |
mediatype: |
| 121 |
description: |
| 122 |
- Media type name to set. |
| 123 |
default: "Email" |
| 124 |
type: str |
| 125 |
sendto: |
| 126 |
description: |
| 127 |
- Address, user name or other identifier of the recipient. |
| 128 |
- If C(mediatype) is Email, values are represented as array. For other types of Media types, value is represented as a string. |
| 129 |
required: true |
| 130 |
type: raw |
| 131 |
period: |
| 132 |
description: |
| 133 |
- Time when the notifications can be sent as a time period or user macros separated by a semicolon. |
| 134 |
- Please review the documentation for more information on the supported time period. |
| 135 |
- https://www.zabbix.com/documentation/current/en/manual/appendix/time_period |
| 136 |
default: "1-7,00:00-24:00" |
| 137 |
type: str |
| 138 |
severity: |
| 139 |
description: |
| 140 |
- Trigger severities to send notifications about. |
| 141 |
suboptions: |
| 142 |
not_classified: |
| 143 |
description: |
| 144 |
- severity not_classified enable/disable. |
| 145 |
default: true |
| 146 |
type: bool |
| 147 |
information: |
| 148 |
description: |
| 149 |
- severity information enable/disable. |
| 150 |
default: true |
| 151 |
type: bool |
| 152 |
warning: |
| 153 |
description: |
| 154 |
- severity warning enable/disable. |
| 155 |
default: true |
| 156 |
type: bool |
| 157 |
average: |
| 158 |
description: |
| 159 |
- severity average enable/disable. |
| 160 |
default: true |
| 161 |
type: bool |
| 162 |
high: |
| 163 |
description: |
| 164 |
- severity high enable/disable. |
| 165 |
default: true |
| 166 |
type: bool |
| 167 |
disaster: |
| 168 |
description: |
| 169 |
- severity disaster enable/disable. |
| 170 |
default: true |
| 171 |
type: bool |
| 172 |
default: |
| 173 |
not_classified: true |
| 174 |
information: true |
| 175 |
warning: true |
| 176 |
average: true |
| 177 |
high: true |
| 178 |
disaster: true |
| 179 |
type: dict |
| 180 |
active: |
| 181 |
description: |
| 182 |
- Whether the media is enabled. |
| 183 |
default: true |
| 184 |
type: bool |
| 185 |
type: list |
| 186 |
elements: dict |
| 187 |
timezone: |
| 188 |
description: |
| 189 |
- User's time zone. |
| 190 |
- For the full list of supported time zones please refer to U(https://www.php.net/manual/en/timezones.php) |
| 191 |
type: str |
| 192 |
version_added: 1.2.0 |
| 193 |
role_name: |
| 194 |
description: |
| 195 |
- User's role. |
| 196 |
- Default is C(User role) when creating a new user. |
| 197 |
- The default value will be removed at the version 2.0.0. |
| 198 |
type: str |
| 199 |
version_added: 1.2.0 |
| 200 |
state: |
| 201 |
description: |
| 202 |
- State of the user. |
| 203 |
- On C(present), it will create if user does not exist or update the user if the associated data is different. |
| 204 |
- On C(absent) will remove a user if it exists. |
| 205 |
default: "present" |
| 206 |
choices: ["present", "absent"] |
| 207 |
type: str |
| 208 |
extends_documentation_fragment: |
| 209 |
- community.zabbix.zabbix |
| 210 |
|
| 211 |
""" |
| 212 |
|
| 213 |
EXAMPLES = r""" |
| 214 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 215 |
- name: Set credentials to access Zabbix Server API |
| 216 |
ansible.builtin.set_fact: |
| 217 |
ansible_user: Admin |
| 218 |
ansible_httpapi_pass: zabbix |
| 219 |
|
| 220 |
# If you want to use API token to be authenticated by Zabbix Server |
| 221 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 222 |
- name: Set API token |
| 223 |
ansible.builtin.set_fact: |
| 224 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 225 |
|
| 226 |
- name: create a new zabbix user. |
| 227 |
# set task level variables as we change ansible_connection plugin here |
| 228 |
vars: |
| 229 |
ansible_network_os: community.zabbix.zabbix |
| 230 |
ansible_connection: httpapi |
| 231 |
ansible_httpapi_port: 443 |
| 232 |
ansible_httpapi_use_ssl: true |
| 233 |
ansible_httpapi_validate_certs: false |
| 234 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 235 |
ansible_host: zabbix-example-fqdn.org |
| 236 |
community.zabbix.zabbix_user: |
| 237 |
username: example |
| 238 |
name: user name |
| 239 |
surname: user surname |
| 240 |
usrgrps: |
| 241 |
- Guests |
| 242 |
- Disabled |
| 243 |
passwd: password |
| 244 |
lang: en_GB |
| 245 |
theme: blue-theme |
| 246 |
autologin: false |
| 247 |
autologout: "0" |
| 248 |
refresh: "30" |
| 249 |
rows_per_page: "200" |
| 250 |
after_login_url: "" |
| 251 |
user_medias: |
| 252 |
- mediatype: Email |
| 253 |
sendto: |
| 254 |
- [email protected] |
| 255 |
- [email protected] |
| 256 |
period: 1-7,00:00-24:00 |
| 257 |
severity: |
| 258 |
not_classified: false |
| 259 |
information: true |
| 260 |
warning: true |
| 261 |
average: true |
| 262 |
high: true |
| 263 |
disaster: true |
| 264 |
active: false |
| 265 |
state: present |
| 266 |
|
| 267 |
- name: delete existing zabbix user. |
| 268 |
# set task level variables as we change ansible_connection plugin here |
| 269 |
vars: |
| 270 |
ansible_network_os: community.zabbix.zabbix |
| 271 |
ansible_connection: httpapi |
| 272 |
ansible_httpapi_port: 443 |
| 273 |
ansible_httpapi_use_ssl: true |
| 274 |
ansible_httpapi_validate_certs: false |
| 275 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 276 |
ansible_host: zabbix-example-fqdn.org |
| 277 |
community.zabbix.zabbix_user: |
| 278 |
username: example |
| 279 |
state: absent |
| 280 |
""" |
| 281 |
|
| 282 |
RETURN = r""" |
| 283 |
user_ids: |
| 284 |
description: User id created or changed |
| 285 |
returned: success |
| 286 |
type: dict |
| 287 |
sample: { "userids": [ "5" ] } |
| 288 |
""" |
| 289 |
|
| 290 |
|
| 291 |
import copy |
| 292 |
|
| 293 |
from ansible.module_utils.basic import AnsibleModule |
| 294 |
|
| 295 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 296 |
from ansible_collections.community.zabbix.plugins.module_utils.helpers import ( |
| 297 |
helper_normalize_data, |
| 298 |
) |
| 299 |
from ansible.module_utils.compat.version import LooseVersion |
| 300 |
|
| 301 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 302 |
|
| 303 |
|
| 304 |
class User(ZabbixBase): |
| 305 |
def get_default_authentication(self): |
| 306 |
auth = self._zapi.authentication.get({"output": "extend"}) |
| 307 |
try: |
| 308 |
if auth["authentication_type"] == "0": |
| 309 |
return "internal" |
| 310 |
elif auth["authentication_type"] == "1": |
| 311 |
return "LDAP" |
| 312 |
else: |
| 313 |
self._module.fail_json( |
| 314 |
msg="Failed to query authentication type. Unknown authentication type %s" |
| 315 |
% auth |
| 316 |
) |
| 317 |
except Exception as e: |
| 318 |
self._module.fail_json( |
| 319 |
msg="Unhandled error while querying authentication type. %s" % (e) |
| 320 |
) |
| 321 |
|
| 322 |
def get_usergroups_by_name(self, usrgrps): |
| 323 |
params = { |
| 324 |
"output": ["usrgrpid", "name", "gui_access"], |
| 325 |
"filter": {"name": usrgrps}, |
| 326 |
} |
| 327 |
res = self._zapi.usergroup.get(params) |
| 328 |
if res: |
| 329 |
ids = [{"usrgrpid": g["usrgrpid"]} for g in res] |
| 330 |
# User can be created password-less only when all groups are of non-internal |
| 331 |
# authentication types |
| 332 |
# 0 = use system default authentication method |
| 333 |
# 1 = use internal authentication |
| 334 |
# 2 = use LDAP authentication |
| 335 |
# 3 = disable access to the frontend |
| 336 |
|
| 337 |
if bool([g for g in res if g["gui_access"] == "1"]): |
| 338 |
require_password = True |
| 339 |
elif bool( |
| 340 |
[g for g in res if g["gui_access"] == "2" or g["gui_access"] == "3"] |
| 341 |
): |
| 342 |
require_password = False |
| 343 |
elif bool([g for g in res if g["gui_access"] == "0"]): |
| 344 |
# Zabbix API for versions < 5.2 does not have a way to query the default auth type |
| 345 |
# so we must assume its set to internal |
| 346 |
default_authentication = self.get_default_authentication() |
| 347 |
require_password = ( |
| 348 |
True if default_authentication == "internal" else False |
| 349 |
) |
| 350 |
|
| 351 |
not_found_groups = set(usrgrps) - set([g["name"] for g in res]) |
| 352 |
if not_found_groups: |
| 353 |
self._module.fail_json( |
| 354 |
msg="User groups not found: %s" % not_found_groups |
| 355 |
) |
| 356 |
return ids, require_password |
| 357 |
else: |
| 358 |
self._module.fail_json(msg="No user groups found") |
| 359 |
|
| 360 |
def check_user_exist(self, username): |
| 361 |
zbx_user = self._zapi.user.get( |
| 362 |
{ |
| 363 |
"output": "extend", |
| 364 |
"filter": {"username": username}, |
| 365 |
"getAccess": True, |
| 366 |
"selectMedias": "extend", |
| 367 |
"selectUsrgrps": "extend", |
| 368 |
} |
| 369 |
) |
| 370 |
|
| 371 |
return zbx_user |
| 372 |
|
| 373 |
def convert_user_medias_parameter_types(self, user_medias): |
| 374 |
copy_user_medias = copy.deepcopy(user_medias) |
| 375 |
for user_media in copy_user_medias: |
| 376 |
media_types = self._zapi.mediatype.get({"output": "extend"}) |
| 377 |
for media_type in media_types: |
| 378 |
if media_type["name"] == user_media["mediatype"]: |
| 379 |
user_media["mediatypeid"] = media_type["mediatypeid"] |
| 380 |
user_media["mediatype"] = media_type["type"] |
| 381 |
break |
| 382 |
if "mediatypeid" not in user_media: |
| 383 |
self._module.fail_json( |
| 384 |
msg="Media type not found: %s" % user_media["mediatype"] |
| 385 |
) |
| 386 |
else: |
| 387 |
if user_media["mediatype"] == "0": # E-Mail |
| 388 |
# Because user media sendto parameter is raw in parameters specs perform explicit check on type |
| 389 |
if not ( |
| 390 |
isinstance(user_media["sendto"], list) |
| 391 |
or isinstance(user_media["sendto"], str) |
| 392 |
): |
| 393 |
self._module.fail_json( |
| 394 |
"For Email media type sendto parameter must be of type list or str." |
| 395 |
) |
| 396 |
if isinstance(user_media["sendto"], str): |
| 397 |
# sendto should be a list for Email media type |
| 398 |
user_media["sendto"] = [user_media["sendto"]] |
| 399 |
else: |
| 400 |
if not isinstance(user_media["sendto"], str): |
| 401 |
self._module.fail_json( |
| 402 |
"For any other than Email media type sendto parameter must be of type str." |
| 403 |
) |
| 404 |
del user_media["mediatype"] |
| 405 |
|
| 406 |
severity_binary_number = "" |
| 407 |
for severity_key in ( |
| 408 |
"disaster", |
| 409 |
"high", |
| 410 |
"average", |
| 411 |
"warning", |
| 412 |
"information", |
| 413 |
"not_classified", |
| 414 |
): |
| 415 |
if user_media["severity"][severity_key]: |
| 416 |
severity_binary_number = severity_binary_number + "1" |
| 417 |
else: |
| 418 |
severity_binary_number = severity_binary_number + "0" |
| 419 |
user_media["severity"] = str(int(severity_binary_number, 2)) |
| 420 |
|
| 421 |
if user_media["active"]: |
| 422 |
user_media["active"] = "0" |
| 423 |
else: |
| 424 |
user_media["active"] = "1" |
| 425 |
|
| 426 |
return copy_user_medias |
| 427 |
|
| 428 |
def get_roleid_by_name(self, role_name): |
| 429 |
roles = self._zapi.role.get({"output": "extend"}) |
| 430 |
for role in roles: |
| 431 |
if role["name"] == role_name: |
| 432 |
return role["roleid"] |
| 433 |
|
| 434 |
self._module.fail_json(msg="Role not found: %s" % role_name) |
| 435 |
|
| 436 |
def user_parameter_difference_check( |
| 437 |
self, |
| 438 |
zbx_user, |
| 439 |
username, |
| 440 |
name, |
| 441 |
surname, |
| 442 |
user_group_ids, |
| 443 |
passwd, |
| 444 |
lang, |
| 445 |
theme, |
| 446 |
autologin, |
| 447 |
autologout, |
| 448 |
refresh, |
| 449 |
rows_per_page, |
| 450 |
url, |
| 451 |
user_medias, |
| 452 |
timezone, |
| 453 |
role_name, |
| 454 |
override_passwd, |
| 455 |
): |
| 456 |
|
| 457 |
# existing data |
| 458 |
existing_data = copy.deepcopy(zbx_user[0]) |
| 459 |
usrgrpids = [] |
| 460 |
for usrgrp in existing_data["usrgrps"]: |
| 461 |
usrgrpids.append({"usrgrpid": usrgrp["usrgrpid"]}) |
| 462 |
existing_data["usrgrps"] = sorted(usrgrpids, key=lambda x: x["usrgrpid"]) |
| 463 |
existing_data["user_medias"] = existing_data["medias"] |
| 464 |
for del_key in [ |
| 465 |
"medias", |
| 466 |
"attempt_clock", |
| 467 |
"attempt_failed", |
| 468 |
"attempt_ip", |
| 469 |
"debug_mode", |
| 470 |
"users_status", |
| 471 |
"gui_access", |
| 472 |
]: |
| 473 |
del existing_data[del_key] |
| 474 |
|
| 475 |
if "user_medias" in existing_data and existing_data["user_medias"]: |
| 476 |
for user_media in existing_data["user_medias"]: |
| 477 |
for del_key in ["mediaid", "userid"]: |
| 478 |
if del_key in user_media: |
| 479 |
del user_media[del_key] |
| 480 |
|
| 481 |
# request data |
| 482 |
request_data = { |
| 483 |
"userid": zbx_user[0]["userid"], |
| 484 |
"username": username, |
| 485 |
"name": name, |
| 486 |
"surname": surname, |
| 487 |
"usrgrps": sorted(user_group_ids, key=lambda x: x["usrgrpid"]), |
| 488 |
"lang": lang, |
| 489 |
"theme": theme, |
| 490 |
"autologin": autologin, |
| 491 |
"autologout": autologout, |
| 492 |
"refresh": refresh, |
| 493 |
"rows_per_page": rows_per_page, |
| 494 |
"url": url, |
| 495 |
} |
| 496 |
|
| 497 |
if user_medias: |
| 498 |
request_data["user_medias"] = user_medias |
| 499 |
else: |
| 500 |
if "user_medias" in existing_data and existing_data["user_medias"]: |
| 501 |
del existing_data["user_medias"] |
| 502 |
|
| 503 |
if override_passwd: |
| 504 |
request_data["passwd"] = passwd |
| 505 |
|
| 506 |
request_data["roleid"] = ( |
| 507 |
self.get_roleid_by_name(role_name) if role_name else None |
| 508 |
) |
| 509 |
request_data["timezone"] = timezone |
| 510 |
|
| 511 |
request_data, del_keys = helper_normalize_data(request_data) |
| 512 |
existing_data, _del_keys = helper_normalize_data(existing_data, del_keys) |
| 513 |
|
| 514 |
user_parameter_difference_check_result = True |
| 515 |
diff_dict = {} |
| 516 |
if not zabbix_utils.helper_compare_dictionaries( |
| 517 |
request_data, existing_data, diff_dict |
| 518 |
): |
| 519 |
user_parameter_difference_check_result = False |
| 520 |
|
| 521 |
if LooseVersion(self._zbx_api_version) >= LooseVersion("7.0"): |
| 522 |
if user_medias: |
| 523 |
request_data["medias"] = user_medias |
| 524 |
del request_data["user_medias"] |
| 525 |
|
| 526 |
diff_params = {"before": existing_data, "after": request_data} |
| 527 |
|
| 528 |
return user_parameter_difference_check_result, diff_params |
| 529 |
|
| 530 |
def add_user( |
| 531 |
self, |
| 532 |
username, |
| 533 |
name, |
| 534 |
surname, |
| 535 |
user_group_ids, |
| 536 |
passwd, |
| 537 |
lang, |
| 538 |
theme, |
| 539 |
autologin, |
| 540 |
autologout, |
| 541 |
refresh, |
| 542 |
rows_per_page, |
| 543 |
url, |
| 544 |
user_medias, |
| 545 |
require_password, |
| 546 |
timezone, |
| 547 |
role_name, |
| 548 |
): |
| 549 |
|
| 550 |
if role_name is None: |
| 551 |
role_name = "User role" |
| 552 |
|
| 553 |
user_ids = {} |
| 554 |
|
| 555 |
request_data = { |
| 556 |
"username": username, |
| 557 |
"name": name, |
| 558 |
"surname": surname, |
| 559 |
"usrgrps": user_group_ids, |
| 560 |
"lang": lang, |
| 561 |
"theme": theme, |
| 562 |
"autologin": autologin, |
| 563 |
"autologout": autologout, |
| 564 |
"refresh": refresh, |
| 565 |
"rows_per_page": rows_per_page, |
| 566 |
"url": url, |
| 567 |
} |
| 568 |
if user_medias: |
| 569 |
if LooseVersion(self._zbx_api_version) <= LooseVersion("7.0"): |
| 570 |
request_data["user_medias"] = user_medias |
| 571 |
else: |
| 572 |
request_data["medias"] = user_medias |
| 573 |
|
| 574 |
if (require_password): |
| 575 |
request_data["passwd"] = passwd |
| 576 |
|
| 577 |
# The type key has changed to roleid key since Zabbix 5.2 |
| 578 |
request_data["roleid"] = self.get_roleid_by_name(role_name) |
| 579 |
request_data["timezone"] = timezone |
| 580 |
|
| 581 |
request_data, _del_keys = helper_normalize_data(request_data) |
| 582 |
|
| 583 |
diff_params = {} |
| 584 |
if not self._module.check_mode: |
| 585 |
try: |
| 586 |
user_ids = self._zapi.user.create(request_data) |
| 587 |
except Exception as e: |
| 588 |
self._module.fail_json( |
| 589 |
msg="Failed to create user %s: %s" % (username, e) |
| 590 |
) |
| 591 |
else: |
| 592 |
diff_params = {"before": "", "after": request_data} |
| 593 |
|
| 594 |
return user_ids, diff_params |
| 595 |
|
| 596 |
def update_user( |
| 597 |
self, |
| 598 |
zbx_user, |
| 599 |
username, |
| 600 |
name, |
| 601 |
surname, |
| 602 |
user_group_ids, |
| 603 |
passwd, |
| 604 |
lang, |
| 605 |
theme, |
| 606 |
autologin, |
| 607 |
autologout, |
| 608 |
refresh, |
| 609 |
rows_per_page, |
| 610 |
url, |
| 611 |
user_medias, |
| 612 |
timezone, |
| 613 |
role_name, |
| 614 |
override_passwd, |
| 615 |
current_passwd, |
| 616 |
): |
| 617 |
|
| 618 |
user_ids = {} |
| 619 |
|
| 620 |
request_data = { |
| 621 |
"userid": zbx_user[0]["userid"], |
| 622 |
"username": username, |
| 623 |
"name": name, |
| 624 |
"surname": surname, |
| 625 |
"usrgrps": user_group_ids, |
| 626 |
"lang": lang, |
| 627 |
"theme": theme, |
| 628 |
"autologin": autologin, |
| 629 |
"autologout": autologout, |
| 630 |
"refresh": refresh, |
| 631 |
"rows_per_page": rows_per_page, |
| 632 |
"url": url, |
| 633 |
} |
| 634 |
|
| 635 |
if override_passwd: |
| 636 |
request_data["passwd"] = passwd |
| 637 |
if current_passwd: |
| 638 |
request_data["current_passwd"] = current_passwd |
| 639 |
|
| 640 |
request_data["roleid"] = ( |
| 641 |
self.get_roleid_by_name(role_name) if role_name else None |
| 642 |
) |
| 643 |
request_data["timezone"] = timezone |
| 644 |
|
| 645 |
request_data, _del_keys = helper_normalize_data(request_data) |
| 646 |
|
| 647 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 648 |
try: |
| 649 |
if user_medias: |
| 650 |
request_data["user_medias"] = user_medias |
| 651 |
user_ids = self._zapi.user.update(request_data) |
| 652 |
except Exception as e: |
| 653 |
self._module.fail_json( |
| 654 |
msg="Failed to update user %s: %s" % (username, e) |
| 655 |
) |
| 656 |
|
| 657 |
if LooseVersion(self._zbx_api_version) >= LooseVersion("7.0"): |
| 658 |
try: |
| 659 |
if user_medias: |
| 660 |
request_data["medias"] = user_medias |
| 661 |
user_ids = self._zapi.user.update(request_data) |
| 662 |
except Exception as e: |
| 663 |
self._module.fail_json( |
| 664 |
msg="Failed to update user %s: %s" % (username, e) |
| 665 |
) |
| 666 |
|
| 667 |
return user_ids |
| 668 |
|
| 669 |
def delete_user(self, zbx_user, username): |
| 670 |
user_ids = {} |
| 671 |
diff_params = {} |
| 672 |
|
| 673 |
if not self._module.check_mode: |
| 674 |
try: |
| 675 |
user_ids = self._zapi.user.delete([zbx_user[0]["userid"]]) |
| 676 |
except Exception as e: |
| 677 |
self._module.fail_json( |
| 678 |
msg="Failed to delete user %s: %s" % (username, e) |
| 679 |
) |
| 680 |
else: |
| 681 |
diff_params = {"before": zbx_user[0], "after": ""} |
| 682 |
|
| 683 |
return user_ids, diff_params |
| 684 |
|
| 685 |
|
| 686 |
def main(): |
| 687 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 688 |
argument_spec.update( |
| 689 |
dict( |
| 690 |
username=dict(type="str", required=True), |
| 691 |
name=dict(type="str"), |
| 692 |
surname=dict(type="str"), |
| 693 |
usrgrps=dict(type="list", elements="str"), |
| 694 |
passwd=dict(type="str", required=False, no_log=True), |
| 695 |
override_passwd=dict( |
| 696 |
type="bool", required=False, default=False, no_log=False |
| 697 |
), |
| 698 |
current_passwd=dict(type="str", required=False, no_log=True), |
| 699 |
lang=dict( |
| 700 |
type="str", |
| 701 |
choices=[ |
| 702 |
"en_GB", |
| 703 |
"en_US", |
| 704 |
"zh_CN", |
| 705 |
"cs_CZ", |
| 706 |
"fr_FR", |
| 707 |
"he_IL", |
| 708 |
"it_IT", |
| 709 |
"ko_KR", |
| 710 |
"ja_JP", |
| 711 |
"nb_NO", |
| 712 |
"pl_PL", |
| 713 |
"pt_BR", |
| 714 |
"pt_PT", |
| 715 |
"ru_RU", |
| 716 |
"sk_SK", |
| 717 |
"tr_TR", |
| 718 |
"uk_UA", |
| 719 |
"default", |
| 720 |
], |
| 721 |
), |
| 722 |
theme=dict(type="str", choices=["default", "blue-theme", "dark-theme"]), |
| 723 |
autologin=dict(type="bool"), |
| 724 |
autologout=dict(type="str"), |
| 725 |
refresh=dict(type="str"), |
| 726 |
rows_per_page=dict(type="str"), |
| 727 |
after_login_url=dict(type="str"), |
| 728 |
user_medias=dict( |
| 729 |
type="list", |
| 730 |
elements="dict", |
| 731 |
options=dict( |
| 732 |
mediatype=dict(type="str", default="Email"), |
| 733 |
sendto=dict(type="raw", required=True), |
| 734 |
period=dict(type="str", default="1-7,00:00-24:00"), |
| 735 |
severity=dict( |
| 736 |
type="dict", |
| 737 |
options=dict( |
| 738 |
not_classified=dict(type="bool", default=True), |
| 739 |
information=dict(type="bool", default=True), |
| 740 |
warning=dict(type="bool", default=True), |
| 741 |
average=dict(type="bool", default=True), |
| 742 |
high=dict(type="bool", default=True), |
| 743 |
disaster=dict(type="bool", default=True), |
| 744 |
), |
| 745 |
default=dict( |
| 746 |
not_classified=True, |
| 747 |
information=True, |
| 748 |
warning=True, |
| 749 |
average=True, |
| 750 |
high=True, |
| 751 |
disaster=True, |
| 752 |
), |
| 753 |
), |
| 754 |
active=dict(type="bool", default=True), |
| 755 |
), |
| 756 |
), |
| 757 |
timezone=dict(type="str"), |
| 758 |
role_name=dict(type="str"), |
| 759 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 760 |
) |
| 761 |
) |
| 762 |
module = AnsibleModule( |
| 763 |
argument_spec=argument_spec, |
| 764 |
required_if=[["state", "present", ["usrgrps"]]], |
| 765 |
supports_check_mode=True, |
| 766 |
) |
| 767 |
|
| 768 |
username = module.params["username"] |
| 769 |
name = module.params["name"] |
| 770 |
surname = module.params["surname"] |
| 771 |
usrgrps = module.params["usrgrps"] |
| 772 |
passwd = module.params["passwd"] |
| 773 |
override_passwd = module.params["override_passwd"] |
| 774 |
current_passwd = module.params["current_passwd"] |
| 775 |
lang = module.params["lang"] |
| 776 |
theme = module.params["theme"] |
| 777 |
autologin = module.params["autologin"] |
| 778 |
autologout = module.params["autologout"] |
| 779 |
refresh = module.params["refresh"] |
| 780 |
rows_per_page = module.params["rows_per_page"] |
| 781 |
after_login_url = module.params["after_login_url"] |
| 782 |
user_medias = module.params["user_medias"] |
| 783 |
timezone = module.params["timezone"] |
| 784 |
role_name = module.params["role_name"] |
| 785 |
state = module.params["state"] |
| 786 |
|
| 787 |
if autologin is not None: |
| 788 |
if autologin: |
| 789 |
autologin = "1" |
| 790 |
else: |
| 791 |
autologin = "0" |
| 792 |
|
| 793 |
user = User(module) |
| 794 |
|
| 795 |
if user_medias: |
| 796 |
user_medias = user.convert_user_medias_parameter_types(user_medias) |
| 797 |
|
| 798 |
user_ids = {} |
| 799 |
zbx_user = user.check_user_exist(username) |
| 800 |
if state == "present": |
| 801 |
user_group_ids, require_password = user.get_usergroups_by_name(usrgrps) |
| 802 |
if (require_password): |
| 803 |
if passwd is None: |
| 804 |
module.fail_json( |
| 805 |
msg="User password is required. One or more groups are not LDAP based." |
| 806 |
) |
| 807 |
|
| 808 |
if zbx_user: |
| 809 |
diff_check_result, diff_params = user.user_parameter_difference_check( |
| 810 |
zbx_user, |
| 811 |
username, |
| 812 |
name, |
| 813 |
surname, |
| 814 |
user_group_ids, |
| 815 |
passwd, |
| 816 |
lang, |
| 817 |
theme, |
| 818 |
autologin, |
| 819 |
autologout, |
| 820 |
refresh, |
| 821 |
rows_per_page, |
| 822 |
after_login_url, |
| 823 |
user_medias, |
| 824 |
timezone, |
| 825 |
role_name, |
| 826 |
override_passwd, |
| 827 |
) |
| 828 |
|
| 829 |
if not module.check_mode and diff_check_result: |
| 830 |
user_ids = user.update_user( |
| 831 |
zbx_user, |
| 832 |
username, |
| 833 |
name, |
| 834 |
surname, |
| 835 |
user_group_ids, |
| 836 |
passwd, |
| 837 |
lang, |
| 838 |
theme, |
| 839 |
autologin, |
| 840 |
autologout, |
| 841 |
refresh, |
| 842 |
rows_per_page, |
| 843 |
after_login_url, |
| 844 |
user_medias, |
| 845 |
timezone, |
| 846 |
role_name, |
| 847 |
override_passwd, |
| 848 |
current_passwd, |
| 849 |
) |
| 850 |
else: |
| 851 |
diff_check_result = True |
| 852 |
user_ids, diff_params = user.add_user( |
| 853 |
username, |
| 854 |
name, |
| 855 |
surname, |
| 856 |
user_group_ids, |
| 857 |
passwd, |
| 858 |
lang, |
| 859 |
theme, |
| 860 |
autologin, |
| 861 |
autologout, |
| 862 |
refresh, |
| 863 |
rows_per_page, |
| 864 |
after_login_url, |
| 865 |
user_medias, |
| 866 |
require_password, |
| 867 |
timezone, |
| 868 |
role_name, |
| 869 |
) |
| 870 |
|
| 871 |
if state == "absent": |
| 872 |
if zbx_user: |
| 873 |
diff_check_result = True |
| 874 |
user_ids, diff_params = user.delete_user(zbx_user, username) |
| 875 |
else: |
| 876 |
diff_check_result = False |
| 877 |
diff_params = {} |
| 878 |
|
| 879 |
if not module.check_mode: |
| 880 |
if user_ids: |
| 881 |
module.exit_json(changed=True, user_ids=user_ids) |
| 882 |
else: |
| 883 |
module.exit_json(changed=False) |
| 884 |
else: |
| 885 |
if diff_check_result: |
| 886 |
module.exit_json(changed=True, diff=diff_params) |
| 887 |
else: |
| 888 |
module.exit_json(changed=False, diff=diff_params) |
| 889 |
|
| 890 |
|
| 891 |
if __name__ == "__main__": |
| 892 |
main() |