| 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_service_info |
| 13 |
short_description: Gather information about Zabbix service |
| 14 |
author: |
| 15 |
- Kanstantsin Maksimau (@us3241) |
| 16 |
description: |
| 17 |
- This module allows you to obtain detailed information about configured zabbix service. |
| 18 |
requirements: |
| 19 |
- "python >= 3.9" |
| 20 |
options: |
| 21 |
service_name: |
| 22 |
description: |
| 23 |
- Name of the Zabbix service. |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
extends_documentation_fragment: |
| 27 |
- community.zabbix.zabbix |
| 28 |
|
| 29 |
""" |
| 30 |
|
| 31 |
EXAMPLES = """ |
| 32 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 33 |
- name: Set credentials to access Zabbix Server API |
| 34 |
ansible.builtin.set_fact: |
| 35 |
ansible_user: Admin |
| 36 |
ansible_httpapi_pass: zabbix |
| 37 |
|
| 38 |
# If you want to use API token to be authenticated by Zabbix Server |
| 39 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 40 |
- name: Set API token |
| 41 |
ansible.builtin.set_fact: |
| 42 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 43 |
|
| 44 |
- name: Get zabbix service info |
| 45 |
# set task level variables as we change ansible_connection plugin here |
| 46 |
vars: |
| 47 |
ansible_network_os: community.zabbix.zabbix |
| 48 |
ansible_connection: httpapi |
| 49 |
ansible_httpapi_port: 443 |
| 50 |
ansible_httpapi_use_ssl: true |
| 51 |
ansible_httpapi_validate_certs: false |
| 52 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 53 |
ansible_host: zabbix-example-fqdn.org |
| 54 |
community.zabbix.zabbix_service_info: |
| 55 |
service_name: zbx01.example.com |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = """ |
| 59 |
zabbix_service: |
| 60 |
description: example |
| 61 |
returned: always |
| 62 |
type: dict |
| 63 |
sample: { |
| 64 |
"algorithm": "2", |
| 65 |
"children": [ |
| 66 |
{ |
| 67 |
"algorithm": "0", |
| 68 |
"created_at": "1712743194", |
| 69 |
"description": "", |
| 70 |
"name": "example children", |
| 71 |
"propagation_rule": "0", |
| 72 |
"propagation_value": "0", |
| 73 |
"readonly": false, |
| 74 |
"serviceid": "81", |
| 75 |
"sortorder": "0", |
| 76 |
"status": "-1", |
| 77 |
"uuid": "ce6e1a3784a547b48ee6707f6e061102", |
| 78 |
"weight": "0" |
| 79 |
} |
| 80 |
], |
| 81 |
"created_at": "1709717864", |
| 82 |
"description": "", |
| 83 |
"name": "example service", |
| 84 |
"parents": [], |
| 85 |
"problem_tags": [], |
| 86 |
"propagation_rule": "0", |
| 87 |
"propagation_value": "0", |
| 88 |
"readonly": false, |
| 89 |
"serviceid": "51", |
| 90 |
"sortorder": "0", |
| 91 |
"status": "-1", |
| 92 |
"status_rules": [], |
| 93 |
"tags": [], |
| 94 |
"uuid": "420e48b363fe473c95288c817031447f", |
| 95 |
"weight": "0" |
| 96 |
} |
| 97 |
""" |
| 98 |
|
| 99 |
|
| 100 |
from ansible.module_utils.basic import AnsibleModule |
| 101 |
|
| 102 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 103 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 104 |
|
| 105 |
|
| 106 |
class Service(ZabbixBase): |
| 107 |
|
| 108 |
def get_service(self, name): |
| 109 |
result = {} |
| 110 |
params = { |
| 111 |
"filter": { |
| 112 |
"name": name |
| 113 |
}, |
| 114 |
"output": "extend", |
| 115 |
"selectParents": "extend", |
| 116 |
"selectTags": "extend", |
| 117 |
"selectProblemTags": "extend", |
| 118 |
"selectChildren": "extend", |
| 119 |
"selectStatusRules": "extend" |
| 120 |
} |
| 121 |
|
| 122 |
try: |
| 123 |
result = self._zapi.service.get(params) |
| 124 |
except Exception as e: |
| 125 |
self._module.fail_json(msg="Failed to get service information: %s" % e) |
| 126 |
|
| 127 |
return result[0] if result else {} |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 132 |
argument_spec.update(dict( |
| 133 |
service_name=dict(type="str", required=True), |
| 134 |
)) |
| 135 |
|
| 136 |
module = AnsibleModule( |
| 137 |
argument_spec=argument_spec, |
| 138 |
supports_check_mode=True |
| 139 |
) |
| 140 |
|
| 141 |
name = module.params["service_name"] |
| 142 |
|
| 143 |
service = Service(module) |
| 144 |
result = service.get_service(name) |
| 145 |
module.exit_json(changed=False, zabbix_service=result) |
| 146 |
|
| 147 |
|
| 148 |
if __name__ == "__main__": |
| 149 |
main() |