| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, G.J. Doornink |
| 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_valuemap_info |
| 13 |
short_description: Gather information about Zabbix valuemap |
| 14 |
author: |
| 15 |
- G.J. Doornink (@gjdoornink) |
| 16 |
description: |
| 17 |
- This module allows you to search for Zabbix valuemap entries. |
| 18 |
requirements: |
| 19 |
- "python >= 3.9" |
| 20 |
options: |
| 21 |
host_name: |
| 22 |
type: str |
| 23 |
description: |
| 24 |
- Name of the Zabbix host. |
| 25 |
required: true |
| 26 |
name: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- Name of the Zabbix valuemap. |
| 30 |
required: false |
| 31 |
extends_documentation_fragment: |
| 32 |
- community.zabbix.zabbix |
| 33 |
|
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = """ |
| 37 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 38 |
- name: Set credentials to access Zabbix Server API |
| 39 |
ansible.builtin.set_fact: |
| 40 |
ansible_user: Admin |
| 41 |
ansible_httpapi_pass: zabbix |
| 42 |
|
| 43 |
# If you want to use API token to be authenticated by Zabbix Server |
| 44 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 45 |
- name: Set API token |
| 46 |
ansible.builtin.set_fact: |
| 47 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 48 |
|
| 49 |
- name: Get zabbix valuemap info |
| 50 |
# set task level variables as we change ansible_connection plugin here |
| 51 |
vars: |
| 52 |
ansible_network_os: community.zabbix.zabbix |
| 53 |
ansible_connection: httpapi |
| 54 |
ansible_httpapi_port: 443 |
| 55 |
ansible_httpapi_use_ssl: true |
| 56 |
ansible_httpapi_validate_certs: false |
| 57 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 58 |
ansible_host: zabbix-example-fqdn.org |
| 59 |
community.zabbix.zabbix_valuemap_info: |
| 60 |
host_name: example_host |
| 61 |
name: Numbers |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = """ |
| 65 |
zabbix_valuemap: |
| 66 |
description: List of Zabbix valuemaps. |
| 67 |
returned: always |
| 68 |
type: list |
| 69 |
elements: dict |
| 70 |
sample: [ |
| 71 |
{ |
| 72 |
"hostid": "10771", |
| 73 |
"mappings": [ |
| 74 |
{ |
| 75 |
"newvalue": "one", |
| 76 |
"type": "0", |
| 77 |
"value": "1" |
| 78 |
}, |
| 79 |
{ |
| 80 |
"newvalue": "two", |
| 81 |
"type": "0", |
| 82 |
"value": "2" |
| 83 |
} |
| 84 |
], |
| 85 |
"name": "Numbers", |
| 86 |
"uuid": "", |
| 87 |
"valuemapid": "1467" |
| 88 |
} |
| 89 |
] |
| 90 |
""" |
| 91 |
|
| 92 |
|
| 93 |
from ansible.module_utils.basic import AnsibleModule |
| 94 |
|
| 95 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 96 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 97 |
|
| 98 |
|
| 99 |
class Valuemap(ZabbixBase): |
| 100 |
def get_host_id(self, host_name): |
| 101 |
try: |
| 102 |
host_list = self._zapi.host.get({"filter": {"host": host_name}}) |
| 103 |
except Exception as e: |
| 104 |
self._module.fail_json(msg="Failed to get host: %s" % e) |
| 105 |
|
| 106 |
if len(host_list) < 1: |
| 107 |
self._module.fail_json(msg="Host not found: %s" % host_name) |
| 108 |
else: |
| 109 |
return host_list[0]["hostid"] |
| 110 |
|
| 111 |
def get_valuemaps(self, host_name, valuemap_name): |
| 112 |
valuemap_list = [] |
| 113 |
try: |
| 114 |
data = {"output": "extend", "selectMappings": "extend", "filter": {}} |
| 115 |
data["filter"]["hostid"] = self.get_host_id(host_name) |
| 116 |
if valuemap_name is not None: |
| 117 |
data["filter"]["name"] = valuemap_name |
| 118 |
|
| 119 |
valuemap_list = self._zapi.valuemap.get(data) |
| 120 |
except Exception as e: |
| 121 |
self._module.fail_json(msg="Failed to get valuemaps: %s" % e) |
| 122 |
|
| 123 |
if len(valuemap_list) > 0: |
| 124 |
valuemap_list = [valuemap for valuemap in valuemap_list if valuemap["uuid"] == ""] |
| 125 |
if valuemap_name is not None and len(valuemap_list) < 1: |
| 126 |
self._module.fail_json(msg="Valuemap not found: %s" % valuemap_name) |
| 127 |
|
| 128 |
return valuemap_list |
| 129 |
|
| 130 |
|
| 131 |
def main(): |
| 132 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 133 |
argument_spec.update(dict( |
| 134 |
host_name=dict(type="str", required=True), |
| 135 |
name=dict(type="str", required=False) |
| 136 |
)) |
| 137 |
module = AnsibleModule( |
| 138 |
argument_spec=argument_spec, |
| 139 |
supports_check_mode=True |
| 140 |
) |
| 141 |
|
| 142 |
host_name = module.params["host_name"] |
| 143 |
name = module.params["name"] |
| 144 |
|
| 145 |
valuemap = Valuemap(module) |
| 146 |
valuemaps = valuemap.get_valuemaps(host_name, name) |
| 147 |
module.exit_json(changed=False, valuemaps=valuemaps) |
| 148 |
|
| 149 |
|
| 150 |
if __name__ == "__main__": |
| 151 |
main() |