| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, 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 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
--- |
| 14 |
module: zabbix_configuration |
| 15 |
short_description: Import Zabbix configuration |
| 16 |
description: |
| 17 |
- This module allows you to import Zabbix configuration data. |
| 18 |
- If the Zabix configuration.importcompare API returns non-empty list, this module returns changed is true. |
| 19 |
|
| 20 |
author: |
| 21 |
- ONODERA Masaru(@masa-orca) |
| 22 |
requirements: |
| 23 |
- "python >= 3.11" |
| 24 |
version_added: 3.4.0 |
| 25 |
options: |
| 26 |
content_json: |
| 27 |
description: |
| 28 |
- The content of the JSON file to be imported. |
| 29 |
- Mutually exclusive with I(content_xml) and I(content_yaml). |
| 30 |
required: false |
| 31 |
type: json |
| 32 |
content_xml: |
| 33 |
description: |
| 34 |
- The content of the XML file to be imported. |
| 35 |
- Mutually exclusive with I(content_json) and I(content_yaml). |
| 36 |
required: false |
| 37 |
type: str |
| 38 |
content_yaml: |
| 39 |
description: |
| 40 |
- The content of the YAML file to be imported. |
| 41 |
- Mutually exclusive with I(content_json) and I(content_xml). |
| 42 |
required: false |
| 43 |
type: str |
| 44 |
rules: |
| 45 |
description: |
| 46 |
- The rules for importing the configuration. |
| 47 |
- Please refer to rules of the Zabbix configuration.import API documentation for more details. |
| 48 |
- https://www.zabbix.com/documentation/current/en/manual/api/reference/configuration/import |
| 49 |
required: false |
| 50 |
type: dict |
| 51 |
|
| 52 |
extends_documentation_fragment: |
| 53 |
- community.zabbix.zabbix |
| 54 |
""" |
| 55 |
|
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
--- |
| 59 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 60 |
- name: Set credentials to access Zabbix Server API |
| 61 |
ansible.builtin.set_fact: |
| 62 |
ansible_user: Admin |
| 63 |
ansible_httpapi_pass: zabbix |
| 64 |
|
| 65 |
# If you want to use API token to be authenticated by Zabbix Server |
| 66 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 67 |
- name: Set API token |
| 68 |
ansible.builtin.set_fact: |
| 69 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 70 |
|
| 71 |
- name: Import Zabbix template from JSON |
| 72 |
# set task level variables as we change ansible_connection plugin here |
| 73 |
vars: |
| 74 |
ansible_network_os: community.zabbix.zabbix |
| 75 |
ansible_connection: httpapi |
| 76 |
ansible_httpapi_port: 443 |
| 77 |
ansible_httpapi_use_ssl: true |
| 78 |
ansible_httpapi_validate_certs: false |
| 79 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 80 |
ansible_host: zabbix-example-fqdn.org |
| 81 |
community.zabbix.zabbix_configuration: |
| 82 |
content_json: "{{ lookup('file', 'zbx_export_template.json') }}" |
| 83 |
rules: |
| 84 |
templates: |
| 85 |
createMissing: true |
| 86 |
updateExisting: true |
| 87 |
items: |
| 88 |
createMissing: true |
| 89 |
updateExisting: true |
| 90 |
deleteMissing: true |
| 91 |
triggers: |
| 92 |
createMissing: true |
| 93 |
updateExisting: true |
| 94 |
deleteMissing: true |
| 95 |
valueMaps: |
| 96 |
createMissing: true |
| 97 |
updateExisting: false |
| 98 |
""" |
| 99 |
|
| 100 |
import traceback |
| 101 |
|
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
from ansible.module_utils._text import to_native |
| 104 |
|
| 105 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 106 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 107 |
|
| 108 |
|
| 109 |
class Configuration(ZabbixBase): |
| 110 |
def import_compare(self, content, fmt, rules): |
| 111 |
"""Import Zabbix configuration data""" |
| 112 |
changed = False |
| 113 |
try: |
| 114 |
params = {"format": fmt, "source": content} |
| 115 |
if rules is not None: |
| 116 |
params["rules"] = rules |
| 117 |
compare_result = self._zapi.configuration.importcompare(params) |
| 118 |
if len(compare_result) != 0: |
| 119 |
changed = True |
| 120 |
return changed |
| 121 |
except Exception as e: |
| 122 |
self._module.fail_json( |
| 123 |
msg="Unable to compare configuration", |
| 124 |
details=to_native(e), |
| 125 |
exception=traceback.format_exc(), |
| 126 |
) |
| 127 |
|
| 128 |
def import_config(self, content, fmt, rules): |
| 129 |
"""Import Zabbix configuration data""" |
| 130 |
try: |
| 131 |
if self._module.check_mode: |
| 132 |
self._module.exit_json(changed=True) |
| 133 |
params = { |
| 134 |
"format": fmt, |
| 135 |
"source": content, |
| 136 |
} |
| 137 |
if rules is not None: |
| 138 |
params["rules"] = rules |
| 139 |
self._zapi.configuration.import_(params) |
| 140 |
except Exception as e: |
| 141 |
self._module.fail_json( |
| 142 |
msg="Unable to import configuration", |
| 143 |
details=to_native(e), |
| 144 |
exception=traceback.format_exc(), |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
def main(): |
| 149 |
"""Main ansible module function""" |
| 150 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 151 |
argument_spec.update( |
| 152 |
dict( |
| 153 |
content_json=dict(type="json", required=False), |
| 154 |
content_xml=dict(type="str", required=False), |
| 155 |
content_yaml=dict(type="str", required=False), |
| 156 |
rules=dict(type="dict", required=False) |
| 157 |
) |
| 158 |
) |
| 159 |
|
| 160 |
module = AnsibleModule( |
| 161 |
argument_spec=argument_spec, |
| 162 |
required_one_of=[["content_json", "content_xml", "content_yaml"]], |
| 163 |
mutually_exclusive=[["content_json", "content_xml", "content_yaml"]], |
| 164 |
supports_check_mode=True, |
| 165 |
) |
| 166 |
|
| 167 |
content_json = module.params["content_json"] |
| 168 |
content_xml = module.params["content_xml"] |
| 169 |
content_yaml = module.params["content_yaml"] |
| 170 |
rules = module.params["rules"] |
| 171 |
|
| 172 |
configuration = Configuration(module) |
| 173 |
|
| 174 |
content, format = None, None |
| 175 |
|
| 176 |
if content_json is not None: |
| 177 |
format = "json" |
| 178 |
content = content_json |
| 179 |
elif content_xml is not None: |
| 180 |
format = "xml" |
| 181 |
content = content_xml |
| 182 |
elif content_yaml is not None: |
| 183 |
format = "yaml" |
| 184 |
content = content_yaml |
| 185 |
|
| 186 |
changed = configuration.import_compare(content, format, rules) |
| 187 |
|
| 188 |
if not changed: |
| 189 |
module.exit_json(changed=changed, result="Configuration is up-to date") |
| 190 |
else: |
| 191 |
configuration.import_config(content, format, rules) |
| 192 |
module.exit_json(changed=changed, result="Configuration imported successfully") |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
main() |