diff --git a/changelogs/fragments/host_info_all_hosts.yml b/changelogs/fragments/host_info_all_hosts.yml new file mode 100644 index 000000000..f930e6f48 --- /dev/null +++ b/changelogs/fragments/host_info_all_hosts.yml @@ -0,0 +1,2 @@ +minor_changes: + - zabbix_host_info - added ability to get all the hosts configured in Zabbix diff --git a/plugins/modules/zabbix_host_info.py b/plugins/modules/zabbix_host_info.py index 9576a118d..92c185227 100644 --- a/plugins/modules/zabbix_host_info.py +++ b/plugins/modules/zabbix_host_info.py @@ -35,6 +35,7 @@ - Name of the host in Zabbix. - host_name is the unique identifier used and cannot be updated using this module. - Required when I(host_ip) is not used. + - If neither host_name nor host_ip specified then all the hosts configured in Zabbix returned. required: false type: str default: "" @@ -207,7 +208,7 @@ def main(): host = Host(module) - if host_name: + if host_name != "" or (host_name == "" and len(host_ips) == 0): hosts = host.get_hosts_by_host_name(host_name, exact_match, host_inventory) if is_remove_duplicate: hosts = host.delete_duplicate_hosts(hosts) diff --git a/tests/integration/targets/test_zabbix_host/tasks/main.yml b/tests/integration/targets/test_zabbix_host/tasks/main.yml index 97b9f4612..c4675797f 100644 --- a/tests/integration/targets/test_zabbix_host/tasks/main.yml +++ b/tests/integration/targets/test_zabbix_host/tasks/main.yml @@ -14,6 +14,10 @@ always: - name: "cleanup if tests failed" community.zabbix.zabbix_host: - host_name: ExampleHost + host_name: "{{ item }}" state: absent ignore_errors: true + with_items: + - ExampleHost + - ExampleHost1 + - ExampleHost2 diff --git a/tests/integration/targets/test_zabbix_host_info/tasks/main.yml b/tests/integration/targets/test_zabbix_host_info/tasks/main.yml index 022d86c1a..72c2f6364 100644 --- a/tests/integration/targets/test_zabbix_host_info/tasks/main.yml +++ b/tests/integration/targets/test_zabbix_host_info/tasks/main.yml @@ -133,3 +133,16 @@ - ansible.builtin.assert: that: - exact_match_host_not_found_result.failed is sameas true + +- name: Get all hosts + community.zabbix.zabbix_host_info: + register: get_all_hosts_result + +- ansible.builtin.assert: + that: + - get_all_hosts_result.hosts | length == 2 + +- name: Clean up test host + community.zabbix.zabbix_host: + host_name: ExampleHostForHostInfoModule + state: absent