| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2013-2014, Epic Games, Inc. |
| 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 = r""" |
| 12 |
--- |
| 13 |
module: zabbix_globalmacro |
| 14 |
short_description: Create/update/delete Zabbix Global macros |
| 15 |
version_added: 1.4.0 |
| 16 |
description: |
| 17 |
- manages Zabbix Global macros, it can create, update or delete them. |
| 18 |
- For macro_type Secret the value field cannot be validated and will always be overwritten due to the secret nature of the Text. |
| 19 |
author: |
| 20 |
- "Cove (@cove)" |
| 21 |
- Dean Hailin Song (!UNKNOWN) |
| 22 |
- Timothy Test (@ttestscripting) |
| 23 |
requirements: |
| 24 |
- "python >= 3.9" |
| 25 |
options: |
| 26 |
macro_name: |
| 27 |
description: |
| 28 |
- Name of the global macro in zabbix native format C({$MACRO}) or simple format C(MACRO). |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
macro_value: |
| 32 |
description: |
| 33 |
- Value of the global macro. |
| 34 |
- Required if I(state=present). |
| 35 |
type: str |
| 36 |
macro_type: |
| 37 |
description: |
| 38 |
- Type of the global macro Text or Secret Text. |
| 39 |
- Required if I(state=present). |
| 40 |
- text |
| 41 |
- secret - Secret Text Works only with Zabbix >= 5.0 and will default to Text in lower versions |
| 42 |
- vault - Vault Secret Works only with Zabbix >= 5.2 and will default to Text in lower versions |
| 43 |
type: str |
| 44 |
choices: [text, secret, vault] |
| 45 |
default: text |
| 46 |
macro_description: |
| 47 |
description: |
| 48 |
- Text Description of the global macro. |
| 49 |
type: str |
| 50 |
default: "" |
| 51 |
state: |
| 52 |
description: |
| 53 |
- State of the macro. |
| 54 |
- On C(present), it will create if macro does not exist or update the macro if the associated data is different. |
| 55 |
- On C(absent) will remove a macro if it exists. |
| 56 |
required: false |
| 57 |
choices: ["present", "absent"] |
| 58 |
type: str |
| 59 |
default: "present" |
| 60 |
force: |
| 61 |
description: |
| 62 |
- Only updates an existing macro if set to C(yes). |
| 63 |
default: "yes" |
| 64 |
type: bool |
| 65 |
|
| 66 |
notes: |
| 67 |
- This module returns changed=true when I(macro_type=secret). |
| 68 |
|
| 69 |
extends_documentation_fragment: |
| 70 |
- community.zabbix.zabbix |
| 71 |
""" |
| 72 |
|
| 73 |
EXAMPLES = r""" |
| 74 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 75 |
- name: Set credentials to access Zabbix Server API |
| 76 |
ansible.builtin.set_fact: |
| 77 |
ansible_user: Admin |
| 78 |
ansible_httpapi_pass: zabbix |
| 79 |
|
| 80 |
# If you want to use API token to be authenticated by Zabbix Server |
| 81 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 82 |
- name: Set API token |
| 83 |
ansible.builtin.set_fact: |
| 84 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 85 |
|
| 86 |
- name: Create new global macro or update an existing macro's value |
| 87 |
# set task level variables as we change ansible_connection plugin here |
| 88 |
vars: |
| 89 |
ansible_network_os: community.zabbix.zabbix |
| 90 |
ansible_connection: httpapi |
| 91 |
ansible_httpapi_port: 443 |
| 92 |
ansible_httpapi_use_ssl: true |
| 93 |
ansible_httpapi_validate_certs: false |
| 94 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 95 |
ansible_host: zabbix-example-fqdn.org |
| 96 |
community.zabbix.zabbix_globalmacro: |
| 97 |
macro_name: EXAMPLE.MACRO |
| 98 |
macro_value: Example value |
| 99 |
macro_type: 0 |
| 100 |
macro_description: Example description |
| 101 |
state: present |
| 102 |
# Values with curly brackets need to be quoted otherwise they will be interpreted as a dictionary |
| 103 |
- name: Create new global macro in Zabbix native format with Secret Type |
| 104 |
# set task level variables as we change ansible_connection plugin here |
| 105 |
vars: |
| 106 |
ansible_network_os: community.zabbix.zabbix |
| 107 |
ansible_connection: httpapi |
| 108 |
ansible_httpapi_port: 443 |
| 109 |
ansible_httpapi_use_ssl: true |
| 110 |
ansible_httpapi_validate_certs: false |
| 111 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 112 |
ansible_host: zabbix-example-fqdn.org |
| 113 |
community.zabbix.zabbix_globalmacro: |
| 114 |
macro_name: "{$EXAMPLE.MACRO}" |
| 115 |
macro_value: Example value |
| 116 |
macro_type: 1 |
| 117 |
macro_description: Example description |
| 118 |
state: present |
| 119 |
- name: Delete existing global macro |
| 120 |
community.zabbix.zabbix_globalmacro: |
| 121 |
macro_name: "{$EXAMPLE.MACRO}" |
| 122 |
state: absent |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = r""" |
| 126 |
""" |
| 127 |
|
| 128 |
|
| 129 |
from ansible.module_utils.basic import AnsibleModule |
| 130 |
|
| 131 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 132 |
|
| 133 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 134 |
|
| 135 |
|
| 136 |
class GlobalMacro(ZabbixBase): |
| 137 |
# get global macro |
| 138 |
def get_global_macro(self, macro_name): |
| 139 |
try: |
| 140 |
all_global_macro_list = self._zapi.usermacro.get({"globalmacro": "true"}) |
| 141 |
global_macro_list = [d for d in all_global_macro_list if d["macro"] == macro_name] |
| 142 |
if len(global_macro_list) > 0: |
| 143 |
return global_macro_list[0] |
| 144 |
return None |
| 145 |
except Exception as e: |
| 146 |
self._module.fail_json(msg="Failed to get global macro %s: %s" % (macro_name, e)) |
| 147 |
|
| 148 |
# create global macro |
| 149 |
def create_global_macro(self, macro_name, macro_value, macro_type, macro_description): |
| 150 |
try: |
| 151 |
if self._module.check_mode: |
| 152 |
self._module.exit_json(changed=True) |
| 153 |
self._zapi.usermacro.createglobal({"macro": macro_name, "value": macro_value, "type": macro_type, "description": macro_description}) |
| 154 |
self._module.exit_json(changed=True, result="Successfully added global macro %s" % macro_name) |
| 155 |
except Exception as e: |
| 156 |
self._module.fail_json(msg="Failed to create global macro %s: %s" % (macro_name, e)) |
| 157 |
|
| 158 |
# update global macro |
| 159 |
def update_global_macro(self, global_macro_obj, macro_name, macro_value, macro_type, macro_description): |
| 160 |
global_macro_id = global_macro_obj["globalmacroid"] |
| 161 |
try: |
| 162 |
if global_macro_obj["type"] == "0" or global_macro_obj["type"] == "2": |
| 163 |
if (global_macro_obj["macro"] == macro_name and global_macro_obj["value"] == macro_value |
| 164 |
and global_macro_obj["type"] == macro_type and global_macro_obj["description"] == macro_description): |
| 165 |
self._module.exit_json(changed=False, result="Global macro %s already up to date" % macro_name) |
| 166 |
if self._module.check_mode: |
| 167 |
self._module.exit_json(changed=True) |
| 168 |
self._zapi.usermacro.updateglobal({"globalmacroid": global_macro_id, "macro": macro_name, |
| 169 |
"value": macro_value, "type": macro_type, "description": macro_description}) |
| 170 |
self._module.exit_json(changed=True, result="Successfully updated global macro %s" % macro_name) |
| 171 |
except Exception as e: |
| 172 |
self._module.fail_json(msg="Failed to update global macro %s: %s" % (macro_name, e)) |
| 173 |
|
| 174 |
# delete global macro |
| 175 |
def delete_global_macro(self, global_macro_obj, macro_name): |
| 176 |
global_macro_id = global_macro_obj["globalmacroid"] |
| 177 |
try: |
| 178 |
if self._module.check_mode: |
| 179 |
self._module.exit_json(changed=True) |
| 180 |
self._zapi.usermacro.deleteglobal([global_macro_id]) |
| 181 |
self._module.exit_json(changed=True, result="Successfully deleted global macro %s" % macro_name) |
| 182 |
except Exception as e: |
| 183 |
self._module.fail_json(msg="Failed to delete global macro %s: %s" % (macro_name, e)) |
| 184 |
|
| 185 |
|
| 186 |
def normalize_macro_name(macro_name): |
| 187 |
# Zabbix handles macro names in upper case characters |
| 188 |
if ":" in macro_name: |
| 189 |
macro_name = ":".join([macro_name.split(":")[0].upper(), ":".join(macro_name.split(":")[1:])]) |
| 190 |
else: |
| 191 |
macro_name = macro_name.upper() |
| 192 |
|
| 193 |
# Valid format for macro is {$MACRO} |
| 194 |
if not macro_name.startswith("{$"): |
| 195 |
macro_name = "{$" + macro_name |
| 196 |
if not macro_name.endswith("}"): |
| 197 |
macro_name = macro_name + "}" |
| 198 |
|
| 199 |
return macro_name |
| 200 |
|
| 201 |
|
| 202 |
def main(): |
| 203 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 204 |
argument_spec.update(dict( |
| 205 |
macro_name=dict(type="str", required=True), |
| 206 |
macro_value=dict(type="str", required=False, no_log=True), |
| 207 |
macro_type=dict(type="str", default="text", choices=["text", "secret", "vault"]), |
| 208 |
macro_description=dict(type="str", default=""), |
| 209 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 210 |
force=dict(type="bool", default=True) |
| 211 |
)) |
| 212 |
module = AnsibleModule( |
| 213 |
argument_spec=argument_spec, |
| 214 |
required_if=[ |
| 215 |
["state", "present", ["macro_value"]] |
| 216 |
], |
| 217 |
supports_check_mode=True |
| 218 |
) |
| 219 |
|
| 220 |
macro_name = normalize_macro_name(module.params["macro_name"]) |
| 221 |
macro_value = module.params["macro_value"] |
| 222 |
macro_type = module.params["macro_type"] |
| 223 |
macro_value = module.params["macro_value"] |
| 224 |
macro_description = module.params["macro_description"] |
| 225 |
state = module.params["state"] |
| 226 |
force = module.params["force"] |
| 227 |
|
| 228 |
if macro_type == "text": |
| 229 |
macro_type = "0" |
| 230 |
elif macro_type == "secret": |
| 231 |
macro_type = "1" |
| 232 |
elif macro_type == "vault": |
| 233 |
macro_type = "2" |
| 234 |
|
| 235 |
global_macro_class_obj = GlobalMacro(module) |
| 236 |
|
| 237 |
if macro_name: |
| 238 |
global_macro_obj = global_macro_class_obj.get_global_macro(macro_name) |
| 239 |
|
| 240 |
if state == "absent": |
| 241 |
if not global_macro_obj: |
| 242 |
module.exit_json(changed=False, msg="Global Macro %s does not exist" % macro_name) |
| 243 |
else: |
| 244 |
# delete a macro |
| 245 |
global_macro_class_obj.delete_global_macro(global_macro_obj, macro_name) |
| 246 |
else: |
| 247 |
if not global_macro_obj: |
| 248 |
# create global macro |
| 249 |
global_macro_class_obj.create_global_macro(macro_name, macro_value, macro_type, macro_description) |
| 250 |
elif force: |
| 251 |
# update global macro |
| 252 |
global_macro_class_obj.update_global_macro(global_macro_obj, macro_name, macro_value, macro_type, macro_description) |
| 253 |
else: |
| 254 |
module.exit_json(changed=False, result="Global macro %s already exists and force is set to no" % macro_name) |
| 255 |
|
| 256 |
|
| 257 |
if __name__ == "__main__": |
| 258 |
main() |