Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, D3DeFi
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_proxy_info
13 short_description: Gather information about Zabbix proxy
14 version_added: 1.5.0
15 author:
16 - Dusan Matejka (@D3DeFi)
17 description:
18 - This module allows you to obtain detailed information about configured zabbix proxies.
19 requirements:
20 - "python >= 3.9"
21 options:
22 proxy_name:
23 description:
24 - Name of the Zabbix proxy.
25 required: true
26 type: str
27 proxy_hosts:
28 description:
29 - Also return list of hosts monitored by the proxy.
30 required: false
31 default: false
32 type: bool
33 extends_documentation_fragment:
34 - community.zabbix.zabbix
35
36 """
37
38 EXAMPLES = """
39 # If you want to use Username and Password to be authenticated by Zabbix Server
40 - name: Set credentials to access Zabbix Server API
41 ansible.builtin.set_fact:
42 ansible_user: Admin
43 ansible_httpapi_pass: zabbix
44
45 # If you want to use API token to be authenticated by Zabbix Server
46 # https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
47 - name: Set API token
48 ansible.builtin.set_fact:
49 ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895
50
51 - name: Get zabbix proxy info alongside the list of hosts monitored by the proxy
52 # set task level variables as we change ansible_connection plugin here
53 vars:
54 ansible_network_os: community.zabbix.zabbix
55 ansible_connection: httpapi
56 ansible_httpapi_port: 443
57 ansible_httpapi_use_ssl: true
58 ansible_httpapi_validate_certs: false
59 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
60 ansible_host: zabbix-example-fqdn.org
61 community.zabbix.zabbix_proxy_info:
62 proxy_name: zbx01.example.com
63 proxy_hosts: true
64 """
65
66 RETURN = """
67 zabbix_proxy:
68 description: example
69 returned: always
70 type: dict
71 sample: {
72 "auto_compress": "1",
73 "custom_interfaces": "0",
74 "description": "ExampleProxy",
75 "discover": "0",
76 "flags": "0",
77 "host": "ExampleProxy",
78 "hosts": [
79 {
80 "host": "ExampleHost",
81 "hostid": "10453"
82 }
83 ],
84 "interface": {
85 "available": "0",
86 "details": [],
87 "disable_until": "0",
88 "dns": "ExampleProxy.local",
89 "error": "",
90 "errors_from": "0",
91 "hostid": "10452",
92 "interfaceid": "10",
93 "ip": "10.1.1.2",
94 "main": "1",
95 "port": "10051",
96 "type": "0",
97 "useip": "1"
98 },
99 "ipmi_authtype": "-1",
100 "ipmi_password": "",
101 "ipmi_privilege": "2",
102 "ipmi_username": "",
103 "lastaccess": "0",
104 "maintenance_from": "0",
105 "maintenance_status": "0",
106 "maintenance_type": "0",
107 "maintenanceid": "0",
108 "name": "",
109 "proxy_address": "",
110 "proxy_hostid": "0",
111 "proxyid": "10452",
112 "status": "6",
113 "templateid": "0",
114 "tls_accept": "1",
115 "tls_connect": "1",
116 "tls_issuer": "",
117 "tls_subject": "",
118 "uuid": ""
119 }
120 """
121
122
123 from ansible.module_utils.basic import AnsibleModule
124
125 from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
126 from ansible.module_utils.compat.version import LooseVersion
127 import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
128
129
130 class Proxy(ZabbixBase):
131
132 def get_proxy(self, name, hosts=False):
133 result = {}
134 if LooseVersion(self._zbx_api_version) < LooseVersion("7.0"):
135 params = {
136 "filter": {
137 "host": name
138 },
139 "selectInterface": "extend",
140 "output": "extend"
141 }
142 else:
143 params = {
144 "filter": {
145 "name": name
146 },
147 "output": "extend"
148 }
149
150 if hosts:
151 params["selectHosts"] = ["host", "hostid"]
152
153 try:
154 result = self._zapi.proxy.get(params)
155 except Exception as e:
156 self._module.fail_json(msg="Failed to get proxy information: %s" % e)
157
158 return result[0] if result else {}
159
160
161 def main():
162 argument_spec = zabbix_utils.zabbix_common_argument_spec()
163 argument_spec.update(dict(
164 proxy_name=dict(type="str", required=True),
165 proxy_hosts=dict(type="bool", required=False, default=False),
166 ))
167
168 module = AnsibleModule(
169 argument_spec=argument_spec,
170 supports_check_mode=True
171 )
172
173 name = module.params["proxy_name"]
174 hosts = module.params["proxy_hosts"]
175
176 proxy = Proxy(module)
177 result = proxy.get_proxy(name, hosts)
178 module.exit_json(changed=False, zabbix_proxy=result)
179
180
181 if __name__ == "__main__":
182 main()