From d0efe3d9fe7b716cd294f4f64d03f7714ca5205a Mon Sep 17 00:00:00 2001 From: sky_joker Date: Tue, 26 Mar 2019 15:31:42 +0900 Subject: [PATCH] Added host inventory option to zabbix_host_facts module (#54341) --- .../monitoring/zabbix/zabbix_host_facts.py | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_host_facts.py b/lib/ansible/modules/monitoring/zabbix/zabbix_host_facts.py index f9e771e71a..1ace8c8a07 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_host_facts.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_host_facts.py @@ -54,6 +54,13 @@ options: - Remove duplicate host from host result type: bool default: yes + host_inventory: + description: + - List of host inventory keys to display in result. + - Whole host inventory is retrieved if keys are not specified. + type: list + required: false + version_added: 2.8 extends_documentation_fragment: - zabbix ''' @@ -70,6 +77,21 @@ EXAMPLES = ''' timeout: 10 exact_match: no remove_duplicate: yes + +- name: Reduce host inventory information to provided keys + local_action: + module: zabbix_host_facts + server_url: http://monitor.example.com + login_user: username + login_password: password + host_name: ExampleHost + host_inventory: + - os + - tag + host_ip: 127.0.0.1 + timeout: 10 + exact_match: no + remove_duplicate: yes ''' from ansible.module_utils.basic import AnsibleModule @@ -98,18 +120,19 @@ class Host(object): self._module = module self._zapi = zbx - def get_hosts_by_host_name(self, host_name, exact_match): + def get_hosts_by_host_name(self, host_name, exact_match, host_inventory): """ Get host by host name """ search_key = 'search' if exact_match: search_key = 'filter' - host_list = self._zapi.host.get({'output': 'extend', 'selectParentTemplates': ['name'], search_key: {'host': [host_name]}}) + host_list = self._zapi.host.get({'output': 'extend', 'selectParentTemplates': ['name'], search_key: {'host': [host_name]}, + 'selectInventory': host_inventory}) if len(host_list) < 1: self._module.fail_json(msg="Host not found: %s" % host_name) else: return host_list - def get_hosts_by_ip(self, host_ips): + def get_hosts_by_ip(self, host_ips, host_inventory): """ Get host by host ip(s) """ hostinterfaces = self._zapi.hostinterface.get({ 'output': 'extend', @@ -125,7 +148,8 @@ class Host(object): 'output': 'extend', 'selectGroups': 'extend', 'selectParentTemplates': ['name'], - 'hostids': hostinterface['hostid'] + 'hostids': hostinterface['hostid'], + 'selectInventory': host_inventory }) host[0]['hostinterfaces'] = hostinterface host_list.append(host[0]) @@ -156,7 +180,8 @@ def main(): validate_certs=dict(type='bool', required=False, default=True), timeout=dict(type='int', default=10), exact_match=dict(type='bool', required=False, default=False), - remove_duplicate=dict(type='bool', required=False, default=True) + remove_duplicate=dict(type='bool', required=False, default=True), + host_inventory=dict(type='list', default=[], required=False) ), supports_check_mode=True ) @@ -175,6 +200,10 @@ def main(): timeout = module.params['timeout'] exact_match = module.params['exact_match'] is_remove_duplicate = module.params['remove_duplicate'] + host_inventory = module.params['host_inventory'] + + if not host_inventory: + host_inventory = 'extend' zbx = None # login to zabbix @@ -188,7 +217,7 @@ def main(): host = Host(module, zbx) if host_name: - hosts = host.get_hosts_by_host_name(host_name, exact_match) + hosts = host.get_hosts_by_host_name(host_name, exact_match, host_inventory) if is_remove_duplicate: hosts = host.delete_duplicate_hosts(hosts) extended_hosts = [] @@ -200,7 +229,7 @@ def main(): module.exit_json(ok=True, hosts=extended_hosts) elif host_ips: - extended_hosts = host.get_hosts_by_ip(host_ips) + extended_hosts = host.get_hosts_by_ip(host_ips, host_inventory) if is_remove_duplicate: hosts = host.delete_duplicate_hosts(extended_hosts) module.exit_json(ok=True, hosts=extended_hosts)