Debian Codegraph @

ansible_collections/community/zabbix/plugins/modules/zabbix_api_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright: (c) 2022, 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 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: zabbix_api_info
14
15 short_description: Retrieve Zabbix API info
16
17 description:
18 - This module allows you to retrieve Zabbix api info.
19
20 author:
21 - ONODERA Masaru(@masa-orca)
22
23 requirements:
24 - "python >= 3.9"
25
26 version_added: 2.1.0
27
28 extends_documentation_fragment:
29 - community.zabbix.zabbix
30 '''
31
32 EXAMPLES = '''
33 # If you want to use Username and Password to be authenticated by Zabbix Server
34 - name: Set credentials to access Zabbix Server API
35 ansible.builtin.set_fact:
36 ansible_user: Admin
37 ansible_httpapi_pass: zabbix
38
39 # If you want to use API token to be authenticated by Zabbix Server
40 # https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
41 - name: Set API token
42 ansible.builtin.set_fact:
43 ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895
44
45 - name: Retrieve API information
46 # set task level variables as we change ansible_connection plugin here
47 vars:
48 ansible_network_os: community.zabbix.zabbix
49 ansible_connection: httpapi
50 ansible_httpapi_port: 443
51 ansible_httpapi_use_ssl: true
52 ansible_httpapi_validate_certs: false
53 ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
54 ansible_host: zabbix-example-fqdn.org
55 community.zabbix.zabbix_api_info:
56 register: zbx_api_info
57 '''
58
59 RETURN = '''
60 api:
61 description: Summaries of Zabbix API info
62 returned: success
63 type: dict
64 contains:
65 version:
66 description: API version
67 type: str
68 sample: 6.0.18
69 '''
70
71 from ansible.module_utils.basic import AnsibleModule
72 from ansible.module_utils.compat.version import LooseVersion
73
74 from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
75 import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
76
77
78 class ApiInfo(ZabbixBase):
79 def __init__(self, module, zbx=None, zapi_wrapper=None):
80 super(ApiInfo, self).__init__(module, zbx, zapi_wrapper)
81 if LooseVersion(self._zbx_api_version) < LooseVersion('2.2.0'):
82 module.fail_json(msg="This module doesn't support Zabbix versions lower than 2.2.0")
83
84 def get_api_info(self):
85 if self._module.check_mode:
86 self._module.exit_json(changed=False)
87 try:
88 api = {
89 'version': self._zbx_api_version
90 }
91 self._module.exit_json(api=api)
92 except Exception as e:
93 self._module.fail_json(msg="Failed to retrieve API info: %s" % e)
94
95
96 def main():
97 argument_spec = zabbix_utils.zabbix_common_argument_spec()
98 module = AnsibleModule(
99 argument_spec=argument_spec,
100 supports_check_mode=True
101 )
102
103 api_info = ApiInfo(module)
104 api_info.get_api_info()
105
106
107 if __name__ == '__main__':
108 main()