| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, Tobias Birkefeld (@tcraxs) <[email protected]> |
| 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 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: zabbix_usergroup |
| 14 |
short_description: Create/delete/update Zabbix user groups |
| 15 |
description: |
| 16 |
- Create user groups if they do not exist. |
| 17 |
- Delete existing user groups if they exist and are empty. |
| 18 |
- Update existing user groups. |
| 19 |
author: |
| 20 |
- "Tobias Birkefeld (@tcraxs)" |
| 21 |
requirements: |
| 22 |
- "python >= 3.9" |
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the user group to create, update or delete. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
aliases: [ "user_group" ] |
| 30 |
gui_access: |
| 31 |
description: |
| 32 |
- Frontend authentication method of the users in the group. |
| 33 |
- "Possible values:" |
| 34 |
- default - use the system default authentication method; |
| 35 |
- internal - use internal authentication; |
| 36 |
- LDAP - use LDAP authentication; |
| 37 |
- disable - disable access to the frontend. |
| 38 |
required: false |
| 39 |
type: str |
| 40 |
default: "default" |
| 41 |
choices: [ "default", "internal", "LDAP", "disable"] |
| 42 |
debug_mode: |
| 43 |
description: |
| 44 |
- Whether debug mode is enabled or disabled. |
| 45 |
required: false |
| 46 |
type: str |
| 47 |
default: "disabled" |
| 48 |
choices: [ "disabled", "enabled" ] |
| 49 |
status: |
| 50 |
description: |
| 51 |
- Whether the user group is enabled or disabled. |
| 52 |
required: false |
| 53 |
type: str |
| 54 |
default: "enabled" |
| 55 |
choices: [ "enabled", "disabled" ] |
| 56 |
rights: |
| 57 |
description: |
| 58 |
- Permissions to assign to the group |
| 59 |
- For <= Zabbix 6.0 |
| 60 |
required: false |
| 61 |
type: list |
| 62 |
elements: dict |
| 63 |
suboptions: |
| 64 |
host_group: |
| 65 |
description: |
| 66 |
- Name of the host group to add permission to. |
| 67 |
required: true |
| 68 |
type: str |
| 69 |
permission: |
| 70 |
description: |
| 71 |
- Access level to the host group. |
| 72 |
required: true |
| 73 |
type: str |
| 74 |
choices: [ "denied", "read-only", "read-write" ] |
| 75 |
hostgroup_rights: |
| 76 |
description: |
| 77 |
- Host group permissions to assign to the user group |
| 78 |
- For => Zabbix 7.0 |
| 79 |
required: false |
| 80 |
type: list |
| 81 |
elements: dict |
| 82 |
suboptions: |
| 83 |
host_group: |
| 84 |
description: |
| 85 |
- Name of the host group to add permission to. |
| 86 |
required: true |
| 87 |
type: str |
| 88 |
permission: |
| 89 |
description: |
| 90 |
- Access level to the host group. |
| 91 |
required: true |
| 92 |
type: str |
| 93 |
choices: [ "denied", "read-only", "read-write" ] |
| 94 |
templategroup_rights: |
| 95 |
description: |
| 96 |
- Template group permissions to assign to the user group |
| 97 |
- For => Zabbix 7.0 |
| 98 |
required: false |
| 99 |
type: list |
| 100 |
elements: dict |
| 101 |
suboptions: |
| 102 |
template_group: |
| 103 |
description: |
| 104 |
- Name of the template group to add permission to. |
| 105 |
required: true |
| 106 |
type: str |
| 107 |
permission: |
| 108 |
description: |
| 109 |
- Access level to the templategroup. |
| 110 |
required: true |
| 111 |
type: str |
| 112 |
choices: [ "denied", "read-only", "read-write" ] |
| 113 |
tag_filters: |
| 114 |
description: |
| 115 |
- Tag based permissions to assign to the group |
| 116 |
required: false |
| 117 |
type: list |
| 118 |
elements: dict |
| 119 |
suboptions: |
| 120 |
host_group: |
| 121 |
description: |
| 122 |
- Name of the host group to add permission to. |
| 123 |
required: true |
| 124 |
type: str |
| 125 |
tag: |
| 126 |
description: |
| 127 |
- Tag name. |
| 128 |
required: false |
| 129 |
type: str |
| 130 |
default: "" |
| 131 |
value: |
| 132 |
description: |
| 133 |
- Tag value. |
| 134 |
required: false |
| 135 |
type: str |
| 136 |
default: "" |
| 137 |
userdirectory: |
| 138 |
description: |
| 139 |
- Authentication user directory when gui_access set to LDAP or System default. |
| 140 |
- For => Zabbix 7.0 |
| 141 |
required: false |
| 142 |
type: str |
| 143 |
state: |
| 144 |
description: |
| 145 |
- State of the user group. |
| 146 |
- On C(present), it will create if user group does not exist or update the user group if the associated data is different. |
| 147 |
- On C(absent) will remove a user group if it exists. |
| 148 |
required: false |
| 149 |
type: str |
| 150 |
default: "present" |
| 151 |
choices: [ "present", "absent" ] |
| 152 |
|
| 153 |
extends_documentation_fragment: |
| 154 |
- community.zabbix.zabbix |
| 155 |
""" |
| 156 |
|
| 157 |
EXAMPLES = r""" |
| 158 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 159 |
- name: Set credentials to access Zabbix Server API |
| 160 |
ansible.builtin.set_fact: |
| 161 |
ansible_user: Admin |
| 162 |
ansible_httpapi_pass: zabbix |
| 163 |
|
| 164 |
# If you want to use API token to be authenticated by Zabbix Server |
| 165 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 166 |
- name: Set API token |
| 167 |
ansible.builtin.set_fact: |
| 168 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 169 |
|
| 170 |
# Base create user group example |
| 171 |
- name: Create user group |
| 172 |
# set task level variables as we change ansible_connection plugin here |
| 173 |
vars: |
| 174 |
ansible_network_os: community.zabbix.zabbix |
| 175 |
ansible_connection: httpapi |
| 176 |
ansible_httpapi_port: 443 |
| 177 |
ansible_httpapi_use_ssl: true |
| 178 |
ansible_httpapi_validate_certs: false |
| 179 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 180 |
ansible_host: zabbix-example-fqdn.org |
| 181 |
community.zabbix.zabbix_usergroup: |
| 182 |
name: ACME |
| 183 |
userdirectory: LDAP infra 1 |
| 184 |
state: present |
| 185 |
|
| 186 |
# Base create user group with selected user directory for LDAP authentication |
| 187 |
- name: Create user group |
| 188 |
# set task level variables as we change ansible_connection plugin here |
| 189 |
vars: |
| 190 |
ansible_network_os: community.zabbix.zabbix |
| 191 |
ansible_connection: httpapi |
| 192 |
ansible_httpapi_port: 443 |
| 193 |
ansible_httpapi_use_ssl: true |
| 194 |
ansible_httpapi_validate_certs: false |
| 195 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 196 |
ansible_host: zabbix-example-fqdn.org |
| 197 |
community.zabbix.zabbix_usergroup: |
| 198 |
name: ACME |
| 199 |
userdirectory: LDAP infra 1 |
| 200 |
state: present |
| 201 |
|
| 202 |
# Base create user group with disabled gui access |
| 203 |
- name: Create user group with disabled gui access |
| 204 |
# set task level variables as we change ansible_connection plugin here |
| 205 |
vars: |
| 206 |
ansible_network_os: community.zabbix.zabbix |
| 207 |
ansible_connection: httpapi |
| 208 |
ansible_httpapi_port: 443 |
| 209 |
ansible_httpapi_use_ssl: true |
| 210 |
ansible_httpapi_validate_certs: false |
| 211 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 212 |
ansible_host: zabbix-example-fqdn.org |
| 213 |
community.zabbix.zabbix_usergroup: |
| 214 |
name: ACME |
| 215 |
gui_access: disable |
| 216 |
|
| 217 |
# Base create user group with permissions for Zabbix <= 6.0 |
| 218 |
- name: Create user group with permissions |
| 219 |
# set task level variables as we change ansible_connection plugin here |
| 220 |
vars: |
| 221 |
ansible_network_os: community.zabbix.zabbix |
| 222 |
ansible_connection: httpapi |
| 223 |
ansible_httpapi_port: 443 |
| 224 |
ansible_httpapi_use_ssl: true |
| 225 |
ansible_httpapi_validate_certs: false |
| 226 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 227 |
ansible_host: zabbix-example-fqdn.org |
| 228 |
community.zabbix.zabbix_usergroup: |
| 229 |
name: ACME |
| 230 |
rights: |
| 231 |
- host_group: Webserver |
| 232 |
permission: read-write |
| 233 |
- host_group: Databaseserver |
| 234 |
permission: read-only |
| 235 |
state: present |
| 236 |
|
| 237 |
# Base create user group with permissions for Zabbix => 7.0 |
| 238 |
- name: Create user group with permissions |
| 239 |
# set task level variables as we change ansible_connection plugin here |
| 240 |
vars: |
| 241 |
ansible_network_os: community.zabbix.zabbix |
| 242 |
ansible_connection: httpapi |
| 243 |
ansible_httpapi_port: 443 |
| 244 |
ansible_httpapi_use_ssl: true |
| 245 |
ansible_httpapi_validate_certs: false |
| 246 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 247 |
ansible_host: zabbix-example-fqdn.org |
| 248 |
community.zabbix.zabbix_usergroup: |
| 249 |
name: ACME |
| 250 |
hostgroup_rights: |
| 251 |
- host_group: Webserver |
| 252 |
permission: read-write |
| 253 |
- host_group: Databaseserver |
| 254 |
permission: read-only |
| 255 |
templategroup_rights: |
| 256 |
- template_group: Linux Templates |
| 257 |
permission: read-write |
| 258 |
- template_group: Templates |
| 259 |
permission: read-only |
| 260 |
state: present |
| 261 |
|
| 262 |
# Base create user group with tag permissions |
| 263 |
- name: Create user group with tag permissions |
| 264 |
# set task level variables as we change ansible_connection plugin here |
| 265 |
vars: |
| 266 |
ansible_network_os: community.zabbix.zabbix |
| 267 |
ansible_connection: httpapi |
| 268 |
ansible_httpapi_port: 443 |
| 269 |
ansible_httpapi_use_ssl: true |
| 270 |
ansible_httpapi_validate_certs: false |
| 271 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 272 |
ansible_host: zabbix-example-fqdn.org |
| 273 |
community.zabbix.zabbix_usergroup: |
| 274 |
name: ACME |
| 275 |
tag_filters: |
| 276 |
- host_group: Webserver |
| 277 |
tag: Application |
| 278 |
value: Java |
| 279 |
- host_group: Discovered hosts |
| 280 |
tag: Service |
| 281 |
value: JIRA |
| 282 |
state: present |
| 283 |
|
| 284 |
# Base delete user groups example |
| 285 |
- name: Delete user groups |
| 286 |
# set task level variables as we change ansible_connection plugin here |
| 287 |
vars: |
| 288 |
ansible_network_os: community.zabbix.zabbix |
| 289 |
ansible_connection: httpapi |
| 290 |
ansible_httpapi_port: 443 |
| 291 |
ansible_httpapi_use_ssl: true |
| 292 |
ansible_httpapi_validate_certs: false |
| 293 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 294 |
ansible_host: zabbix-example-fqdn.org |
| 295 |
community.zabbix.zabbix_usergroup: |
| 296 |
name: ACME |
| 297 |
state: absent |
| 298 |
""" |
| 299 |
|
| 300 |
RETURN = r""" |
| 301 |
state: |
| 302 |
description: User group state at the end of execution. |
| 303 |
returned: on success |
| 304 |
type: str |
| 305 |
sample: "present" |
| 306 |
usergroup: |
| 307 |
description: User group name. |
| 308 |
returned: on success |
| 309 |
type: str |
| 310 |
sample: "ACME" |
| 311 |
usrgrpid: |
| 312 |
description: User group id, if created, changed or deleted. |
| 313 |
returned: on success |
| 314 |
type: str |
| 315 |
sample: "42" |
| 316 |
msg: |
| 317 |
description: The result of the operation |
| 318 |
returned: always |
| 319 |
type: str |
| 320 |
sample: "User group created: ACME, ID: 42" |
| 321 |
""" |
| 322 |
|
| 323 |
from ansible.module_utils.basic import AnsibleModule |
| 324 |
|
| 325 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 326 |
from ansible.module_utils.compat.version import LooseVersion |
| 327 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 328 |
|
| 329 |
|
| 330 |
class Rights(ZabbixBase): |
| 331 |
""" |
| 332 |
Restructure the user defined rights to fit the Zabbix API requirements |
| 333 |
""" |
| 334 |
|
| 335 |
def get_hostgroup_by_hostgroup_name(self, name): |
| 336 |
"""Get host group by host group name. |
| 337 |
|
| 338 |
Parameters: |
| 339 |
name: Name of the host group. |
| 340 |
|
| 341 |
Returns: |
| 342 |
host group matching host group name. |
| 343 |
""" |
| 344 |
try: |
| 345 |
_hostgroup = self._zapi.hostgroup.get( |
| 346 |
{"output": "extend", "filter": {"name": [name]}} |
| 347 |
) |
| 348 |
if len(_hostgroup) < 1: |
| 349 |
self._module.fail_json(msg="Host group not found: %s" % name) |
| 350 |
else: |
| 351 |
return _hostgroup[0] |
| 352 |
except Exception as e: |
| 353 |
self._module.fail_json(msg="Failed to get host group '%s': %s" % (name, e)) |
| 354 |
|
| 355 |
def construct_the_data(self, _rights): |
| 356 |
"""Construct the user defined rights to fit the Zabbix API requirements |
| 357 |
|
| 358 |
Parameters: |
| 359 |
_rights: rights to construct |
| 360 |
|
| 361 |
Returns: |
| 362 |
dict: user defined rights |
| 363 |
""" |
| 364 |
if _rights is None: |
| 365 |
return [] |
| 366 |
constructed_data = [] |
| 367 |
for right in _rights: |
| 368 |
constructed_right = { |
| 369 |
"id": self.get_hostgroup_by_hostgroup_name(right.get("host_group"))[ |
| 370 |
"groupid" |
| 371 |
], |
| 372 |
"permission": zabbix_utils.helper_to_numeric_value( |
| 373 |
["denied", None, "read-only", "read-write"], right.get("permission") |
| 374 |
), |
| 375 |
} |
| 376 |
constructed_data.append(constructed_right) |
| 377 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 378 |
|
| 379 |
|
| 380 |
class HostgroupRights(ZabbixBase): |
| 381 |
""" |
| 382 |
Restructure the user defined host group rights to fit the Zabbix API requirements |
| 383 |
""" |
| 384 |
|
| 385 |
def get_hostgroup_by_hostgroup_name(self, name): |
| 386 |
"""Get host group by host group name. |
| 387 |
|
| 388 |
Parameters: |
| 389 |
name: Name of the host group. |
| 390 |
|
| 391 |
Returns: |
| 392 |
host group matching host group name. |
| 393 |
""" |
| 394 |
try: |
| 395 |
_hostgroup = self._zapi.hostgroup.get({ |
| 396 |
"output": "extend", |
| 397 |
"filter": {"name": [name]} |
| 398 |
}) |
| 399 |
if len(_hostgroup) < 1: |
| 400 |
self._module.fail_json(msg="Host group not found: %s" % name) |
| 401 |
else: |
| 402 |
return _hostgroup[0] |
| 403 |
except Exception as e: |
| 404 |
self._module.fail_json(msg="Failed to get host group '%s': %s" % (name, e)) |
| 405 |
|
| 406 |
def construct_the_data(self, _rights): |
| 407 |
"""Construct the user defined host group rights to fit the Zabbix API requirements |
| 408 |
|
| 409 |
Parameters: |
| 410 |
_rights: rights to construct |
| 411 |
|
| 412 |
Returns: |
| 413 |
dict: user defined rights |
| 414 |
""" |
| 415 |
if _rights is None: |
| 416 |
return [] |
| 417 |
constructed_data = [] |
| 418 |
for right in _rights: |
| 419 |
constructed_right = { |
| 420 |
"id": self.get_hostgroup_by_hostgroup_name(right.get("host_group"))["groupid"], |
| 421 |
"permission": zabbix_utils.helper_to_numeric_value([ |
| 422 |
"denied", |
| 423 |
None, |
| 424 |
"read-only", |
| 425 |
"read-write"], right.get("permission") |
| 426 |
) |
| 427 |
} |
| 428 |
constructed_data.append(constructed_right) |
| 429 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 430 |
|
| 431 |
|
| 432 |
class TemplategroupRights(ZabbixBase): |
| 433 |
""" |
| 434 |
Restructure the user defined template group rights to fit the Zabbix API requirements |
| 435 |
""" |
| 436 |
|
| 437 |
def get_templategroup_by_templategroup_name(self, name): |
| 438 |
"""Get template group by template group name. |
| 439 |
|
| 440 |
Parameters: |
| 441 |
name: Name of the template group. |
| 442 |
|
| 443 |
Returns: |
| 444 |
template group matching template group name. |
| 445 |
""" |
| 446 |
try: |
| 447 |
_templategroup = self._zapi.templategroup.get({ |
| 448 |
"output": "extend", |
| 449 |
"filter": {"name": [name]} |
| 450 |
}) |
| 451 |
if len(_templategroup) < 1: |
| 452 |
self._module.fail_json(msg="Template group not found: %s" % name) |
| 453 |
else: |
| 454 |
return _templategroup[0] |
| 455 |
except Exception as e: |
| 456 |
self._module.fail_json(msg="Failed to get template group '%s': %s" % (name, e)) |
| 457 |
|
| 458 |
def construct_the_data(self, _rights): |
| 459 |
"""Construct the user defined template rights to fit the Zabbix API requirements |
| 460 |
|
| 461 |
Parameters: |
| 462 |
_rights: rights to construct |
| 463 |
|
| 464 |
Returns: |
| 465 |
dict: user defined rights |
| 466 |
""" |
| 467 |
if _rights is None: |
| 468 |
return [] |
| 469 |
constructed_data = [] |
| 470 |
for right in _rights: |
| 471 |
constructed_right = { |
| 472 |
"id": self.get_templategroup_by_templategroup_name(right.get("template_group"))["groupid"], |
| 473 |
"permission": zabbix_utils.helper_to_numeric_value([ |
| 474 |
"denied", |
| 475 |
None, |
| 476 |
"read-only", |
| 477 |
"read-write"], right.get("permission") |
| 478 |
) |
| 479 |
} |
| 480 |
constructed_data.append(constructed_right) |
| 481 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 482 |
|
| 483 |
|
| 484 |
class TagFilters(Rights): |
| 485 |
""" |
| 486 |
Restructure the user defined tag_filters to fit the Zabbix API requirements |
| 487 |
""" |
| 488 |
|
| 489 |
def construct_the_data(self, _tag_filters): |
| 490 |
"""Construct the user defined tag filters to fit the Zabbix API requirements |
| 491 |
|
| 492 |
Parameters: |
| 493 |
_tag_filters: tag filters to construct |
| 494 |
|
| 495 |
Returns: |
| 496 |
dict: user defined tag filters |
| 497 |
""" |
| 498 |
if _tag_filters is None: |
| 499 |
return [] |
| 500 |
constructed_data = [] |
| 501 |
for tag_filter in _tag_filters: |
| 502 |
constructed_tag_filter = { |
| 503 |
"groupid": self.get_hostgroup_by_hostgroup_name( |
| 504 |
tag_filter.get("host_group") |
| 505 |
)["groupid"], |
| 506 |
"tag": tag_filter.get("tag"), |
| 507 |
"value": tag_filter.get("value"), |
| 508 |
} |
| 509 |
constructed_data.append(constructed_tag_filter) |
| 510 |
return zabbix_utils.helper_cleanup_data(constructed_data) |
| 511 |
|
| 512 |
|
| 513 |
class UserGroup(ZabbixBase): |
| 514 |
def _construct_parameters(self, **kwargs): |
| 515 |
"""Construct parameters of UserGroup object |
| 516 |
|
| 517 |
Parameters: |
| 518 |
**kwargs: Arbitrary keyword parameters. |
| 519 |
|
| 520 |
Returns: |
| 521 |
dict: dictionary of specified parameters |
| 522 |
""" |
| 523 |
_params = { |
| 524 |
"name": kwargs["name"], |
| 525 |
"gui_access": zabbix_utils.helper_to_numeric_value( |
| 526 |
["default", "internal", "LDAP", "disable"], kwargs["gui_access"] |
| 527 |
), |
| 528 |
"debug_mode": zabbix_utils.helper_to_numeric_value( |
| 529 |
["disabled", "enabled"], kwargs["debug_mode"] |
| 530 |
), |
| 531 |
"users_status": zabbix_utils.helper_to_numeric_value( |
| 532 |
["enabled", "disabled"], kwargs["status"] |
| 533 |
), |
| 534 |
"tag_filters": kwargs["tag_filters"], |
| 535 |
} |
| 536 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 537 |
_params["rights"] = kwargs["rights"] |
| 538 |
else: |
| 539 |
_params["hostgroup_rights"] = kwargs["hostgroup_rights"] |
| 540 |
_params["templategroup_rights"] = kwargs["templategroup_rights"] |
| 541 |
|
| 542 |
if kwargs["userdirectory"]: |
| 543 |
try: |
| 544 |
_userdir = self._zapi.userdirectory.get( |
| 545 |
{ |
| 546 |
"output": "extend", |
| 547 |
"search": {"name": [kwargs["userdirectory"]]}, |
| 548 |
} |
| 549 |
) |
| 550 |
except Exception as e: |
| 551 |
self._module.fail_json( |
| 552 |
msg="Failed to get user directory '%s': %s" |
| 553 |
% (kwargs["userdirectory"], e) |
| 554 |
) |
| 555 |
if len(_userdir) == 0: |
| 556 |
self._module.fail_json( |
| 557 |
msg="User directory '%s' not found" % kwargs["userdirectory"] |
| 558 |
) |
| 559 |
_params["userdirectoryid"] = _userdir[0]["userdirectoryid"] |
| 560 |
|
| 561 |
return _params |
| 562 |
|
| 563 |
def check_if_usergroup_exists(self, name): |
| 564 |
"""Check if user group exists. |
| 565 |
|
| 566 |
Parameters: |
| 567 |
name: Name of the user group. |
| 568 |
|
| 569 |
Returns: |
| 570 |
The return value. True for success, False otherwise. |
| 571 |
""" |
| 572 |
try: |
| 573 |
_usergroup = self._zapi.usergroup.get( |
| 574 |
{"output": "extend", "filter": {"name": [name]}} |
| 575 |
) |
| 576 |
if len(_usergroup) > 0: |
| 577 |
return _usergroup |
| 578 |
except Exception as e: |
| 579 |
self._module.fail_json( |
| 580 |
msg="Failed to check if user group '%s' exists: %s" % (name, e) |
| 581 |
) |
| 582 |
|
| 583 |
def get_usergroup_by_usergroup_name(self, name): |
| 584 |
"""Get user group by user group name. |
| 585 |
|
| 586 |
Parameters: |
| 587 |
name: Name of the user group. |
| 588 |
|
| 589 |
Returns: |
| 590 |
User group matching user group name. |
| 591 |
""" |
| 592 |
try: |
| 593 |
if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"): |
| 594 |
_usergroup = self._zapi.usergroup.get( |
| 595 |
{ |
| 596 |
"output": "extend", |
| 597 |
"selectTagFilters": "extend", |
| 598 |
"selectRights": "extend", |
| 599 |
"filter": {"name": [name]}, |
| 600 |
} |
| 601 |
) |
| 602 |
else: |
| 603 |
_usergroup = self._zapi.usergroup.get( |
| 604 |
{ |
| 605 |
"output": "extend", |
| 606 |
"selectTagFilters": "extend", |
| 607 |
"selectHostGroupRights": "extend", |
| 608 |
"selectTemplateGroupRights": "extend", |
| 609 |
"filter": {"name": [name]}, |
| 610 |
} |
| 611 |
) |
| 612 |
|
| 613 |
if len(_usergroup) < 1: |
| 614 |
self._module.fail_json(msg="User group not found: %s" % name) |
| 615 |
else: |
| 616 |
return _usergroup[0] |
| 617 |
except Exception as e: |
| 618 |
self._module.fail_json(msg="Failed to get user group '%s': %s" % (name, e)) |
| 619 |
|
| 620 |
def check_difference(self, **kwargs): |
| 621 |
"""Check difference between user group and user specified parameters. |
| 622 |
|
| 623 |
Parameters: |
| 624 |
**kwargs: Arbitrary keyword parameters. |
| 625 |
|
| 626 |
Returns: |
| 627 |
dict: dictionary of differences |
| 628 |
""" |
| 629 |
existing_usergroup = zabbix_utils.helper_convert_unicode_to_str( |
| 630 |
self.get_usergroup_by_usergroup_name(kwargs["name"]) |
| 631 |
) |
| 632 |
parameters = zabbix_utils.helper_convert_unicode_to_str( |
| 633 |
self._construct_parameters(**kwargs) |
| 634 |
) |
| 635 |
change_parameters = {} |
| 636 |
_diff = zabbix_utils.helper_compare_dictionaries( |
| 637 |
parameters, existing_usergroup, change_parameters |
| 638 |
) |
| 639 |
return _diff |
| 640 |
|
| 641 |
def update(self, **kwargs): |
| 642 |
"""Update user group. |
| 643 |
|
| 644 |
Parameters: |
| 645 |
**kwargs: Arbitrary keyword parameters. |
| 646 |
|
| 647 |
Returns: |
| 648 |
usergroup: updated user group |
| 649 |
""" |
| 650 |
try: |
| 651 |
if self._module.check_mode: |
| 652 |
self._module.exit_json(changed=True) |
| 653 |
return self._zapi.usergroup.update(kwargs) |
| 654 |
except Exception as e: |
| 655 |
self._module.fail_json( |
| 656 |
msg="Failed to update user group '%s': %s" % (kwargs["usrgrpid"], e) |
| 657 |
) |
| 658 |
|
| 659 |
def add(self, **kwargs): |
| 660 |
"""Add user group. |
| 661 |
|
| 662 |
Parameters: |
| 663 |
**kwargs: Arbitrary keyword parameters. |
| 664 |
|
| 665 |
Returns: |
| 666 |
usergroup: added user group |
| 667 |
""" |
| 668 |
try: |
| 669 |
if self._module.check_mode: |
| 670 |
self._module.exit_json(changed=True) |
| 671 |
parameters = self._construct_parameters(**kwargs) |
| 672 |
usergroup = self._zapi.usergroup.create(parameters) |
| 673 |
return usergroup["usrgrpids"][0] |
| 674 |
except Exception as e: |
| 675 |
self._module.fail_json( |
| 676 |
msg="Failed to create user group '%s': %s" % (kwargs["name"], e) |
| 677 |
) |
| 678 |
|
| 679 |
def delete(self, usrgrpid): |
| 680 |
"""Delete user group. |
| 681 |
|
| 682 |
Parameters: |
| 683 |
usrgrpid: User group id. |
| 684 |
|
| 685 |
Returns: |
| 686 |
usergroup: deleted user group |
| 687 |
""" |
| 688 |
try: |
| 689 |
if self._module.check_mode: |
| 690 |
self._module.exit_json(changed=True) |
| 691 |
else: |
| 692 |
return self._zapi.usergroup.delete([usrgrpid]) |
| 693 |
except Exception as e: |
| 694 |
self._module.fail_json( |
| 695 |
msg="Failed to delete user group '%s': %s" % (usrgrpid, e) |
| 696 |
) |
| 697 |
|
| 698 |
|
| 699 |
def main(): |
| 700 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 701 |
argument_spec.update( |
| 702 |
name=dict(type="str", required=True, aliases=["user_group"]), |
| 703 |
gui_access=dict( |
| 704 |
type="str", |
| 705 |
required=False, |
| 706 |
default="default", |
| 707 |
choices=["default", "internal", "LDAP", "disable"], |
| 708 |
), |
| 709 |
debug_mode=dict( |
| 710 |
type="str", |
| 711 |
required=False, |
| 712 |
default="disabled", |
| 713 |
choices=["disabled", "enabled"], |
| 714 |
), |
| 715 |
status=dict( |
| 716 |
type="str", |
| 717 |
required=False, |
| 718 |
default="enabled", |
| 719 |
choices=["enabled", "disabled"], |
| 720 |
), |
| 721 |
rights=dict( |
| 722 |
type="list", |
| 723 |
elements="dict", |
| 724 |
required=False, |
| 725 |
options=dict( |
| 726 |
host_group=dict(type="str", required=True), |
| 727 |
permission=dict( |
| 728 |
type="str", |
| 729 |
required=True, |
| 730 |
choices=["denied", "read-only", "read-write"], |
| 731 |
), |
| 732 |
), |
| 733 |
), |
| 734 |
hostgroup_rights=dict( |
| 735 |
type="list", |
| 736 |
elements="dict", |
| 737 |
required=False, |
| 738 |
options=dict( |
| 739 |
host_group=dict(type="str", required=True), |
| 740 |
permission=dict( |
| 741 |
type="str", |
| 742 |
required=True, |
| 743 |
choices=["denied", "read-only", "read-write"], |
| 744 |
), |
| 745 |
), |
| 746 |
), |
| 747 |
templategroup_rights=dict( |
| 748 |
type="list", |
| 749 |
elements="dict", |
| 750 |
required=False, |
| 751 |
options=dict( |
| 752 |
template_group=dict(type="str", required=True), |
| 753 |
permission=dict( |
| 754 |
type="str", |
| 755 |
required=True, |
| 756 |
choices=["denied", "read-only", "read-write"], |
| 757 |
), |
| 758 |
), |
| 759 |
), |
| 760 |
tag_filters=dict( |
| 761 |
type="list", |
| 762 |
elements="dict", |
| 763 |
required=False, |
| 764 |
options=dict( |
| 765 |
host_group=dict(type="str", required=True), |
| 766 |
tag=dict(type="str", default=""), |
| 767 |
value=dict(type="str", default=""), |
| 768 |
), |
| 769 |
), |
| 770 |
userdirectory=dict(type="str", required=False), |
| 771 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 772 |
) |
| 773 |
|
| 774 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 775 |
|
| 776 |
name = module.params["name"] |
| 777 |
gui_access = module.params["gui_access"] |
| 778 |
debug_mode = module.params["debug_mode"] |
| 779 |
status = module.params["status"] |
| 780 |
rights = module.params["rights"] |
| 781 |
hostgroup_rights = module.params["hostgroup_rights"] |
| 782 |
templategroup_rights = module.params["templategroup_rights"] |
| 783 |
tag_filters = module.params["tag_filters"] |
| 784 |
userdirectory = module.params["userdirectory"] |
| 785 |
state = module.params["state"] |
| 786 |
|
| 787 |
userGroup = UserGroup(module) |
| 788 |
zbx = userGroup._zapi |
| 789 |
if LooseVersion(userGroup._zbx_api_version) < LooseVersion("7.0"): |
| 790 |
rgts = Rights(module, zbx) |
| 791 |
else: |
| 792 |
hostgroup_rgts = HostgroupRights(module, zbx) |
| 793 |
templategroup_rgts = TemplategroupRights(module, zbx) |
| 794 |
tgflts = TagFilters(module, zbx) |
| 795 |
|
| 796 |
usergroup_exists = userGroup.check_if_usergroup_exists(name) |
| 797 |
|
| 798 |
if usergroup_exists: |
| 799 |
usrgrpid = userGroup.get_usergroup_by_usergroup_name(name)["usrgrpid"] |
| 800 |
if state == "absent": |
| 801 |
userGroup.delete(usrgrpid) |
| 802 |
module.exit_json( |
| 803 |
changed=True, |
| 804 |
state=state, |
| 805 |
usergroup=name, |
| 806 |
usrgrpid=usrgrpid, |
| 807 |
msg="User group deleted: %s, ID: %s" % (name, usrgrpid), |
| 808 |
) |
| 809 |
else: |
| 810 |
if LooseVersion(userGroup._zbx_api_version) < LooseVersion("7.0"): |
| 811 |
difference = userGroup.check_difference( |
| 812 |
usrgrpid=usrgrpid, |
| 813 |
name=name, |
| 814 |
gui_access=gui_access, |
| 815 |
debug_mode=debug_mode, |
| 816 |
status=status, |
| 817 |
rights=rgts.construct_the_data(rights), |
| 818 |
tag_filters=tgflts.construct_the_data(tag_filters), |
| 819 |
) |
| 820 |
else: |
| 821 |
difference = userGroup.check_difference( |
| 822 |
usrgrpid=usrgrpid, |
| 823 |
name=name, |
| 824 |
gui_access=gui_access, |
| 825 |
debug_mode=debug_mode, |
| 826 |
status=status, |
| 827 |
hostgroup_rights=hostgroup_rgts.construct_the_data( |
| 828 |
hostgroup_rights |
| 829 |
), |
| 830 |
templategroup_rights=templategroup_rgts.construct_the_data( |
| 831 |
templategroup_rights |
| 832 |
), |
| 833 |
tag_filters=tgflts.construct_the_data(tag_filters), |
| 834 |
userdirectory=userdirectory, |
| 835 |
) |
| 836 |
if difference == {}: |
| 837 |
module.exit_json( |
| 838 |
changed=False, |
| 839 |
state=state, |
| 840 |
usergroup=name, |
| 841 |
usrgrpid=usrgrpid, |
| 842 |
msg="User group is up to date: %s" % name, |
| 843 |
) |
| 844 |
else: |
| 845 |
userGroup.update(usrgrpid=usrgrpid, **difference) |
| 846 |
module.exit_json( |
| 847 |
changed=True, |
| 848 |
state=state, |
| 849 |
usergroup=name, |
| 850 |
usrgrpid=usrgrpid, |
| 851 |
msg="User group updated: %s, ID: %s" % (name, usrgrpid), |
| 852 |
) |
| 853 |
else: |
| 854 |
if state == "absent": |
| 855 |
module.exit_json( |
| 856 |
changed=False, |
| 857 |
state=state, |
| 858 |
usergroup=name, |
| 859 |
msg="User group %s does not exists, nothing to delete" % name, |
| 860 |
) |
| 861 |
else: |
| 862 |
if LooseVersion(userGroup._zbx_api_version) < LooseVersion("7.0"): |
| 863 |
usrgrpid = userGroup.add( |
| 864 |
name=name, |
| 865 |
gui_access=gui_access, |
| 866 |
debug_mode=debug_mode, |
| 867 |
status=status, |
| 868 |
rights=rgts.construct_the_data(rights), |
| 869 |
tag_filters=tgflts.construct_the_data(tag_filters), |
| 870 |
) |
| 871 |
else: |
| 872 |
usrgrpid = userGroup.add( |
| 873 |
name=name, |
| 874 |
gui_access=gui_access, |
| 875 |
debug_mode=debug_mode, |
| 876 |
status=status, |
| 877 |
hostgroup_rights=hostgroup_rgts.construct_the_data( |
| 878 |
hostgroup_rights |
| 879 |
), |
| 880 |
templategroup_rights=templategroup_rgts.construct_the_data( |
| 881 |
templategroup_rights |
| 882 |
), |
| 883 |
tag_filters=tgflts.construct_the_data(tag_filters), |
| 884 |
userdirectory=userdirectory, |
| 885 |
) |
| 886 |
module.exit_json( |
| 887 |
changed=True, |
| 888 |
state=state, |
| 889 |
usergroup=name, |
| 890 |
usrgrpid=usrgrpid, |
| 891 |
msg="User group created: %s, ID: %s" % (name, usrgrpid), |
| 892 |
) |
| 893 |
|
| 894 |
|
| 895 |
if __name__ == "__main__": |
| 896 |
main() |