Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) [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 RETURN = r"""
12 ---
13 host_groups:
14 description: List of Zabbix groups.
15 returned: success
16 type: dict
17 sample: [ { "flags": "0", "groupid": "33", "internal": "0", "name": "Hostgruup A" } ]
18 """
19
20 DOCUMENTATION = r"""
21 ---
22 module: zabbix_group_info
23 short_description: Gather information about Zabbix hostgroup
24 description:
25 - This module allows you to search for Zabbix hostgroup entries.
26 - This module was called C(zabbix_group_facts) before Ansible 2.9. The usage did not change.
27 author:
28 - "Michael Miko (@RedWhiteMiko)"
29 requirements:
30 - "python >= 3.9"
31 options:
32 hostgroup_name:
33 description:
34 - Name of the hostgroup in Zabbix.
35 - hostgroup is the unique identifier used and cannot be updated using this module.
36 required: false
37 type: list
38 elements: str
39 all_hostgroups:
40 description:
41 - return info about all hostgroups.
42 required: false
43 type: bool
44
45 extends_documentation_fragment:
46 - community.zabbix.zabbix
47 """
48
49 EXAMPLES = r"""
50 # If you want to use Username and Password to be authenticated by Zabbix Server
51 - name: Set credentials to access Zabbix Server API
52 ansible.builtin.set_fact:
53 ansible_user: Admin
54 ansible_httpapi_pass: zabbix
55
56 # If you want to use API token to be authenticated by Zabbix Server
57 # https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
58 - name: Set API token
59 ansible.builtin.set_fact:
60 ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895
61
62 - name: Get hostgroup info
63 # set task level variables as we change ansible_connection plugin here
64 vars:
65 ansible_network_os: community.zabbix.zabbix
66 ansible_connection: httpapi
67 ansible_httpapi_port: 443
68 ansible_httpapi_use_ssl: true
69 ansible_httpapi_validate_certs: false
70 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
71 ansible_host: zabbix-example-fqdn.org
72 community.zabbix.zabbix_group_info:
73 hostgroup_name:
74 - ExampleHostgroup
75
76 - name: Get info about all hostgroups
77 # set task level variables as we change ansible_connection plugin here
78 vars:
79 ansible_network_os: community.zabbix.zabbix
80 ansible_connection: httpapi
81 ansible_httpapi_port: 443
82 ansible_httpapi_use_ssl: true
83 ansible_httpapi_validate_certs: false
84 ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
85 ansible_host: zabbix-example-fqdn.org
86 community.zabbix.zabbix_group_info:
87 all_hostgroups: true
88 register: all_hostgroups
89 """
90
91 from ansible.module_utils.basic import AnsibleModule
92
93 from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase
94 import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils
95
96
97 class Host(ZabbixBase):
98 def get_group_ids_by_group_names(self, group_names):
99 group_list = self._zapi.hostgroup.get({"output": "extend", "filter": {"name": group_names}})
100 if len(group_list) < 1:
101 self._module.fail_json(msg="Hostgroup not found: %s" % group_names)
102 return group_list
103
104 def get_all_groups(self):
105 group_list = self._zapi.hostgroup.get({"output": "extend"})
106 if len(group_list) < 1:
107 self._module.fail_json(msg="No Hostgroup can be found")
108 return group_list
109
110
111 def main():
112 argument_spec = zabbix_utils.zabbix_common_argument_spec()
113 argument_spec.update(dict(
114 hostgroup_name=dict(type="list", required=False, elements="str"),
115 all_hostgroups=dict(type="bool", required=False),
116 ))
117 module = AnsibleModule(
118 argument_spec=argument_spec,
119 supports_check_mode=True
120 )
121
122 hostgroup_name = module.params["hostgroup_name"]
123 all_hostgroups = module.params["all_hostgroups"]
124 if hostgroup_name:
125 host = Host(module)
126 host_groups = host.get_group_ids_by_group_names(hostgroup_name)
127 module.exit_json(host_groups=host_groups)
128
129 if all_hostgroups:
130 host = Host(module)
131 host_groups = host.get_all_groups()
132 module.exit_json(host_groups=host_groups)
133
134
135 if __name__ == "__main__":
136 main()