| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright: (c) 2024, 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 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = """ |
| 14 |
--- |
| 15 |
module: zabbix_regexp_info |
| 16 |
|
| 17 |
short_description: Retrieve Zabbix regular expression |
| 18 |
|
| 19 |
|
| 20 |
description: |
| 21 |
- This module allows you to retrieve Zabbix regular expression. |
| 22 |
|
| 23 |
author: |
| 24 |
- ONODERA Masaru(@masa-orca) |
| 25 |
|
| 26 |
requirements: |
| 27 |
- "python >= 3.9" |
| 28 |
|
| 29 |
version_added: 3.3.0 |
| 30 |
|
| 31 |
options: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Name of this regular expression |
| 35 |
type: str |
| 36 |
required: true |
| 37 |
|
| 38 |
extends_documentation_fragment: |
| 39 |
- community.zabbix.zabbix |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = """ |
| 43 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 44 |
- name: Set credentials to access Zabbix Server API |
| 45 |
ansible.builtin.set_fact: |
| 46 |
ansible_user: Admin |
| 47 |
ansible_httpapi_pass: zabbix |
| 48 |
|
| 49 |
# If you want to use API token to be authenticated by Zabbix Server |
| 50 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 51 |
- name: Set API token |
| 52 |
ansible.builtin.set_fact: |
| 53 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 54 |
|
| 55 |
- name: Retrieve regexp of 'File systems for discovery' |
| 56 |
# set task level variables as we change ansible_connection plugin here |
| 57 |
vars: |
| 58 |
ansible_network_os: community.zabbix.zabbix |
| 59 |
ansible_connection: httpapi |
| 60 |
ansible_httpapi_port: 443 |
| 61 |
ansible_httpapi_use_ssl: true |
| 62 |
ansible_httpapi_validate_certs: false |
| 63 |
ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 64 |
ansible_host: zabbix-example-fqdn.org |
| 65 |
community.zabbix.zabbix_regexp_info: |
| 66 |
name: File systems for discovery |
| 67 |
""" |
| 68 |
|
| 69 |
RETURN = """ |
| 70 |
regexp: |
| 71 |
description: A Zabbix regular expression which is converted value to be used in community.zabbix.zabbix_regexp module. |
| 72 |
returned: success |
| 73 |
type: dict |
| 74 |
contains: |
| 75 |
name: |
| 76 |
description: Name of the regular expression |
| 77 |
type: str |
| 78 |
sample: File systems for discovery |
| 79 |
expressions: |
| 80 |
description: Expressions of the regular expression |
| 81 |
type: list |
| 82 |
sample: [{"case_sensitive": false, "expression_type": "result_is_true", ...}] |
| 83 |
test_string: |
| 84 |
description: Test string of the regular expression |
| 85 |
type: str |
| 86 |
sample: ext3 |
| 87 |
""" |
| 88 |
|
| 89 |
|
| 90 |
from ansible.module_utils.basic import AnsibleModule |
| 91 |
|
| 92 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 93 |
|
| 94 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 95 |
|
| 96 |
|
| 97 |
class RegularExpression(ZabbixBase): |
| 98 |
expression_type_values = [ |
| 99 |
"character_string_included", |
| 100 |
"any_character_string_included", |
| 101 |
"character_string_not_included", |
| 102 |
"result_is_true", |
| 103 |
"result_is_false", |
| 104 |
] |
| 105 |
|
| 106 |
exp_delimiter_values = [",", ".", "/"] |
| 107 |
|
| 108 |
def __init__(self, module, zbx=None, zapi_wrapper=None): |
| 109 |
super(RegularExpression, self).__init__(module, zbx, zapi_wrapper) |
| 110 |
|
| 111 |
def get_regexp(self, regexp_name): |
| 112 |
try: |
| 113 |
regexps = self._zapi.regexp.get( |
| 114 |
{ |
| 115 |
"output": "extend", |
| 116 |
"selectExpressions": [ |
| 117 |
"expression", |
| 118 |
"expression_type", |
| 119 |
"exp_delimiter", |
| 120 |
"case_sensitive", |
| 121 |
], |
| 122 |
"filter": {"name": regexp_name}, |
| 123 |
} |
| 124 |
) |
| 125 |
if len(regexps) >= 2: |
| 126 |
self._module.fail_json("Too many regexps are matched.") |
| 127 |
except Exception as e: |
| 128 |
self._module.fail_json( |
| 129 |
msg="Failed to get regular expression setting: %s" % e |
| 130 |
) |
| 131 |
regexp_json = regexps[0] |
| 132 |
|
| 133 |
expressions = [] |
| 134 |
|
| 135 |
for _expression in regexp_json['expressions']: |
| 136 |
case_sensitive = True |
| 137 |
if _expression['case_sensitive'] == '0': |
| 138 |
case_sensitive = False |
| 139 |
|
| 140 |
expression = { |
| 141 |
'expression': _expression['expression'], |
| 142 |
'expression_type': self.expression_type_values[int(_expression['expression_type'])], |
| 143 |
'case_sensitive': case_sensitive |
| 144 |
} |
| 145 |
|
| 146 |
if _expression['expression_type'] == '1': |
| 147 |
expression['exp_delimiter'] = _expression['exp_delimiter'] |
| 148 |
|
| 149 |
expressions.append(expression) |
| 150 |
|
| 151 |
regexp = { |
| 152 |
'name': regexp_name, |
| 153 |
'test_string': regexp_json['test_string'], |
| 154 |
'expressions': expressions |
| 155 |
} |
| 156 |
|
| 157 |
self._module.exit_json(regexp=regexp) |
| 158 |
|
| 159 |
|
| 160 |
def main(): |
| 161 |
"""Main ansible module function""" |
| 162 |
|
| 163 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 164 |
argument_spec.update( |
| 165 |
dict(name=dict(type="str", required=True)) |
| 166 |
) |
| 167 |
|
| 168 |
module = AnsibleModule( |
| 169 |
argument_spec=argument_spec, |
| 170 |
supports_check_mode=True |
| 171 |
) |
| 172 |
|
| 173 |
name = module.params["name"] |
| 174 |
|
| 175 |
regexp_class_obj = RegularExpression(module) |
| 176 |
regexp_class_obj.get_regexp(name) |
| 177 |
|
| 178 |
|
| 179 |
if __name__ == "__main__": |
| 180 |
main() |