diff --git a/changelogs/fragments/pr_1234.yml b/changelogs/fragments/pr_1234.yml new file mode 100644 index 000000000..f5239f331 --- /dev/null +++ b/changelogs/fragments/pr_1234.yml @@ -0,0 +1,3 @@ +minor_changes: + - zabbix_item - add support for setting master items by name + - zabbix_itemprototype - add support for setting master items by name diff --git a/plugins/modules/zabbix_item.py b/plugins/modules/zabbix_item.py index 845eac85d..2d6bb95eb 100644 --- a/plugins/modules/zabbix_item.py +++ b/plugins/modules/zabbix_item.py @@ -116,6 +116,29 @@ - log - numeric_unsigned - text + master_item: + description: + - item that is the master of the current one + - Overrides "master_itemid" in API docs + required: false + type: dict + suboptions: + item_name: + description: + - name of the master item + required: true + type: str + host_name: + description: + - name of the host the master item belongs to + - Required when I(template_name) is not used. + - Mutually exclusive with I(template_name). + required: false + template_name: + description: + - name of the template the master item belongs to + - Required when I(host_name) is not used. + - Mutually exclusive with I(host_name). preprocessing: description: - Item preprocessing options. @@ -260,6 +283,36 @@ value: application state: present +- name: create a dependent item + # set task level variables as we change ansible_connection plugin here + vars: + ansible_network_os: community.zabbix.zabbix + ansible_connection: httpapi + ansible_httpapi_port: 443 + ansible_httpapi_use_ssl: true + ansible_httpapi_validate_certs: false + ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http:///zabbixeu + ansible_host: zabbix-example-fqdn.org + community.zabbix.zabbix_item: + name: depend_item + host_name: example_host + params: + type: dependent_item + key: vfs.fs.pused + value_type: numeric_float + units: '%' + master_item: + item_name: example_item + host_name: example_host + preprocessing: + - type: jsonpath + params: '$[?(@.fstype == "ext4")]' + error_handler: zabbix_server + - type: jsonpath + params: "$[*].['bytes', 'inodes'].pused.max()" + error_handler: zabbix_server + state: present + - name: Delete Zabbix item # set task level variables as we change ansible_connection plugin here vars: @@ -401,6 +454,16 @@ def sanitize_params(self, name, params): params['status'] = 1 else: self._module.fail_json(msg="Status must be 'enabled' or 'disabled', got %s" % status) + if 'master_item' in params: + if 'host_name' not in params['master_item']: + params['master_item']['host_name'] = None + if 'template_name' not in params['master_item']: + params['master_item']['template_name'] = None + master_items = self.get_items(params['master_item']['item_name'], params['master_item']['host_name'], params['master_item']['template_name']) + if len(master_items) == 0: + self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name']) + params['master_itemid'] = master_items[0]['itemid'] + params.pop('master_item') if 'preprocessing' in params: for param in params['preprocessing']: preprocess_type_int = self.PREPROCESSING_TYPES[param['type']] diff --git a/plugins/modules/zabbix_itemprototype.py b/plugins/modules/zabbix_itemprototype.py index 66e756ed4..1fe5b9560 100644 --- a/plugins/modules/zabbix_itemprototype.py +++ b/plugins/modules/zabbix_itemprototype.py @@ -121,6 +121,34 @@ - log - numeric_unsigned - text + master_item: + description: + - item that is the master of the current one + - Overrides "master_itemid" in API docs + required: false + type: dict + suboptions: + item_name: + description: + - name of the master item + required: true + type: str + discovery_rule: + description: + - name of the discovery rule the master item belongs to + required: true + type: str + host_name: + description: + - name of the host the master item belongs to + - Required when I(template_name) is not used. + - Mutually exclusive with I(template_name). + required: false + template_name: + description: + - name of the template the master item belongs to + - Required when I(host_name) is not used. + - Mutually exclusive with I(host_name). preprocessing: description: - Item preprocessing options. @@ -272,6 +300,35 @@ value: application state: present +- name: create dependent item + # set task level variables as we change ansible_connection plugin here + vars: + ansible_network_os: community.zabbix.zabbix + ansible_connection: httpapi + ansible_httpapi_port: 443 + ansible_httpapi_use_ssl: true + ansible_httpapi_validate_certs: false + ansible_zabbix_url_path: 'zabbixeu' # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http:///zabbixeu + ansible_host: zabbix-example-fqdn.org + community.zabbix.zabbix_itemprototype: + name: '{% raw %}{#FSNAME}:example_depend_item_prototype{% endraw %}' + discoveryrule_name: example_rule + host_name: example_host + params: + type: dependent_item + key: '{% raw %}vfs.fs.size.half[{#FSNAME}]{% endraw %}' + value_type: numeric_float + units: B + master_item: + item_name: '{% raw %}{#FSNAME}:example_item_prototype{% endraw %}' + discoveryrule_name: example_rule + host_name: example_host + preprocessing: + - type: javascript + params: 'return value / 2;' + error_handler: zabbix_server + state: present + - name: Delete Zabbix item prototype # set task level variables as we change ansible_connection plugin here vars: @@ -423,6 +480,17 @@ def sanitize_params(self, name, discoveryrule_name, params, host_name=None, temp if 'enabled' in params: params['status'] = params['enabled'] params.pop('enabled') + if 'master_item' in params: + if 'host_name' not in params['master_item']: + params['master_item']['host_name'] = None + if 'template_name' not in params['master_item']: + params['master_item']['template_name'] = None + master_items = self.get_itemprototypes(params['master_item']['item_name'], params['master_item']['discoveryrule_name'], + params['master_item']['host_name'], params['master_item']['template_name']) + if len(master_items) == 0: + self._module.fail_json(msg="No items with the name %s exist to depend on" % params['master_item']['item_name']) + params['master_itemid'] = master_items[0]['itemid'] + params.pop('master_item') if 'preprocessing' in params: for param in params['preprocessing']: preprocess_type_int = self.PREPROCESSING_TYPES[param['type']] diff --git a/tests/integration/targets/test_zabbix_item/tasks/zabbix_tests.yml b/tests/integration/targets/test_zabbix_item/tasks/zabbix_tests.yml index 3c6831acd..86767a716 100644 --- a/tests/integration/targets/test_zabbix_item/tasks/zabbix_tests.yml +++ b/tests/integration/targets/test_zabbix_item/tasks/zabbix_tests.yml @@ -89,6 +89,73 @@ ansible.builtin.assert: that: not zbxhostitem_missing_delete is changed +- name: test - create new Zabbix master item on host + community.zabbix.zabbix_item: + name: TestItem + host_name: ExampleHost + params: + type: zabbix_agent_active + key: vfs.fs.get + value_type: text + interval: 1m + tags: + - tag: tag + value: value + state: present + register: zbxhostmstitem_new + +- name: assert that item was created + ansible.builtin.assert: + that: zbxhostmstitem_new is changed + +- name: create dependent item + community.zabbix.zabbix_item: + name: TestDependItem + host_name: ExampleHost + params: + type: dependent_item + key: vfs.fs.pused + value_type: numeric_float + units: '%' + master_item: + item_name: TestItem + host_name: ExampleHost + preprocessing: + - type: jsonpath + params: '$[?(@.fstype == "ext4")]' + error_handler: zabbix_server + - type: jsonpath + params: "$[*].['bytes', 'inodes'].pused.max()" + error_handler: zabbix_server + state: present + register: zbxhostdependitem_new + +- name: assert that item was created + ansible.builtin.assert: + that: zbxhostdependitem_new is changed + +- name: test - attempt to delete previously created zabbix master item + community.zabbix.zabbix_item: + name: TestItem + host_name: ExampleHost + state: absent + register: zbxhostmstitem_existing_delete + +- name: assert that item was deleted + ansible.builtin.assert: + that: zbxhostmstitem_existing_delete is changed + +- name: test - attempt to delete dependent item + community.zabbix.zabbix_item: + name: TestDependItem + host_name: ExampleHost + state: absent + register: zbxhostdependitem_delete + +- name: assert that the item had been removed with its master + ansible.builtin.assert: + that: not zbxhostdependitem_delete is changed + - name: test - create new Zabbix item on template with many options set community.zabbix.zabbix_item: name: TestItem @@ -116,7 +183,7 @@ ansible.builtin.assert: that: zbxtempitem_new is changed -- name: test - create same Zabbix item group once again +- name: test - create same Zabbix item once again community.zabbix.zabbix_item: name: TestItem template_name: ExampleTemplate diff --git a/tests/integration/targets/test_zabbix_itemprototype/tasks/zabbix_tests.yml b/tests/integration/targets/test_zabbix_itemprototype/tasks/zabbix_tests.yml index c27506746..604ecf4d4 100644 --- a/tests/integration/targets/test_zabbix_itemprototype/tasks/zabbix_tests.yml +++ b/tests/integration/targets/test_zabbix_itemprototype/tasks/zabbix_tests.yml @@ -80,6 +80,76 @@ ansible.builtin.assert: that: not zbxhostitem_missing_delete is changed +- name: test - create new Zabbix master item on host + community.zabbix.zabbix_itemprototype: + name: '{% raw %}{#FSNAME}:TestItemPrototype{% endraw %}' + discoveryrule_name: ExampleHostRule + host_name: ExampleHost + params: + type: zabbix_agent_active + key: '{% raw %}vfs.fs.size[{#FSNAME},used]{% endraw %}' + value_type: numeric_unsigned + units: B + interval: 1m + tags: + - tag: tag + value: value + state: present + register: zbxhostmstitem_new + +- name: assert that item was created + ansible.builtin.assert: + that: zbxhostmstitem_new is changed + +- name: create dependent item + community.zabbix.zabbix_itemprototype: + name: '{% raw %}{#FSNAME}:TestDependItemPrototype{% endraw %}' + discoveryrule_name: ExampleHostRule + host_name: ExampleHost + params: + type: dependent_item + key: '{% raw %}vfs.fs.size.half[{#FSNAME}]{% endraw %}' + value_type: numeric_float + units: B + master_item: + item_name: '{% raw %}{#FSNAME}:TestItemPrototype{% endraw %}' + discoveryrule_name: ExampleHostRule + host_name: ExampleHost + preprocessing: + - type: javascript + params: 'return value / 2;' + error_handler: zabbix_server + state: present + register: zbxhostdependitem_new + +- name: assert that item was created + ansible.builtin.assert: + that: zbxhostdependitem_new is changed + +- name: test - attempt to delete previously created zabbix item + community.zabbix.zabbix_itemprototype: + name: '{% raw %}{#FSNAME}:TestItemPrototype{% endraw %}' + discoveryrule_name: ExampleHostRule + host_name: ExampleHost + state: absent + register: zbxhostmstitem_existing_delete + +- name: assert that item was deleted + ansible.builtin.assert: + that: zbxhostmstitem_existing_delete is changed + +- name: test - attempt to delete dependent item + community.zabbix.zabbix_itemprototype: + name: '{% raw %}{#FSNAME}:TestDependItemPrototype{% endraw %}' + discoveryrule_name: ExampleHostRule + host_name: ExampleHost + state: absent + register: zbxhostdependitem_delete + +- name: assert that the item had been removed with its master + ansible.builtin.assert: + that: not zbxhostdependitem_delete is changed + - name: remove host rule community.zabbix.zabbix_discoveryrule: name: ExampleHostRule @@ -106,7 +176,7 @@ type: zabbix_agent_active key: '{% raw %}vfs.fs.size[{#FSNAME},used]{% endraw %}' value_type: numeric_unsigned - units: GB + units: B interval: 1m tags: - tag: tag @@ -127,7 +197,7 @@ type: zabbix_agent_active key: '{% raw %}vfs.fs.size[{#FSNAME},used]{% endraw %}' value_type: numeric_unsigned - units: GB + units: B interval: 1m tags: - tag: tag