| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
--- |
| 13 |
module: zabbix_autoregister |
| 14 |
|
| 15 |
short_description: Update Zabbix autoregistration |
| 16 |
|
| 17 |
|
| 18 |
description: |
| 19 |
- This module allows you to modify Zabbix autoregistration. |
| 20 |
|
| 21 |
author: |
| 22 |
- ONODERA Masaru(@masa-orca) |
| 23 |
|
| 24 |
requirements: |
| 25 |
- "python >= 3.9" |
| 26 |
|
| 27 |
version_added: 1.6.0 |
| 28 |
|
| 29 |
options: |
| 30 |
tls_accept: |
| 31 |
description: |
| 32 |
- Type of allowed incoming connections for autoregistration. |
| 33 |
- Choose from C(unsecure), C(tls_with_psk) or both. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
required: true |
| 37 |
tls_psk_identity: |
| 38 |
description: |
| 39 |
- TLS connection uses this PSK identity string. |
| 40 |
- The PSK identity string will be transmitted unencrypted over the network. Therefore, you should not put any sensitive information here. |
| 41 |
- This setting requires I(tls_accept=tls_with_psk) if current value of I(tls_accept) is C(unsecure). |
| 42 |
type: str |
| 43 |
tls_psk: |
| 44 |
description: |
| 45 |
- TLS connection uses this PSK value. |
| 46 |
- This setting requires I(tls_accept=tls_with_psk) if current value of I(tls_accept) is C(unsecure). |
| 47 |
type: str |
| 48 |
|
| 49 |
notes: |
| 50 |
- This module returns changed=true when any value is set in I(tls_psk_identity) or I(tls_psk) as Zabbix API |
| 51 |
will not return any sensitive information back for module to compare. |
| 52 |
- Please note that this module configures B(global Zabbix Server settings). |
| 53 |
If you want to create autoregistration action so your hosts can automatically add themselves |
| 54 |
to the monitoring have a look at M(community.zabbix.zabbix_action). |
| 55 |
|
| 56 |
extends_documentation_fragment: |
| 57 |
- community.zabbix.zabbix |
| 58 |
|
| 59 |
""" |
| 60 |
|
| 61 |
EXAMPLES = """ |
| 62 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 63 |
- name: Set credentials to access Zabbix Server API |
| 64 |
ansible.builtin.set_fact: |
| 65 |
ansible_user: Admin |
| 66 |
ansible_httpapi_pass: zabbix |
| 67 |
|
| 68 |
# If you want to use API token to be authenticated by Zabbix Server |
| 69 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 70 |
- name: Set API token |
| 71 |
ansible.builtin.set_fact: |
| 72 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 73 |
|
| 74 |
- name: Update autoregistration |
| 75 |
# set task level variables as we change ansible_connection plugin here |
| 76 |
vars: |
| 77 |
ansible_network_os: community.zabbix.zabbix |
| 78 |
ansible_connection: httpapi |
| 79 |
ansible_httpapi_port: 443 |
| 80 |
ansible_httpapi_use_ssl: true |
| 81 |
ansible_httpapi_validate_certs: false |
| 82 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 83 |
ansible_host: zabbix-example-fqdn.org |
| 84 |
community.zabbix.zabbix_autoregister: |
| 85 |
tls_accept: |
| 86 |
- unsecure |
| 87 |
- tls_with_psk |
| 88 |
tls_psk_identity: "PSK 001" |
| 89 |
tls_psk: "11111595725ac58dd977beef14b97461a7c1045b9a1c923453302c5473193478" |
| 90 |
|
| 91 |
- name: Set unsecure to tls_accept |
| 92 |
# set task level variables as we change ansible_connection plugin here |
| 93 |
vars: |
| 94 |
ansible_network_os: community.zabbix.zabbix |
| 95 |
ansible_connection: httpapi |
| 96 |
ansible_httpapi_port: 443 |
| 97 |
ansible_httpapi_use_ssl: true |
| 98 |
ansible_httpapi_validate_certs: false |
| 99 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 100 |
ansible_host: zabbix-example-fqdn.org |
| 101 |
community.zabbix.zabbix_autoregister: |
| 102 |
tls_accept: unsecure |
| 103 |
""" |
| 104 |
|
| 105 |
RETURN = """ |
| 106 |
msg: |
| 107 |
description: The result of the operation |
| 108 |
returned: success |
| 109 |
type: str |
| 110 |
sample: "Successfully updated global autoregistration setting" |
| 111 |
""" |
| 112 |
|
| 113 |
|
| 114 |
from ansible.module_utils.basic import AnsibleModule |
| 115 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 116 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 117 |
|
| 118 |
|
| 119 |
class Autoregistration(ZabbixBase): |
| 120 |
|
| 121 |
# get autoregistration |
| 122 |
def get_autoregistration(self): |
| 123 |
try: |
| 124 |
return self._zapi.autoregistration.get({"output": "extend"}) |
| 125 |
except Exception as e: |
| 126 |
self._module.fail_json(msg="Failed to get autoregistration: %s" % e) |
| 127 |
|
| 128 |
# update autoregistration |
| 129 |
def update_autoregistration(self, current_setting, tls_accept, tls_psk_identity, tls_psk): |
| 130 |
tls_accept_values = [ |
| 131 |
None, |
| 132 |
"unsecure", |
| 133 |
"tls_with_psk" |
| 134 |
] |
| 135 |
params = {} |
| 136 |
try: |
| 137 |
if isinstance(tls_accept, str): |
| 138 |
params["tls_accept"] = zabbix_utils.helper_to_numeric_value( |
| 139 |
tls_accept_values, tls_accept |
| 140 |
) |
| 141 |
elif isinstance(tls_accept, list): |
| 142 |
params["tls_accept"] = 0 |
| 143 |
for _tls_accept_value in tls_accept: |
| 144 |
params["tls_accept"] += zabbix_utils.helper_to_numeric_value( |
| 145 |
tls_accept_values, _tls_accept_value |
| 146 |
) |
| 147 |
else: |
| 148 |
self._module.fail_json(msg="Value of tls_accept must be list or string.") |
| 149 |
|
| 150 |
if tls_psk_identity: |
| 151 |
params["tls_psk_identity"] = tls_psk_identity |
| 152 |
|
| 153 |
if tls_psk: |
| 154 |
params["tls_psk"] = tls_psk |
| 155 |
|
| 156 |
current_tls_accept = int(current_setting["tls_accept"]) |
| 157 |
if (current_tls_accept == tls_accept_values.index("unsecure") |
| 158 |
and params["tls_accept"] >= tls_accept_values.index("tls_with_psk")): |
| 159 |
if not tls_psk_identity or not tls_psk: |
| 160 |
self._module.fail_json(msg="Please set tls_psk_identity and tls_psk.") |
| 161 |
|
| 162 |
if (not tls_psk_identity and not tls_psk |
| 163 |
and params["tls_accept"] == current_tls_accept): |
| 164 |
self._module.exit_json(changed=False, result="Autoregistration is already up to date") |
| 165 |
|
| 166 |
if self._module.check_mode: |
| 167 |
self._module.exit_json(changed=True) |
| 168 |
self._zapi.autoregistration.update(params) |
| 169 |
self._module.exit_json(changed=True, result="Successfully updated global autoregistration setting") |
| 170 |
except Exception as e: |
| 171 |
self._module.fail_json(msg="Failed to update autoregistration: %s" % e) |
| 172 |
|
| 173 |
|
| 174 |
def main(): |
| 175 |
"""Main ansible module function |
| 176 |
""" |
| 177 |
|
| 178 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 179 |
argument_spec.update(dict( |
| 180 |
tls_accept=dict( |
| 181 |
type="list", |
| 182 |
elements="str", |
| 183 |
required=True |
| 184 |
), |
| 185 |
tls_psk_identity=dict(type="str", required=False, no_log=True), |
| 186 |
tls_psk=dict(type="str", required=False, no_log=True), |
| 187 |
)) |
| 188 |
module = AnsibleModule( |
| 189 |
argument_spec=argument_spec, |
| 190 |
supports_check_mode=True |
| 191 |
) |
| 192 |
|
| 193 |
tls_accept = module.params["tls_accept"] |
| 194 |
tls_psk_identity = module.params["tls_psk_identity"] |
| 195 |
tls_psk = module.params["tls_psk"] |
| 196 |
|
| 197 |
autoregistration_class_obj = Autoregistration(module) |
| 198 |
current_setting = autoregistration_class_obj.get_autoregistration() |
| 199 |
autoregistration_class_obj.update_autoregistration(current_setting, tls_accept, tls_psk_identity, tls_psk) |
| 200 |
|
| 201 |
|
| 202 |
if __name__ == "__main__": |
| 203 |
main() |