Added host inventory option to zabbix_host_facts module (#54341)

This commit is contained in:
sky_joker 2019-03-26 15:31:42 +09:00 committed by ansibot
parent 73faa376ad
commit d0efe3d9fe

View file

@ -54,6 +54,13 @@ options:
- Remove duplicate host from host result - Remove duplicate host from host result
type: bool type: bool
default: yes 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: extends_documentation_fragment:
- zabbix - zabbix
''' '''
@ -70,6 +77,21 @@ EXAMPLES = '''
timeout: 10 timeout: 10
exact_match: no exact_match: no
remove_duplicate: yes 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 from ansible.module_utils.basic import AnsibleModule
@ -98,18 +120,19 @@ class Host(object):
self._module = module self._module = module
self._zapi = zbx 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 """ """ Get host by host name """
search_key = 'search' search_key = 'search'
if exact_match: if exact_match:
search_key = 'filter' 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: if len(host_list) < 1:
self._module.fail_json(msg="Host not found: %s" % host_name) self._module.fail_json(msg="Host not found: %s" % host_name)
else: else:
return host_list 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) """ """ Get host by host ip(s) """
hostinterfaces = self._zapi.hostinterface.get({ hostinterfaces = self._zapi.hostinterface.get({
'output': 'extend', 'output': 'extend',
@ -125,7 +148,8 @@ class Host(object):
'output': 'extend', 'output': 'extend',
'selectGroups': 'extend', 'selectGroups': 'extend',
'selectParentTemplates': ['name'], 'selectParentTemplates': ['name'],
'hostids': hostinterface['hostid'] 'hostids': hostinterface['hostid'],
'selectInventory': host_inventory
}) })
host[0]['hostinterfaces'] = hostinterface host[0]['hostinterfaces'] = hostinterface
host_list.append(host[0]) host_list.append(host[0])
@ -156,7 +180,8 @@ def main():
validate_certs=dict(type='bool', required=False, default=True), validate_certs=dict(type='bool', required=False, default=True),
timeout=dict(type='int', default=10), timeout=dict(type='int', default=10),
exact_match=dict(type='bool', required=False, default=False), 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 supports_check_mode=True
) )
@ -175,6 +200,10 @@ def main():
timeout = module.params['timeout'] timeout = module.params['timeout']
exact_match = module.params['exact_match'] exact_match = module.params['exact_match']
is_remove_duplicate = module.params['remove_duplicate'] is_remove_duplicate = module.params['remove_duplicate']
host_inventory = module.params['host_inventory']
if not host_inventory:
host_inventory = 'extend'
zbx = None zbx = None
# login to zabbix # login to zabbix
@ -188,7 +217,7 @@ def main():
host = Host(module, zbx) host = Host(module, zbx)
if host_name: 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: if is_remove_duplicate:
hosts = host.delete_duplicate_hosts(hosts) hosts = host.delete_duplicate_hosts(hosts)
extended_hosts = [] extended_hosts = []
@ -200,7 +229,7 @@ def main():
module.exit_json(ok=True, hosts=extended_hosts) module.exit_json(ok=True, hosts=extended_hosts)
elif host_ips: 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: if is_remove_duplicate:
hosts = host.delete_duplicate_hosts(extended_hosts) hosts = host.delete_duplicate_hosts(extended_hosts)
module.exit_json(ok=True, hosts=extended_hosts) module.exit_json(ok=True, hosts=extended_hosts)