Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to rename objects #1447

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelogs/fragments/pr_1447.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
minor_changes:
- zabbix_item - add support for renaming items
- zabbix_trigger - add support for renaming triggers
- zabbix_discoveryrule - add support for renaming discoveryrules
- zabbix_itemprototype - add support for renaming itemprototypes
- zabbix_triggerprototype - add support for renaming triggerprototypes
27 changes: 27 additions & 0 deletions plugins/modules/zabbix_discoveryrule.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
- http_agent
- snmp_agent
- script
new_name:
description:
- New name for LLD rule.
required: false
type: str
preprocessing:
description:
- discovery rules preprocessing options.
Expand Down Expand Up @@ -239,6 +244,23 @@
name: mounted_filesystem_discovery
template_name: example_template
state: absent

- name: Rename Zabbix LLD rule
# 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://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_discoveryrule:
name: mounted_filesystem_discovery
template_name: example_template
params:
new_name: new_mounted_filesystem_discovery
state: present
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -429,6 +451,8 @@ def main():
discoveryrules = discoveryrule.get_discoveryrules(name, host_name, template_name)
results = []
if len(discoveryrules) == 0:
if 'new_name' in params:
module.fail_json('Cannot rename discoveryrule: %s is not found' % name)
hosts_templates = discoveryrule.get_hosts_templates(host_name, template_name)
for host_template in hosts_templates:
if 'hostid' in host_template:
Expand All @@ -443,6 +467,9 @@ def main():
changed = False
for d in discoveryrules:
params['itemid'] = d['itemid']
if 'new_name' in params:
params['name'] = params['new_name']
params.pop("new_name")
results.append(discoveryrule.update_discoveryrule(params))
changed_rule = discoveryrule.check_discoveryrule_changed(d)
if changed_rule:
Expand Down
27 changes: 27 additions & 0 deletions plugins/modules/zabbix_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
- log
- numeric_unsigned
- text
new_name:
description:
- New name for item
required: false
type: str
master_item:
description:
- item that is the master of the current one
Expand Down Expand Up @@ -327,6 +332,23 @@
name: agent_ping
template_name: example_template
state: absent

- name: Rename Zabbix 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://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_item:
name: agent_ping
template_name: example_template
params:
new_name: new_agent_ping
state: present
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -554,6 +576,8 @@ def main():
items = item.get_items(name, host_name, template_name)
results = []
if len(items) == 0:
if 'new_name' in params:
module.fail_json('Cannot rename item: %s is not found' % name)
hosts_templates = item.get_hosts_templates(host_name, template_name)
for host_template in hosts_templates:
if 'hostid' in host_template:
Expand All @@ -568,6 +592,9 @@ def main():
changed = False
for i in items:
params['itemid'] = i['itemid']
if 'new_name' in params:
params['name'] = params['new_name']
params.pop("new_name")
results.append(item.update_item(params))
changed_item = item.check_item_changed(i)
if changed_item:
Expand Down
28 changes: 28 additions & 0 deletions plugins/modules/zabbix_itemprototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@
- log
- numeric_unsigned
- text
new_name:
description:
- New name for item
required: false
type: str
master_item:
description:
- item that is the master of the current one
Expand Down Expand Up @@ -344,6 +349,24 @@
discoveryrule_name: example_rule
template_name: example_template
state: absent

- name: Rename Zabbix item prototype
# 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://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_itemprototype:
name: '{% raw %}{#FSNAME}:Used space{% endraw %}'
discoveryrule_name: example_rule
template_name: example_template
params:
new_name: '{% raw %}{#FSNAME}:New Used space{% endraw %}'
state: present
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -583,6 +606,8 @@ def main():
itemprototypes = itemprototype.get_itemprototypes(name, discoveryrule_name, host_name, template_name)
results = []
if len(itemprototypes) == 0:
if 'new_name' in params:
module.fail_json('Cannot rename item prototype: %s is not found' % name)
hosts_templates = itemprototype.get_hosts_templates(host_name, template_name)
for host_template in hosts_templates:
if 'hostid' in host_template:
Expand All @@ -598,6 +623,9 @@ def main():
params.pop('ruleid')
for i in itemprototypes:
params['itemid'] = i['itemid']
if 'new_name' in params:
params['name'] = params['new_name']
params.pop("new_name")
results.append(itemprototype.update_itemprototype(params))
changed_item = itemprototype.check_itemprototype_changed(i)
if changed_item:
Expand Down
27 changes: 27 additions & 0 deletions plugins/modules/zabbix_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
- Overrides "status" in API docs.
required: false
type: bool
new_name:
description:
- New name for trigger
required: false
type: str
generate_multiple_events:
description:
- Whether the trigger can generate multiple problem events.
Expand Down Expand Up @@ -245,6 +250,23 @@
name: agent_ping
host_name: example_template
state: absent

- name: Rename Zabbix trigger
# 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://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_trigger:
name: agent_ping
template_name: example_template
params:
new_name: new_agent_ping
state: present
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -425,6 +447,8 @@ def main():
trigger.sanitize_params(name, params, desc, dependencies)
triggers = trigger.get_triggers(name, host_name, template_name)
if len(triggers) == 0:
if 'new_name' in params:
module.fail_json('Cannot rename trigger: %s is not found' % name)
results = trigger.add_trigger(params)
module.exit_json(changed=True, result=results)
else:
Expand All @@ -433,6 +457,9 @@ def main():
for t in triggers:
params['triggerid'] = t['triggerid']
params.pop('description')
if 'new_name' in params:
params['description'] = params['new_name']
params.pop("new_name")
results.append(trigger.update_trigger(params))
changed_trigger = trigger.check_trigger_changed(t)
if changed_trigger:
Expand Down
27 changes: 27 additions & 0 deletions plugins/modules/zabbix_triggerprototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
- Overrides "status" in API docs.
required: false
type: bool
new_name:
description:
- New name for trigger prototype
required: false
type: str
generate_multiple_events:
description:
- Whether the trigger prototype can generate multiple problem events.
Expand Down Expand Up @@ -249,6 +254,23 @@
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: example_template
state: absent

- name: Rename Zabbix trigger prototype
# 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://<FQDN>/zabbixeu
ansible_host: zabbix-example-fqdn.org
community.zabbix.zabbix_triggerprototype:
name: '{% raw %}Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
template_name: example_template
params:
new_name: '{% raw %}New Free disk space is less than 20% on volume {#FSNAME}{% endraw %}'
state: present
'''

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -429,6 +451,8 @@ def main():
triggerprototype.sanitize_params(name, params, desc, dependencies)
triggerprototypes = triggerprototype.get_triggerprototypes(name, host_name, template_name)
if len(triggerprototypes) == 0:
if 'new_name' in params:
module.fail_json('Cannot rename trigger prototype: %s is not found' % name)
results = triggerprototype.add_triggerprototype(params)
module.exit_json(changed=True, result=results)
else:
Expand All @@ -437,6 +461,9 @@ def main():
for t in triggerprototypes:
params['triggerid'] = t['triggerid']
params.pop('description')
if 'new_name' in params:
params['description'] = params['new_name']
params.pop("new_name")
results.append(triggerprototype.update_triggerprototype(params))
changed_trigger = triggerprototype.check_triggerprototype_changed(t)
if changed_trigger:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,37 @@
ansible.builtin.assert:
that: zbxtemprule_changed is changed

- name: test - attempt to delete previously created zabbix discoveryrule
- name: test - rename existing zabbix discoveryrule
community.zabbix.zabbix_discoveryrule:
name: TestRule
template_name: ExampleTemplate
params:
new_name: NewTestRule
state: present
register: zbxnamerule_changed

- name: expect to succeed and that things changed
ansible.builtin.assert:
that: zbxnamerule_changed is changed

- name: test - rename non-existing zabbix discoveryrule
community.zabbix.zabbix_discoveryrule:
name: TestRule
template_name: ExampleTemplate
params:
new_name: NewTestRule
state: present
ignore_errors: yes
register: zbxnamerule_failed

- name: expect to fail
ansible.builtin.assert:
that: zbxnamerule_failed is failed

- name: test - attempt to delete previously created zabbix discoveryrule
community.zabbix.zabbix_discoveryrule:
name: NewTestRule
template_name: ExampleTemplate
state: absent
register: zbxtemprule_existing_delete

Expand All @@ -125,7 +152,7 @@

- name: test - attempt to delete non-existing zabbix discoveryrule
community.zabbix.zabbix_discoveryrule:
name: TestRule
name: NewTestRule
template_name: ExampleTemplate
state: absent
register: zbxtemprule_missing_delete
Expand Down
31 changes: 29 additions & 2 deletions tests/integration/targets/test_zabbix_item/tasks/zabbix_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,37 @@
ansible.builtin.assert:
that: zbxtempitem_changed is changed

- name: test - attempt to delete previously created zabbix item
- name: test - rename existing zabbix item
community.zabbix.zabbix_item:
name: TestItem
template_name: ExampleTemplate
params:
new_name: NewTestItem
state: present
register: zbxnameitem_changed

- name: expect to succeed and that things changed
ansible.builtin.assert:
that: zbxnameitem_changed is changed

- name: test - rename non-existing zabbix item
community.zabbix.zabbix_item:
name: TestItem
template_name: ExampleTemplate
params:
new_name: NewTestItem
state: present
ignore_errors: yes
register: zbxnameitem_failed

- name: expect to fail
ansible.builtin.assert:
that: zbxnameitem_failed is failed

- name: test - attempt to delete previously created zabbix item
community.zabbix.zabbix_item:
name: NewTestItem
template_name: ExampleTemplate
state: absent
register: zbxtempitem_existing_delete

Expand All @@ -236,7 +263,7 @@

- name: test - attempt to delete non-existing zabbix item
community.zabbix.zabbix_item:
name: TestItem
name: NewTestItem
template_name: ExampleTemplate
state: absent
register: zbxtempitem_missing_delete
Expand Down
Loading
Loading