| 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 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
module: zabbix_user_info |
| 13 |
short_description: Gather information about Zabbix user |
| 14 |
author: |
| 15 |
- sky-joker (@sky-joker) |
| 16 |
description: |
| 17 |
- This module allows you to search for Zabbix user entries. |
| 18 |
requirements: |
| 19 |
- "python >= 3.9" |
| 20 |
options: |
| 21 |
username: |
| 22 |
description: |
| 23 |
- User name. |
| 24 |
- sername is the unique identifier used and cannot be updated using this module. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
extends_documentation_fragment: |
| 28 |
- community.zabbix.zabbix |
| 29 |
|
| 30 |
""" |
| 31 |
|
| 32 |
EXAMPLES = """ |
| 33 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 34 |
- name: Set credentials to access Zabbix Server API |
| 35 |
ansible.builtin.set_fact: |
| 36 |
ansible_user: Admin |
| 37 |
ansible_httpapi_pass: zabbix |
| 38 |
|
| 39 |
# If you want to use API token to be authenticated by Zabbix Server |
| 40 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 41 |
- name: Set API token |
| 42 |
ansible.builtin.set_fact: |
| 43 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 44 |
|
| 45 |
- name: Get zabbix user info |
| 46 |
# set task level variables as we change ansible_connection plugin here |
| 47 |
vars: |
| 48 |
ansible_network_os: community.zabbix.zabbix |
| 49 |
ansible_connection: httpapi |
| 50 |
ansible_httpapi_port: 443 |
| 51 |
ansible_httpapi_use_ssl: true |
| 52 |
ansible_httpapi_validate_certs: false |
| 53 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 54 |
ansible_host: zabbix-example-fqdn.org |
| 55 |
community.zabbix.zabbix_user_info: |
| 56 |
username: example |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = """ |
| 60 |
zabbix_user: |
| 61 |
description: example |
| 62 |
returned: always |
| 63 |
type: dict |
| 64 |
sample: { |
| 65 |
"username": "example", |
| 66 |
"attempt_clock": "0", |
| 67 |
"attempt_failed": "0", |
| 68 |
"attempt_ip": "", |
| 69 |
"autologin": "0", |
| 70 |
"autologout": "0", |
| 71 |
"debug_mode": "0", |
| 72 |
"gui_access": "0", |
| 73 |
"lang": "en_GB", |
| 74 |
"medias": [ |
| 75 |
{ |
| 76 |
"active": "0", |
| 77 |
"mediaid": "668", |
| 78 |
"mediatypeid": "1", |
| 79 |
"period": "1-7,00:00-24:00", |
| 80 |
"sendto": "[email protected]", |
| 81 |
"severity": "63", |
| 82 |
"userid": "660" |
| 83 |
} |
| 84 |
], |
| 85 |
"name": "user", |
| 86 |
"refresh": "30s", |
| 87 |
"rows_per_page": "50", |
| 88 |
"surname": "example", |
| 89 |
"theme": "default", |
| 90 |
"type": "1", |
| 91 |
"url": "", |
| 92 |
"userid": "660", |
| 93 |
"users_status": "0", |
| 94 |
"usrgrps": [ |
| 95 |
{ |
| 96 |
"debug_mode": "0", |
| 97 |
"gui_access": "0", |
| 98 |
"name": "Guests", |
| 99 |
"users_status": "0", |
| 100 |
"usrgrpid": "8" |
| 101 |
} |
| 102 |
] |
| 103 |
} |
| 104 |
""" |
| 105 |
|
| 106 |
|
| 107 |
from ansible.module_utils.basic import AnsibleModule |
| 108 |
|
| 109 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 110 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 111 |
|
| 112 |
|
| 113 |
class User(ZabbixBase): |
| 114 |
def get_user_by_user_username(self, username): |
| 115 |
zabbix_user = "" |
| 116 |
try: |
| 117 |
data = {"output": "extend", "filter": {}, |
| 118 |
"getAccess": True, "selectMedias": "extend", |
| 119 |
"selectUsrgrps": "extend"} |
| 120 |
data["filter"]["username"] = username |
| 121 |
|
| 122 |
zabbix_user = self._zapi.user.get(data) |
| 123 |
except Exception as e: |
| 124 |
self._zapi.logout() |
| 125 |
self._module.fail_json(msg="Failed to get user information: %s" % e) |
| 126 |
|
| 127 |
if not zabbix_user: |
| 128 |
zabbix_user = {} |
| 129 |
else: |
| 130 |
zabbix_user = zabbix_user[0] |
| 131 |
|
| 132 |
return zabbix_user |
| 133 |
|
| 134 |
|
| 135 |
def main(): |
| 136 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 137 |
argument_spec.update(dict( |
| 138 |
username=dict(type="str", required=True), |
| 139 |
)) |
| 140 |
module = AnsibleModule( |
| 141 |
argument_spec=argument_spec, |
| 142 |
supports_check_mode=True |
| 143 |
) |
| 144 |
|
| 145 |
username = module.params["username"] |
| 146 |
|
| 147 |
user = User(module) |
| 148 |
zabbix_user = user.get_user_by_user_username(username) |
| 149 |
module.exit_json(changed=False, zabbix_user=zabbix_user) |
| 150 |
|
| 151 |
|
| 152 |
if __name__ == "__main__": |
| 153 |
main() |