| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright: (c) 2023, ONODERA Masaru <[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 |
|
| 12 |
DOCUMENTATION = """ |
| 13 |
--- |
| 14 |
module: zabbix_token |
| 15 |
|
| 16 |
short_description: Create/Update/Generate/Delete Zabbix token. |
| 17 |
|
| 18 |
description: |
| 19 |
- This module allows you to create, update, generate and delete Zabbix token. |
| 20 |
|
| 21 |
author: |
| 22 |
- ONODERA Masaru(@masa-orca) |
| 23 |
|
| 24 |
requirements: |
| 25 |
- "python >= 3.9" |
| 26 |
|
| 27 |
version_added: 2.1.0 |
| 28 |
|
| 29 |
options: |
| 30 |
name: |
| 31 |
description: |
| 32 |
- Name of the token. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
description: |
| 36 |
description: |
| 37 |
- Description of the token. |
| 38 |
required: false |
| 39 |
type: str |
| 40 |
username: |
| 41 |
description: |
| 42 |
- Name of user who is the token assinged to. |
| 43 |
required: true |
| 44 |
type: str |
| 45 |
status: |
| 46 |
description: |
| 47 |
- Status of the token. |
| 48 |
required: false |
| 49 |
type: bool |
| 50 |
expires_at: |
| 51 |
description: |
| 52 |
- A timestamp of the token will be expired. |
| 53 |
- The token will never expire if C(0) |
| 54 |
required: false |
| 55 |
type: int |
| 56 |
generate_token: |
| 57 |
description: |
| 58 |
- New token string will be generated if C(true). |
| 59 |
required: false |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
state: |
| 63 |
description: |
| 64 |
- Create or delete token. |
| 65 |
type: str |
| 66 |
default: present |
| 67 |
choices: |
| 68 |
- present |
| 69 |
- absent |
| 70 |
|
| 71 |
extends_documentation_fragment: |
| 72 |
- community.zabbix.zabbix |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = """ |
| 76 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 77 |
- name: Set credentials to access Zabbix Server API |
| 78 |
ansible.builtin.set_fact: |
| 79 |
ansible_user: Admin |
| 80 |
ansible_httpapi_pass: zabbix |
| 81 |
|
| 82 |
# If you want to use API token to be authenticated by Zabbix Server |
| 83 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 84 |
- name: Set API token |
| 85 |
ansible.builtin.set_fact: |
| 86 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 87 |
|
| 88 |
- name: Create Zabbix token and generate token string |
| 89 |
# set task level variables as we change ansible_connection plugin here |
| 90 |
vars: |
| 91 |
ansible_network_os: community.zabbix.zabbix |
| 92 |
ansible_connection: httpapi |
| 93 |
ansible_httpapi_port: 443 |
| 94 |
ansible_httpapi_use_ssl: true |
| 95 |
ansible_httpapi_validate_certs: false |
| 96 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 97 |
ansible_host: zabbix-example-fqdn.org |
| 98 |
community.zabbix.zabbix_token: |
| 99 |
name: test token |
| 100 |
description: Admin test token |
| 101 |
username: Admin |
| 102 |
status: true |
| 103 |
expires_at: 1700000000 |
| 104 |
generate_token: true |
| 105 |
state: present |
| 106 |
""" |
| 107 |
|
| 108 |
RETURN = """ |
| 109 |
msg: |
| 110 |
description: The result of the operation |
| 111 |
returned: success |
| 112 |
type: str |
| 113 |
sample: "Successfully created token" |
| 114 |
token: |
| 115 |
description: Generated token string |
| 116 |
returned: I(generate_token=true) |
| 117 |
type: str |
| 118 |
sample: "8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895" |
| 119 |
""" |
| 120 |
|
| 121 |
from ansible.module_utils.basic import AnsibleModule |
| 122 |
|
| 123 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 124 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 125 |
|
| 126 |
|
| 127 |
class Token(ZabbixBase): |
| 128 |
def get_userid_from_name(self, username): |
| 129 |
try: |
| 130 |
userids = self._zapi.user.get( |
| 131 |
{"output": "extend", "filter": {"username": username}} |
| 132 |
) |
| 133 |
if not userids or len(userids) > 1: |
| 134 |
self._module.fail_json("User '%s' cannot be found" % username) |
| 135 |
return userids[0]["userid"] |
| 136 |
except Exception as e: |
| 137 |
self._module.fail_json(msg="Failed to get userid: %s" % e) |
| 138 |
|
| 139 |
# get token |
| 140 |
def get_token(self, name, userid): |
| 141 |
try: |
| 142 |
return self._zapi.token.get( |
| 143 |
{"output": "extend", "userids": [userid], "filter": {"name": name}} |
| 144 |
) |
| 145 |
except Exception as e: |
| 146 |
self._module.fail_json(msg="Failed to get token: %s" % e) |
| 147 |
|
| 148 |
def create_token( |
| 149 |
self, name, description, userid, status, expires_at, generate_token |
| 150 |
): |
| 151 |
try: |
| 152 |
params = {} |
| 153 |
params["name"] = name |
| 154 |
if isinstance(description, str): |
| 155 |
params["description"] = description |
| 156 |
|
| 157 |
params["userid"] = userid |
| 158 |
|
| 159 |
if isinstance(status, bool): |
| 160 |
if status: |
| 161 |
params["status"] = "0" |
| 162 |
else: |
| 163 |
params["status"] = "1" |
| 164 |
|
| 165 |
if isinstance(expires_at, str): |
| 166 |
params["expires_at"] = str(expires_at) |
| 167 |
|
| 168 |
if self._module.check_mode: |
| 169 |
self._module.exit_json(changed=True) |
| 170 |
result = self._zapi.token.create(params) |
| 171 |
|
| 172 |
if generate_token: |
| 173 |
generated_tokens = self._zapi.token.generate(result["tokenids"]) |
| 174 |
self._module.exit_json( |
| 175 |
changed=True, |
| 176 |
msg="Successfully created token.", |
| 177 |
token=generated_tokens[0]["token"], |
| 178 |
) |
| 179 |
else: |
| 180 |
self._module.exit_json(changed=True, msg="Successfully created token.") |
| 181 |
|
| 182 |
except Exception as e: |
| 183 |
self._module.fail_json(msg="Failed to create token: %s" % e) |
| 184 |
|
| 185 |
def update_token( |
| 186 |
self, token, name, description, status, expires_at, generate_token |
| 187 |
): |
| 188 |
try: |
| 189 |
params = {} |
| 190 |
params["tokenid"] = token["tokenid"] |
| 191 |
params["name"] = name |
| 192 |
if isinstance(description, str) and description != token["description"]: |
| 193 |
params["description"] = description |
| 194 |
|
| 195 |
if isinstance(status, bool): |
| 196 |
if status: |
| 197 |
if token["status"] != "1": |
| 198 |
params["status"] = "0" |
| 199 |
else: |
| 200 |
if token["status"] != "0": |
| 201 |
params["status"] = "1" |
| 202 |
|
| 203 |
if isinstance(expires_at, int) and str(expires_at) != token["expires_at"]: |
| 204 |
params["expires_at"] = str(expires_at) |
| 205 |
|
| 206 |
# If params does not have any parameter except tokenid and name, no need to update. |
| 207 |
if len(params.keys()) == 2: |
| 208 |
if not generate_token: |
| 209 |
self._module.exit_json(changed=False) |
| 210 |
elif self._module.check_mode: |
| 211 |
self._module.exit_json(changed=True) |
| 212 |
else: |
| 213 |
if self._module.check_mode: |
| 214 |
self._module.exit_json(changed=True) |
| 215 |
self._zapi.token.update(params) |
| 216 |
|
| 217 |
if generate_token: |
| 218 |
generated_tokens = self._zapi.token.generate([token["tokenid"]]) |
| 219 |
self._module.exit_json( |
| 220 |
changed=True, |
| 221 |
msg="Successfully updated token.", |
| 222 |
token=generated_tokens[0]["token"], |
| 223 |
) |
| 224 |
else: |
| 225 |
self._module.exit_json(changed=True, msg="Successfully updated token.") |
| 226 |
|
| 227 |
except Exception as e: |
| 228 |
self._module.fail_json(msg="Failed to update token: %s" % e) |
| 229 |
|
| 230 |
# delete token |
| 231 |
def delete_token(self, token): |
| 232 |
try: |
| 233 |
tokenid = token["tokenid"] |
| 234 |
if self._module.check_mode: |
| 235 |
self._module.exit_json(changed=True) |
| 236 |
self._zapi.token.delete([tokenid]) |
| 237 |
self._module.exit_json(changed=True, msg="Successfully deleted token.") |
| 238 |
except Exception as e: |
| 239 |
self._module.fail_json(msg="Failed to delete token: %s" % e) |
| 240 |
|
| 241 |
|
| 242 |
def main(): |
| 243 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 244 |
argument_spec.update( |
| 245 |
dict( |
| 246 |
name=dict(type="str", required=True), |
| 247 |
description=dict(type="str"), |
| 248 |
username=dict(type="str", required=True), |
| 249 |
status=dict(type="bool"), |
| 250 |
expires_at=dict(type="int"), |
| 251 |
generate_token=dict(type="bool", default=False), |
| 252 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 253 |
) |
| 254 |
) |
| 255 |
module = AnsibleModule( |
| 256 |
argument_spec=argument_spec, |
| 257 |
supports_check_mode=True, |
| 258 |
) |
| 259 |
|
| 260 |
name = module.params["name"] |
| 261 |
description = module.params["description"] |
| 262 |
username = module.params["username"] |
| 263 |
status = module.params["status"] |
| 264 |
expires_at = module.params["expires_at"] |
| 265 |
generate_token = module.params["generate_token"] |
| 266 |
state = module.params["state"] |
| 267 |
|
| 268 |
token_class_obj = Token(module) |
| 269 |
userid = token_class_obj.get_userid_from_name(username) |
| 270 |
tokens = token_class_obj.get_token(name, userid) |
| 271 |
if state == "absent": |
| 272 |
if len(tokens) == 1: |
| 273 |
token_class_obj.delete_token(tokens[0]) |
| 274 |
else: |
| 275 |
module.exit_json(changed=False) |
| 276 |
else: |
| 277 |
if len(tokens) == 1: |
| 278 |
token_class_obj.update_token( |
| 279 |
tokens[0], name, description, status, expires_at, generate_token |
| 280 |
) |
| 281 |
else: |
| 282 |
token_class_obj.create_token( |
| 283 |
name, description, userid, status, expires_at, generate_token |
| 284 |
) |
| 285 |
|
| 286 |
|
| 287 |
if __name__ == "__main__": |
| 288 |
main() |