Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2024, Evgeny Yurchenko
4 #
5 # This file is part of Ansible
6 #
7 # Ansible is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Ansible is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 from __future__ import (absolute_import, division, print_function)
22 __metaclass__ = type
23
24
25 DOCUMENTATION = r"""
26 ---
27 module: zabbix_proxy_group
28 short_description: Create/update/delete Zabbix proxy group
29 description:
30 - This module allows you to create, modify and delete Zabbix proxy group.
31 author:
32 - Evgeny Yurchenko (@BGmot)
33 requirements:
34 - python >= 3.9
35 options:
36 name:
37 description:
38 - Name of the proxy group.
39 required: true
40 type: str
41 description:
42 description:
43 - Description of the proxy group.
44 required: false
45 type: str
46 failover_delay:
47 description:
48 - Failover period for each proxy in the group to have online/offline state.
49 - Time suffixes are supported, e.g. 30s, 1m.
50 required: false
51 type: str
52 default: 1m
53 min_online:
54 description:
55 - Minimum number of online proxies required for the group to be online.
56 - "Possible values range: 1-1000."
57 required: false
58 type: str
59 default: "1"
60 state:
61 description:
62 - State of the proxy group.
63 - On C(present), it will create if proxy group does not exist or update it if the associated data is different.
64 - On C(absent) will remove the proxy group if it exists.
65 choices: ["present", "absent"]
66 default: "present"
67 type: str
68
69 extends_documentation_fragment:
70 - community.zabbix.zabbix
71
72 """
73
74 EXAMPLES = r"""
75 ---
76 # If you want to use Username and Password to be authenticated by Zabbix Server
77 - name: Set credentials to access Zabbix Server API
78 ansible.builtin.set_fact:
79 ansible_user: Admin
80 ansible_httpapi_pass: zabbix
81
82 # If you want to use API token to be authenticated by Zabbix Server
83 # https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
84 - name: Set API token
85 ansible.builtin.set_fact:
86 ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895
87
88 - name: Create/update Zabbix proxy group
89 vars:
90 ansible_network_os: community.zabbix.zabbix
91 ansible_connection: httpapi
92 ansible_httpapi_port: 443
93 ansible_httpapi_use_ssl: true
94 ansible_httpapi_validate_certs: false
95 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
96 ansible_host: zabbix-example-fqdn.org
97 community.zabbix.zabbix_proxy_group:
98 name: ProxyGroup01
99 description: Example Zabbix Proxy Group
100 state: present
101 failover_delay: 10s
102 min_online: 2
103
104 - name: delete Zabbix proxy group
105 vars:
106 ansible_network_os: community.zabbix.zabbix
107 ansible_connection: httpapi
108 ansible_httpapi_port: 443
109 ansible_httpapi_use_ssl: true
110 ansible_httpapi_validate_certs: false
111 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
112 ansible_host: zabbix-example-fqdn.org
113 community.zabbix.zabbix_proxy_group:
114 name: ProxyGroup01
115 state: absent
116 """
117
118 RETURN = r""" # """
119
120
121 from ansible.module_utils.basic import AnsibleModule
122
123 from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
124
125 import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
126
127
128 class ProxyGroup(ZabbixBase):
129 def __init__(self, module, zbx=None, zapi_wrapper=None):
130 super(ProxyGroup, self).__init__(module, zbx, zapi_wrapper)
131 self.existing_data = None
132
133 def proxy_group_exists(self, proxy_group_name):
134 result = self._zapi.proxygroup.get({"output": "extend",
135 "filter": {"name": proxy_group_name}})
136
137 if len(result) > 0 and "proxy_groupid" in result[0]:
138 self.existing_data = result[0]
139 return result[0]["proxy_groupid"]
140 else:
141 return result
142
143 def add_proxy_group(self, data):
144 try:
145 if self._module.check_mode:
146 self._module.exit_json(changed=True)
147
148 parameters = {}
149 for item in data:
150 if data[item]:
151 parameters[item] = data[item]
152
153 proxy_group_ids_list = self._zapi.proxygroup.create(parameters)
154
155 self._module.exit_json(changed=True,
156 result="Successfully added proxy group %s" % (data["name"]))
157
158 if len(proxy_group_ids_list) >= 1:
159 return proxy_group_ids_list["proxy_groupids"][0]
160
161 except Exception as e:
162 self._module.fail_json(msg="Failed to create proxy group %s: %s" % (data["name"], e))
163
164 def delete_proxy_group(self, proxy_group_id, proxy_group_name):
165 try:
166 if self._module.check_mode:
167 self._module.exit_json(changed=True)
168
169 self._zapi.proxygroup.delete([proxy_group_id])
170
171 self._module.exit_json(changed=True, result="Successfully deleted proxy group %s" % proxy_group_name)
172
173 except Exception as e:
174 self._module.fail_json(msg="Failed to delete proxy group %s: %s" % (proxy_group_name, str(e)))
175
176 def update_proxy_group(self, proxy_group_id, data):
177 try:
178 if self._module.check_mode:
179 self._module.exit_json(changed=True)
180
181 parameters = {}
182 for key in data:
183 if data[key]:
184 parameters[key] = data[key]
185
186 parameters["proxy_groupid"] = proxy_group_id
187
188 change_parameters = {}
189 difference = zabbix_utils.helper_cleanup_data(zabbix_utils.helper_compare_dictionaries(parameters, self.existing_data, change_parameters))
190
191 if difference == {}:
192 self._module.exit_json(changed=False)
193 else:
194 difference["proxy_groupid"] = proxy_group_id
195 self._zapi.proxygroup.update(parameters)
196 self._module.exit_json(
197 changed=True,
198 result="Successfully updated proxy group %s (%s)" % (data["name"], proxy_group_id)
199 )
200
201 except Exception as e:
202 self._module.fail_json(msg="Failed to update proxy group %s: %s" % (data["name"], e))
203
204
205 def main():
206 argument_spec = zabbix_utils.zabbix_common_argument_spec()
207 argument_spec.update(dict(
208 name=dict(type="str", required=True),
209 description=dict(type="str", required=False),
210 failover_delay=dict(type="str", default="1m"),
211 state=dict(type="str", default="present", choices=["present", "absent"]),
212 min_online=dict(type="str", default="1")
213 ))
214 module = AnsibleModule(
215 argument_spec=argument_spec,
216 supports_check_mode=True
217 )
218
219 proxy_group_name = module.params["name"]
220 description = module.params["description"]
221 failover_delay = module.params["failover_delay"]
222 min_online = module.params["min_online"]
223 state = module.params["state"]
224
225 proxy_group = ProxyGroup(module)
226
227 # check if proxy group already exists
228 proxy_group_id = proxy_group.proxy_group_exists(proxy_group_name)
229
230 if proxy_group_id:
231 if state == "absent":
232 # remove proxy group
233 proxy_group.delete_proxy_group(proxy_group_id, proxy_group_name)
234 else:
235 proxy_group.update_proxy_group(proxy_group_id, {
236 "name": proxy_group_name,
237 "description": description,
238 "failover_delay": str(failover_delay),
239 "min_online": str(min_online)
240 })
241 else:
242 if state == "absent":
243 # the proxy group is already deleted.
244 module.exit_json(changed=False)
245
246 proxy_group_id = proxy_group.add_proxy_group(data={
247 "name": proxy_group_name,
248 "description": description,
249 "failover_delay": str(failover_delay),
250 "min_online": str(min_online)
251 })
252
253
254 if __name__ == "__main__":
255 main()