| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, mrvanes |
| 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 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: zabbix_user_role |
| 13 |
short_description: Adds or removes zabbix roles |
| 14 |
author: |
| 15 |
- Martin van Es (@mrvanes) |
| 16 |
description: |
| 17 |
- This module adds or removes zabbix roles |
| 18 |
requirements: |
| 19 |
- "python >= 3.9" |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- State of the user_role. |
| 24 |
- On C(present), it will create if user_role does not exist or update the user_role if the associated data is different. |
| 25 |
- On C(absent) will remove a user_role if it exists. |
| 26 |
default: "present" |
| 27 |
choices: ["present", "absent"] |
| 28 |
type: str |
| 29 |
required: false |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Name of the role to be processed |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
type: |
| 36 |
description: |
| 37 |
- User type. |
| 38 |
choices: ["User", "Admin", "Super Admin"] |
| 39 |
default: "User" |
| 40 |
type: str |
| 41 |
required: false |
| 42 |
rules: |
| 43 |
description: |
| 44 |
- Rules set as defined in https://www.zabbix.com/documentation/current/en/manual/api/reference/role/object#role-rules |
| 45 |
default: {} |
| 46 |
type: dict |
| 47 |
required: false |
| 48 |
extends_documentation_fragment: |
| 49 |
- community.zabbix.zabbix |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 54 |
- name: Set credentials to access Zabbix Server API |
| 55 |
ansible.builtin.set_fact: |
| 56 |
ansible_user: Admin |
| 57 |
ansible_httpapi_pass: zabbix |
| 58 |
|
| 59 |
# If you want to use API token to be authenticated by Zabbix Server |
| 60 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 61 |
- name: Set API token |
| 62 |
ansible.builtin.set_fact: |
| 63 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 64 |
|
| 65 |
# Create user role Operators with ui elements monitoring.hosts |
| 66 |
# disabled and monitoring.maps enabled |
| 67 |
|
| 68 |
- name: Create Zabbix user role |
| 69 |
# set task level variables as we change ansible_connection plugin here |
| 70 |
vars: |
| 71 |
ansible_network_os: community.zabbix.zabbix |
| 72 |
ansible_connection: httpapi |
| 73 |
ansible_httpapi_port: 443 |
| 74 |
ansible_httpapi_use_ssl: true |
| 75 |
ansible_httpapi_validate_certs: false |
| 76 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 77 |
ansible_host: zabbix-example-fqdn.org |
| 78 |
community.zabbix.zabbix_user_role: |
| 79 |
state: present |
| 80 |
name: Operators |
| 81 |
type: User |
| 82 |
rules: |
| 83 |
ui.default_access: 0 |
| 84 |
ui: |
| 85 |
- name: "monitoring.hosts" |
| 86 |
status: 0 |
| 87 |
- name: "monitoring.maps" |
| 88 |
status: 1 |
| 89 |
""" |
| 90 |
|
| 91 |
RETURN = r""" |
| 92 |
# Return values |
| 93 |
msg: |
| 94 |
description: The result of the action |
| 95 |
type: str |
| 96 |
returned: always |
| 97 |
sample: "No action" |
| 98 |
changed: |
| 99 |
description: The consequence of the action |
| 100 |
type: bool |
| 101 |
returned: always |
| 102 |
sample: false |
| 103 |
""" |
| 104 |
|
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 108 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 109 |
|
| 110 |
|
| 111 |
class UserRole(ZabbixBase): |
| 112 |
def __find_val(self, outval, inval): |
| 113 |
if outval == str(inval): |
| 114 |
return True |
| 115 |
return False |
| 116 |
|
| 117 |
def __find_list(self, outval, inval): |
| 118 |
if set(outval) == set(inval): |
| 119 |
return True |
| 120 |
return False |
| 121 |
|
| 122 |
def __find_dict(self, outval, inval): |
| 123 |
for out in outval: |
| 124 |
m = True |
| 125 |
for k, v in inval.items(): |
| 126 |
if out[k] == str(v): |
| 127 |
continue |
| 128 |
else: |
| 129 |
m = False |
| 130 |
if m: |
| 131 |
break |
| 132 |
return m |
| 133 |
|
| 134 |
def is_part_of(self, inp, out): |
| 135 |
verdict = True |
| 136 |
for rule, value in inp.items(): |
| 137 |
if not isinstance(value, list): |
| 138 |
verdict = verdict and self.__find_val(out.get(rule, ""), value) |
| 139 |
else: |
| 140 |
if len(value): |
| 141 |
if not isinstance(value[0], dict): |
| 142 |
verdict = verdict and self.__find_list(out.get(rule, []), value) |
| 143 |
else: |
| 144 |
for v in value: |
| 145 |
verdict = verdict and self.__find_dict(out.get(rule, {}), v) |
| 146 |
else: |
| 147 |
verdict = verdict and self.__find_list(rule, value) |
| 148 |
return verdict |
| 149 |
|
| 150 |
def get_user_role(self, name): |
| 151 |
result = self._zapi.role.get({ |
| 152 |
"output": "extend", |
| 153 |
"selectRules": "extend", |
| 154 |
"filter": {"name": name} |
| 155 |
}) |
| 156 |
return result |
| 157 |
|
| 158 |
|
| 159 |
def main(): |
| 160 |
msg = "No action" |
| 161 |
changed = False |
| 162 |
|
| 163 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 164 |
argument_spec.update(dict( |
| 165 |
state=dict(type="str", required=False, default="present", choices=["present", "absent"]), |
| 166 |
name=dict(type="str", required=True), |
| 167 |
type=dict(type="str", required=False, choices=["User", "Admin", "Super Admin"], default="User"), |
| 168 |
rules=dict(type="dict", required=False, default={}), |
| 169 |
)) |
| 170 |
|
| 171 |
# the AnsibleModule object |
| 172 |
module = AnsibleModule( |
| 173 |
argument_spec=argument_spec, |
| 174 |
supports_check_mode=False |
| 175 |
) |
| 176 |
|
| 177 |
state = module.params["state"] |
| 178 |
name = module.params["name"] |
| 179 |
type = zabbix_utils.helper_to_numeric_value( |
| 180 |
["", "user", "admin", "super admin"], module.params["type"].lower() |
| 181 |
) |
| 182 |
rules = module.params["rules"] |
| 183 |
|
| 184 |
user_role = UserRole(module) |
| 185 |
|
| 186 |
result = user_role.get_user_role(name) |
| 187 |
if result: |
| 188 |
if len(result) == 1: |
| 189 |
role = result[0] |
| 190 |
if role["readonly"] != 1: |
| 191 |
roleid = role["roleid"] |
| 192 |
if state == "absent": |
| 193 |
result = user_role._zapi.role.delete([f"{roleid}"]) |
| 194 |
changed = True |
| 195 |
msg = "Role deleted" |
| 196 |
else: |
| 197 |
if not user_role.is_part_of(rules, role["rules"]): |
| 198 |
result = user_role._zapi.role.update({"roleid": roleid, "rules": rules}) |
| 199 |
changed = True |
| 200 |
msg = "Role updated" |
| 201 |
else: |
| 202 |
module.fail_json(msg="Too many role matches") |
| 203 |
else: |
| 204 |
user_role._zapi.role.create({ |
| 205 |
"name": name, |
| 206 |
"type": type, |
| 207 |
"rules": rules |
| 208 |
}) |
| 209 |
changed = True |
| 210 |
msg = "Role created" |
| 211 |
|
| 212 |
module.exit_json(msg=msg, changed=changed) |
| 213 |
|
| 214 |
|
| 215 |
if __name__ == "__main__": |
| 216 |
main() |